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