search_scope.py 4.6 KB

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