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.

link.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import posixpath
  2. import re
  3. from pip._vendor.six.moves.urllib import parse as urllib_parse
  4. from pip._internal.download import path_to_url
  5. from pip._internal.utils.misc import (
  6. WHEEL_EXTENSION, redact_password_from_url, splitext,
  7. )
  8. from pip._internal.utils.models import KeyBasedCompareMixin
  9. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  10. if MYPY_CHECK_RUNNING:
  11. from typing import Optional, Tuple, Union, Text # noqa: F401
  12. from pip._internal.index import HTMLPage # noqa: F401
  13. class Link(KeyBasedCompareMixin):
  14. """Represents a parsed link from a Package Index's simple URL
  15. """
  16. def __init__(self, url, comes_from=None, requires_python=None):
  17. # type: (str, Optional[Union[str, HTMLPage]], Optional[str]) -> None
  18. """
  19. url:
  20. url of the resource pointed to (href of the link)
  21. comes_from:
  22. instance of HTMLPage where the link was found, or string.
  23. requires_python:
  24. String containing the `Requires-Python` metadata field, specified
  25. in PEP 345. This may be specified by a data-requires-python
  26. attribute in the HTML link tag, as described in PEP 503.
  27. """
  28. # url can be a UNC windows share
  29. if url.startswith('\\\\'):
  30. url = path_to_url(url)
  31. self.url = url
  32. self.comes_from = comes_from
  33. self.requires_python = requires_python if requires_python else None
  34. super(Link, self).__init__(
  35. key=(self.url),
  36. defining_class=Link
  37. )
  38. def __str__(self):
  39. if self.requires_python:
  40. rp = ' (requires-python:%s)' % self.requires_python
  41. else:
  42. rp = ''
  43. if self.comes_from:
  44. return '%s (from %s)%s' % (redact_password_from_url(self.url),
  45. self.comes_from, rp)
  46. else:
  47. return redact_password_from_url(str(self.url))
  48. def __repr__(self):
  49. return '<Link %s>' % self
  50. @property
  51. def filename(self):
  52. # type: () -> str
  53. _, netloc, path, _, _ = urllib_parse.urlsplit(self.url)
  54. name = posixpath.basename(path.rstrip('/')) or netloc
  55. name = urllib_parse.unquote(name)
  56. assert name, ('URL %r produced no filename' % self.url)
  57. return name
  58. @property
  59. def scheme(self):
  60. # type: () -> str
  61. return urllib_parse.urlsplit(self.url)[0]
  62. @property
  63. def netloc(self):
  64. # type: () -> str
  65. return urllib_parse.urlsplit(self.url)[1]
  66. @property
  67. def path(self):
  68. # type: () -> str
  69. return urllib_parse.unquote(urllib_parse.urlsplit(self.url)[2])
  70. def splitext(self):
  71. # type: () -> Tuple[str, str]
  72. return splitext(posixpath.basename(self.path.rstrip('/')))
  73. @property
  74. def ext(self):
  75. # type: () -> str
  76. return self.splitext()[1]
  77. @property
  78. def url_without_fragment(self):
  79. # type: () -> str
  80. scheme, netloc, path, query, fragment = urllib_parse.urlsplit(self.url)
  81. return urllib_parse.urlunsplit((scheme, netloc, path, query, None))
  82. _egg_fragment_re = re.compile(r'[#&]egg=([^&]*)')
  83. @property
  84. def egg_fragment(self):
  85. # type: () -> Optional[str]
  86. match = self._egg_fragment_re.search(self.url)
  87. if not match:
  88. return None
  89. return match.group(1)
  90. _subdirectory_fragment_re = re.compile(r'[#&]subdirectory=([^&]*)')
  91. @property
  92. def subdirectory_fragment(self):
  93. # type: () -> Optional[str]
  94. match = self._subdirectory_fragment_re.search(self.url)
  95. if not match:
  96. return None
  97. return match.group(1)
  98. _hash_re = re.compile(
  99. r'(sha1|sha224|sha384|sha256|sha512|md5)=([a-f0-9]+)'
  100. )
  101. @property
  102. def hash(self):
  103. # type: () -> Optional[str]
  104. match = self._hash_re.search(self.url)
  105. if match:
  106. return match.group(2)
  107. return None
  108. @property
  109. def hash_name(self):
  110. # type: () -> Optional[str]
  111. match = self._hash_re.search(self.url)
  112. if match:
  113. return match.group(1)
  114. return None
  115. @property
  116. def show_url(self):
  117. # type: () -> Optional[str]
  118. return posixpath.basename(self.url.split('#', 1)[0].split('?', 1)[0])
  119. @property
  120. def is_wheel(self):
  121. # type: () -> bool
  122. return self.ext == WHEEL_EXTENSION
  123. @property
  124. def is_artifact(self):
  125. # type: () -> bool
  126. """
  127. Determines if this points to an actual artifact (e.g. a tarball) or if
  128. it points to an "abstract" thing like a path or a VCS location.
  129. """
  130. from pip._internal.vcs import vcs
  131. if self.scheme in vcs.all_schemes:
  132. return False
  133. return True