search_scope.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import itertools
  2. import logging
  3. import os
  4. import posixpath
  5. import urllib.parse
  6. from dataclasses import dataclass
  7. from pip._vendor.packaging.utils import canonicalize_name
  8. from pip._internal.models.index import PyPI
  9. from pip._internal.utils.compat import has_tls
  10. from pip._internal.utils.misc import normalize_path, redact_auth_from_url
  11. logger = logging.getLogger(__name__)
  12. @dataclass(frozen=True)
  13. class SearchScope:
  14. """
  15. Encapsulates the locations that pip is configured to search.
  16. """
  17. __slots__ = ["find_links", "index_urls", "no_index"]
  18. find_links: list[str]
  19. index_urls: list[str]
  20. no_index: bool
  21. @classmethod
  22. def create(
  23. cls,
  24. find_links: list[str],
  25. index_urls: list[str],
  26. no_index: bool,
  27. ) -> "SearchScope":
  28. """
  29. Create a SearchScope object after normalizing the `find_links`.
  30. """
  31. # Build find_links. If an argument starts with ~, it may be
  32. # a local file relative to a home directory. So try normalizing
  33. # it and if it exists, use the normalized version.
  34. # This is deliberately conservative - it might be fine just to
  35. # blindly normalize anything starting with a ~...
  36. built_find_links: list[str] = []
  37. for link in find_links:
  38. if link.startswith("~"):
  39. new_link = normalize_path(link)
  40. if os.path.exists(new_link):
  41. link = new_link
  42. built_find_links.append(link)
  43. # If we don't have TLS enabled, then WARN if anyplace we're looking
  44. # relies on TLS.
  45. if not has_tls():
  46. for link in itertools.chain(index_urls, built_find_links):
  47. parsed = urllib.parse.urlparse(link)
  48. if parsed.scheme == "https":
  49. logger.warning(
  50. "pip is configured with locations that require "
  51. "TLS/SSL, however the ssl module in Python is not "
  52. "available."
  53. )
  54. break
  55. return cls(
  56. find_links=built_find_links,
  57. index_urls=index_urls,
  58. no_index=no_index,
  59. )
  60. def get_formatted_locations(self) -> str:
  61. lines = []
  62. redacted_index_urls = []
  63. if self.index_urls and self.index_urls != [PyPI.simple_url]:
  64. for url in self.index_urls:
  65. redacted_index_url = redact_auth_from_url(url)
  66. # Parse the URL
  67. purl = urllib.parse.urlsplit(redacted_index_url)
  68. # URL is generally invalid if scheme and netloc is missing
  69. # there are issues with Python and URL parsing, so this test
  70. # is a bit crude. See bpo-20271, bpo-23505. Python doesn't
  71. # always parse invalid URLs correctly - it should raise
  72. # exceptions for malformed URLs
  73. if not purl.scheme and not purl.netloc:
  74. logger.warning(
  75. 'The index url "%s" seems invalid, please provide a scheme.',
  76. redacted_index_url,
  77. )
  78. redacted_index_urls.append(redacted_index_url)
  79. lines.append(
  80. "Looking in indexes: {}".format(", ".join(redacted_index_urls))
  81. )
  82. if self.find_links:
  83. lines.append(
  84. "Looking in links: {}".format(
  85. ", ".join(redact_auth_from_url(url) for url in self.find_links)
  86. )
  87. )
  88. return "\n".join(lines)
  89. def get_index_urls_locations(self, project_name: str) -> list[str]:
  90. """Returns the locations found via self.index_urls
  91. Checks the url_name on the main (first in the list) index and
  92. use this url_name to produce all locations
  93. """
  94. def mkurl_pypi_url(url: str) -> str:
  95. loc = posixpath.join(
  96. url, urllib.parse.quote(canonicalize_name(project_name))
  97. )
  98. # For maximum compatibility with easy_install, ensure the path
  99. # ends in a trailing slash. Although this isn't in the spec
  100. # (and PyPI can handle it without the slash) some other index
  101. # implementations might break if they relied on easy_install's
  102. # behavior.
  103. if not loc.endswith("/"):
  104. loc = loc + "/"
  105. return loc
  106. return [mkurl_pypi_url(url) for url in self.index_urls]