link.py 7.3 KB

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