pip/src/pip/_internal/models/link.py

228 lines
6.7 KiB
Python
Raw Normal View History

# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False
2019-09-20 08:09:21 +02:00
import os
2018-06-18 12:59:56 +02:00
import posixpath
import re
from pip._vendor.six.moves.urllib import parse as urllib_parse
from pip._internal.utils.filetypes import WHEEL_EXTENSION
from pip._internal.utils.misc import (
redact_auth_from_url,
2019-07-22 06:45:27 +02:00
split_auth_from_netloc,
splitext,
)
from pip._internal.utils.models import KeyBasedCompareMixin
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
2019-09-24 10:56:42 +02:00
from pip._internal.utils.urls import path_to_url, url_to_path
if MYPY_CHECK_RUNNING:
from typing import Optional, Text, Tuple, Union
from pip._internal.collector import HTMLPage
2019-07-05 09:48:17 +02:00
from pip._internal.utils.hashes import Hashes
2018-06-18 12:59:56 +02:00
class Link(KeyBasedCompareMixin):
2018-06-18 12:59:56 +02:00
"""Represents a parsed link from a Package Index's simple URL
"""
2019-06-24 16:37:13 +02:00
def __init__(
self,
url, # type: str
comes_from=None, # type: Optional[Union[str, HTMLPage]]
requires_python=None, # type: Optional[str]
yanked_reason=None, # type: Optional[Text]
2019-06-24 16:37:13 +02:00
):
# type: (...) -> None
2018-06-18 12:59:56 +02:00
"""
2019-06-24 16:37:13 +02:00
:param url: url of the resource pointed to (href of the link)
:param comes_from: instance of HTMLPage where the link was found,
or string.
:param requires_python: String containing the `Requires-Python`
metadata field, specified in PEP 345. This may be specified by
a data-requires-python attribute in the HTML link tag, as
described in PEP 503.
:param yanked_reason: the reason the file has been yanked, if the
file has been yanked, or None if the file hasn't been yanked.
This is the value of the "data-yanked" attribute, if present, in
a simple repository HTML link. If the file has been yanked but
no reason was provided, this should be the empty string. See
PEP 592 for more information and the specification.
2018-06-18 12:59:56 +02:00
"""
# url can be a UNC windows share
if url.startswith('\\\\'):
url = path_to_url(url)
self._parsed_url = urllib_parse.urlsplit(url)
# Store the url as a private attribute to prevent accidentally
# trying to set a new value.
self._url = url
2018-06-18 12:59:56 +02:00
self.comes_from = comes_from
self.requires_python = requires_python if requires_python else None
2019-06-24 16:37:13 +02:00
self.yanked_reason = yanked_reason
2018-06-18 12:59:56 +02:00
super(Link, self).__init__(key=url, defining_class=Link)
2018-06-18 12:59:56 +02:00
def __str__(self):
if self.requires_python:
rp = ' (requires-python:%s)' % self.requires_python
else:
rp = ''
if self.comes_from:
return '%s (from %s)%s' % (redact_auth_from_url(self._url),
self.comes_from, rp)
2018-06-18 12:59:56 +02:00
else:
return redact_auth_from_url(str(self._url))
2018-06-18 12:59:56 +02:00
def __repr__(self):
return '<Link %s>' % self
@property
def url(self):
# type: () -> str
return self._url
2018-06-18 12:59:56 +02:00
@property
def filename(self):
# type: () -> str
path = self.path.rstrip('/')
name = posixpath.basename(path)
if not name:
# Make sure we don't leak auth information if the netloc
# includes a username and password.
netloc, user_pass = split_auth_from_netloc(self.netloc)
return netloc
2018-06-18 12:59:56 +02:00
name = urllib_parse.unquote(name)
assert name, ('URL %r produced no filename' % self._url)
2018-06-18 12:59:56 +02:00
return name
2019-09-20 02:14:10 +02:00
@property
def file_path(self):
# type: () -> str
return url_to_path(self.url)
2018-06-18 12:59:56 +02:00
@property
def scheme(self):
# type: () -> str
2019-06-24 11:26:14 +02:00
return self._parsed_url.scheme
2018-06-18 12:59:56 +02:00
@property
def netloc(self):
# type: () -> str
"""
This can contain auth information.
"""
2019-06-24 11:26:14 +02:00
return self._parsed_url.netloc
2018-06-18 12:59:56 +02:00
@property
def path(self):
# type: () -> str
2019-06-24 11:26:14 +02:00
return urllib_parse.unquote(self._parsed_url.path)
2018-06-18 12:59:56 +02:00
def splitext(self):
# type: () -> Tuple[str, str]
2018-06-18 12:59:56 +02:00
return splitext(posixpath.basename(self.path.rstrip('/')))
@property
def ext(self):
# type: () -> str
2018-06-18 12:59:56 +02:00
return self.splitext()[1]
@property
def url_without_fragment(self):
# type: () -> str
scheme, netloc, path, query, fragment = self._parsed_url
2018-06-18 12:59:56 +02:00
return urllib_parse.urlunsplit((scheme, netloc, path, query, None))
_egg_fragment_re = re.compile(r'[#&]egg=([^&]*)')
@property
def egg_fragment(self):
# type: () -> Optional[str]
match = self._egg_fragment_re.search(self._url)
2018-06-18 12:59:56 +02:00
if not match:
return None
return match.group(1)
_subdirectory_fragment_re = re.compile(r'[#&]subdirectory=([^&]*)')
@property
def subdirectory_fragment(self):
# type: () -> Optional[str]
match = self._subdirectory_fragment_re.search(self._url)
2018-06-18 12:59:56 +02:00
if not match:
return None
return match.group(1)
_hash_re = re.compile(
r'(sha1|sha224|sha384|sha256|sha512|md5)=([a-f0-9]+)'
)
@property
def hash(self):
# type: () -> Optional[str]
match = self._hash_re.search(self._url)
2018-06-18 12:59:56 +02:00
if match:
return match.group(2)
return None
@property
def hash_name(self):
# type: () -> Optional[str]
match = self._hash_re.search(self._url)
2018-06-18 12:59:56 +02:00
if match:
return match.group(1)
return None
@property
def show_url(self):
# type: () -> Optional[str]
return posixpath.basename(self._url.split('#', 1)[0].split('?', 1)[0])
2018-06-18 12:59:56 +02:00
2019-09-20 08:09:21 +02:00
@property
def is_file(self):
# type: () -> bool
return self.scheme == 'file'
def is_existing_dir(self):
# type: () -> bool
return self.is_file and os.path.isdir(self.file_path)
2018-06-18 12:59:56 +02:00
@property
def is_wheel(self):
# type: () -> bool
return self.ext == WHEEL_EXTENSION
2018-06-18 12:59:56 +02:00
2019-08-15 23:00:55 +02:00
@property
def is_vcs(self):
# type: () -> bool
from pip._internal.vcs import vcs
return self.scheme in vcs.all_schemes
2019-06-24 16:37:13 +02:00
@property
def is_yanked(self):
# type: () -> bool
return self.yanked_reason is not None
2019-07-05 09:48:17 +02:00
@property
def has_hash(self):
return self.hash_name is not None
def is_hash_allowed(self, hashes):
# type: (Optional[Hashes]) -> bool
2019-07-05 09:48:17 +02:00
"""
Return True if the link has a hash and it is allowed.
"""
if hashes is None or not self.has_hash:
2019-07-05 09:48:17 +02:00
return False
# Assert non-None so mypy knows self.hash_name and self.hash are str.
assert self.hash_name is not None
assert self.hash is not None
2019-07-05 09:48:17 +02:00
return hashes.is_hash_allowed(self.hash_name, hex_digest=self.hash)