link.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import os
  2. import posixpath
  3. import re
  4. import urllib.parse
  5. from typing import TYPE_CHECKING, Optional, Tuple, Union
  6. from pip._internal.utils.filetypes import WHEEL_EXTENSION
  7. from pip._internal.utils.hashes import Hashes
  8. from pip._internal.utils.misc import (
  9. redact_auth_from_url,
  10. split_auth_from_netloc,
  11. splitext,
  12. )
  13. from pip._internal.utils.models import KeyBasedCompareMixin
  14. from pip._internal.utils.urls import path_to_url, url_to_path
  15. if TYPE_CHECKING:
  16. from pip._internal.index.collector import HTMLPage
  17. class Link(KeyBasedCompareMixin):
  18. """Represents a parsed link from a Package Index's simple URL
  19. """
  20. __slots__ = [
  21. "_parsed_url",
  22. "_url",
  23. "comes_from",
  24. "requires_python",
  25. "yanked_reason",
  26. "cache_link_parsing",
  27. ]
  28. def __init__(
  29. self,
  30. url, # type: str
  31. comes_from=None, # type: Optional[Union[str, HTMLPage]]
  32. requires_python=None, # type: Optional[str]
  33. yanked_reason=None, # type: Optional[str]
  34. cache_link_parsing=True, # type: bool
  35. ):
  36. # type: (...) -> None
  37. """
  38. :param url: url of the resource pointed to (href of the link)
  39. :param comes_from: instance of HTMLPage where the link was found,
  40. or string.
  41. :param requires_python: String containing the `Requires-Python`
  42. metadata field, specified in PEP 345. This may be specified by
  43. a data-requires-python attribute in the HTML link tag, as
  44. described in PEP 503.
  45. :param yanked_reason: the reason the file has been yanked, if the
  46. file has been yanked, or None if the file hasn't been yanked.
  47. This is the value of the "data-yanked" attribute, if present, in
  48. a simple repository HTML link. If the file has been yanked but
  49. no reason was provided, this should be the empty string. See
  50. PEP 592 for more information and the specification.
  51. :param cache_link_parsing: A flag that is used elsewhere to determine
  52. whether resources retrieved from this link
  53. should be cached. PyPI index urls should
  54. generally have this set to False, for
  55. example.
  56. """
  57. # url can be a UNC windows share
  58. if url.startswith('\\\\'):
  59. url = path_to_url(url)
  60. self._parsed_url = urllib.parse.urlsplit(url)
  61. # Store the url as a private attribute to prevent accidentally
  62. # trying to set a new value.
  63. self._url = url
  64. self.comes_from = comes_from
  65. self.requires_python = requires_python if requires_python else None
  66. self.yanked_reason = yanked_reason
  67. super().__init__(key=url, defining_class=Link)
  68. self.cache_link_parsing = cache_link_parsing
  69. def __str__(self):
  70. # type: () -> str
  71. if self.requires_python:
  72. rp = f' (requires-python:{self.requires_python})'
  73. else:
  74. rp = ''
  75. if self.comes_from:
  76. return '{} (from {}){}'.format(
  77. redact_auth_from_url(self._url), self.comes_from, rp)
  78. else:
  79. return redact_auth_from_url(str(self._url))
  80. def __repr__(self):
  81. # type: () -> str
  82. return f'<Link {self}>'
  83. @property
  84. def url(self):
  85. # type: () -> str
  86. return self._url
  87. @property
  88. def filename(self):
  89. # type: () -> str
  90. path = self.path.rstrip('/')
  91. name = posixpath.basename(path)
  92. if not name:
  93. # Make sure we don't leak auth information if the netloc
  94. # includes a username and password.
  95. netloc, user_pass = split_auth_from_netloc(self.netloc)
  96. return netloc
  97. name = urllib.parse.unquote(name)
  98. assert name, f'URL {self._url!r} produced no filename'
  99. return name
  100. @property
  101. def file_path(self):
  102. # type: () -> str
  103. return url_to_path(self.url)
  104. @property
  105. def scheme(self):
  106. # type: () -> str
  107. return self._parsed_url.scheme
  108. @property
  109. def netloc(self):
  110. # type: () -> str
  111. """
  112. This can contain auth information.
  113. """
  114. return self._parsed_url.netloc
  115. @property
  116. def path(self):
  117. # type: () -> str
  118. return urllib.parse.unquote(self._parsed_url.path)
  119. def splitext(self):
  120. # type: () -> Tuple[str, str]
  121. return splitext(posixpath.basename(self.path.rstrip('/')))
  122. @property
  123. def ext(self):
  124. # type: () -> str
  125. return self.splitext()[1]
  126. @property
  127. def url_without_fragment(self):
  128. # type: () -> str
  129. scheme, netloc, path, query, fragment = self._parsed_url
  130. return urllib.parse.urlunsplit((scheme, netloc, path, query, None))
  131. _egg_fragment_re = re.compile(r'[#&]egg=([^&]*)')
  132. @property
  133. def egg_fragment(self):
  134. # type: () -> Optional[str]
  135. match = self._egg_fragment_re.search(self._url)
  136. if not match:
  137. return None
  138. return match.group(1)
  139. _subdirectory_fragment_re = re.compile(r'[#&]subdirectory=([^&]*)')
  140. @property
  141. def subdirectory_fragment(self):
  142. # type: () -> Optional[str]
  143. match = self._subdirectory_fragment_re.search(self._url)
  144. if not match:
  145. return None
  146. return match.group(1)
  147. _hash_re = re.compile(
  148. r'(sha1|sha224|sha384|sha256|sha512|md5)=([a-f0-9]+)'
  149. )
  150. @property
  151. def hash(self):
  152. # type: () -> Optional[str]
  153. match = self._hash_re.search(self._url)
  154. if match:
  155. return match.group(2)
  156. return None
  157. @property
  158. def hash_name(self):
  159. # type: () -> Optional[str]
  160. match = self._hash_re.search(self._url)
  161. if match:
  162. return match.group(1)
  163. return None
  164. @property
  165. def show_url(self):
  166. # type: () -> str
  167. return posixpath.basename(self._url.split('#', 1)[0].split('?', 1)[0])
  168. @property
  169. def is_file(self):
  170. # type: () -> bool
  171. return self.scheme == 'file'
  172. def is_existing_dir(self):
  173. # type: () -> bool
  174. return self.is_file and os.path.isdir(self.file_path)
  175. @property
  176. def is_wheel(self):
  177. # type: () -> bool
  178. return self.ext == WHEEL_EXTENSION
  179. @property
  180. def is_vcs(self):
  181. # type: () -> bool
  182. from pip._internal.vcs import vcs
  183. return self.scheme in vcs.all_schemes
  184. @property
  185. def is_yanked(self):
  186. # type: () -> bool
  187. return self.yanked_reason is not None
  188. @property
  189. def has_hash(self):
  190. # type: () -> bool
  191. return self.hash_name is not None
  192. def is_hash_allowed(self, hashes):
  193. # type: (Optional[Hashes]) -> bool
  194. """
  195. Return True if the link has a hash and it is allowed.
  196. """
  197. if hashes is None or not self.has_hash:
  198. return False
  199. # Assert non-None so mypy knows self.hash_name and self.hash are str.
  200. assert self.hash_name is not None
  201. assert self.hash is not None
  202. return hashes.is_hash_allowed(self.hash_name, hex_digest=self.hash)
  203. # TODO: Relax this comparison logic to ignore, for example, fragments.
  204. def links_equivalent(link1, link2):
  205. # type: (Link, Link) -> bool
  206. return link1 == link2