search_scope.py 4.5 KB

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