You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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