Path: blob/master/venv/Lib/site-packages/pip/_internal/models/link.py
811 views
import os1import posixpath2import re34from pip._vendor.six.moves.urllib import parse as urllib_parse56from pip._internal.utils.filetypes import WHEEL_EXTENSION7from pip._internal.utils.misc import (8redact_auth_from_url,9split_auth_from_netloc,10splitext,11)12from pip._internal.utils.models import KeyBasedCompareMixin13from pip._internal.utils.typing import MYPY_CHECK_RUNNING14from pip._internal.utils.urls import path_to_url, url_to_path1516if MYPY_CHECK_RUNNING:17from typing import Optional, Text, Tuple, Union18from pip._internal.index.collector import HTMLPage19from pip._internal.utils.hashes import Hashes202122class Link(KeyBasedCompareMixin):23"""Represents a parsed link from a Package Index's simple URL24"""2526def __init__(27self,28url, # type: str29comes_from=None, # type: Optional[Union[str, HTMLPage]]30requires_python=None, # type: Optional[str]31yanked_reason=None, # type: Optional[Text]32cache_link_parsing=True, # type: bool33):34# type: (...) -> None35"""36:param url: url of the resource pointed to (href of the link)37:param comes_from: instance of HTMLPage where the link was found,38or string.39:param requires_python: String containing the `Requires-Python`40metadata field, specified in PEP 345. This may be specified by41a data-requires-python attribute in the HTML link tag, as42described in PEP 503.43:param yanked_reason: the reason the file has been yanked, if the44file has been yanked, or None if the file hasn't been yanked.45This is the value of the "data-yanked" attribute, if present, in46a simple repository HTML link. If the file has been yanked but47no reason was provided, this should be the empty string. See48PEP 592 for more information and the specification.49:param cache_link_parsing: A flag that is used elsewhere to determine50whether resources retrieved from this link51should be cached. PyPI index urls should52generally have this set to False, for53example.54"""5556# url can be a UNC windows share57if url.startswith('\\\\'):58url = path_to_url(url)5960self._parsed_url = urllib_parse.urlsplit(url)61# Store the url as a private attribute to prevent accidentally62# trying to set a new value.63self._url = url6465self.comes_from = comes_from66self.requires_python = requires_python if requires_python else None67self.yanked_reason = yanked_reason6869super(Link, self).__init__(key=url, defining_class=Link)7071self.cache_link_parsing = cache_link_parsing7273def __str__(self):74# type: () -> str75if self.requires_python:76rp = ' (requires-python:{})'.format(self.requires_python)77else:78rp = ''79if self.comes_from:80return '{} (from {}){}'.format(81redact_auth_from_url(self._url), self.comes_from, rp)82else:83return redact_auth_from_url(str(self._url))8485def __repr__(self):86# type: () -> str87return '<Link {}>'.format(self)8889@property90def url(self):91# type: () -> str92return self._url9394@property95def filename(self):96# type: () -> str97path = self.path.rstrip('/')98name = posixpath.basename(path)99if not name:100# Make sure we don't leak auth information if the netloc101# includes a username and password.102netloc, user_pass = split_auth_from_netloc(self.netloc)103return netloc104105name = urllib_parse.unquote(name)106assert name, (107'URL {self._url!r} produced no filename'.format(**locals()))108return name109110@property111def file_path(self):112# type: () -> str113return url_to_path(self.url)114115@property116def scheme(self):117# type: () -> str118return self._parsed_url.scheme119120@property121def netloc(self):122# type: () -> str123"""124This can contain auth information.125"""126return self._parsed_url.netloc127128@property129def path(self):130# type: () -> str131return urllib_parse.unquote(self._parsed_url.path)132133def splitext(self):134# type: () -> Tuple[str, str]135return splitext(posixpath.basename(self.path.rstrip('/')))136137@property138def ext(self):139# type: () -> str140return self.splitext()[1]141142@property143def url_without_fragment(self):144# type: () -> str145scheme, netloc, path, query, fragment = self._parsed_url146return urllib_parse.urlunsplit((scheme, netloc, path, query, None))147148_egg_fragment_re = re.compile(r'[#&]egg=([^&]*)')149150@property151def egg_fragment(self):152# type: () -> Optional[str]153match = self._egg_fragment_re.search(self._url)154if not match:155return None156return match.group(1)157158_subdirectory_fragment_re = re.compile(r'[#&]subdirectory=([^&]*)')159160@property161def subdirectory_fragment(self):162# type: () -> Optional[str]163match = self._subdirectory_fragment_re.search(self._url)164if not match:165return None166return match.group(1)167168_hash_re = re.compile(169r'(sha1|sha224|sha384|sha256|sha512|md5)=([a-f0-9]+)'170)171172@property173def hash(self):174# type: () -> Optional[str]175match = self._hash_re.search(self._url)176if match:177return match.group(2)178return None179180@property181def hash_name(self):182# type: () -> Optional[str]183match = self._hash_re.search(self._url)184if match:185return match.group(1)186return None187188@property189def show_url(self):190# type: () -> str191return posixpath.basename(self._url.split('#', 1)[0].split('?', 1)[0])192193@property194def is_file(self):195# type: () -> bool196return self.scheme == 'file'197198def is_existing_dir(self):199# type: () -> bool200return self.is_file and os.path.isdir(self.file_path)201202@property203def is_wheel(self):204# type: () -> bool205return self.ext == WHEEL_EXTENSION206207@property208def is_vcs(self):209# type: () -> bool210from pip._internal.vcs import vcs211212return self.scheme in vcs.all_schemes213214@property215def is_yanked(self):216# type: () -> bool217return self.yanked_reason is not None218219@property220def has_hash(self):221# type: () -> bool222return self.hash_name is not None223224def is_hash_allowed(self, hashes):225# type: (Optional[Hashes]) -> bool226"""227Return True if the link has a hash and it is allowed.228"""229if hashes is None or not self.has_hash:230return False231# Assert non-None so mypy knows self.hash_name and self.hash are str.232assert self.hash_name is not None233assert self.hash is not None234235return hashes.is_hash_allowed(self.hash_name, hex_digest=self.hash)236237238