Change venv

This commit is contained in:
Ambulance Clerc
2023-05-31 08:31:22 +02:00
parent fb6f579089
commit fdbb52c96f
466 changed files with 25899 additions and 64721 deletions

View File

@@ -6,6 +6,7 @@ import shutil
import sys
import urllib.parse
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterable,
@@ -30,10 +31,22 @@ from pip._internal.utils.misc import (
is_installable_dir,
rmtree,
)
from pip._internal.utils.subprocess import CommandArgs, call_subprocess, make_command
from pip._internal.utils.subprocess import (
CommandArgs,
call_subprocess,
format_command_args,
make_command,
)
from pip._internal.utils.urls import get_url_scheme
__all__ = ['vcs']
if TYPE_CHECKING:
# Literal was introduced in Python 3.8.
#
# TODO: Remove `if TYPE_CHECKING` when dropping support for Python 3.7.
from typing import Literal
__all__ = ["vcs"]
logger = logging.getLogger(__name__)
@@ -41,19 +54,19 @@ logger = logging.getLogger(__name__)
AuthInfo = Tuple[Optional[str], Optional[str]]
def is_url(name):
# type: (str) -> bool
def is_url(name: str) -> bool:
"""
Return true if the name looks like a URL.
"""
scheme = get_url_scheme(name)
if scheme is None:
return False
return scheme in ['http', 'https', 'file', 'ftp'] + vcs.all_schemes
return scheme in ["http", "https", "file", "ftp"] + vcs.all_schemes
def make_vcs_requirement_url(repo_url, rev, project_name, subdir=None):
# type: (str, str, str, Optional[str]) -> str
def make_vcs_requirement_url(
repo_url: str, rev: str, project_name: str, subdir: Optional[str] = None
) -> str:
"""
Return the URL for a VCS requirement.
@@ -62,15 +75,16 @@ def make_vcs_requirement_url(repo_url, rev, project_name, subdir=None):
project_name: the (unescaped) project name.
"""
egg_project_name = project_name.replace("-", "_")
req = f'{repo_url}@{rev}#egg={egg_project_name}'
req = f"{repo_url}@{rev}#egg={egg_project_name}"
if subdir:
req += f'&subdirectory={subdir}'
req += f"&subdirectory={subdir}"
return req
def find_path_to_project_root_from_repo_root(location, repo_root):
# type: (str, str) -> Optional[str]
def find_path_to_project_root_from_repo_root(
location: str, repo_root: str
) -> Optional[str]:
"""
Find the the Python project's root by searching up the filesystem from
`location`. Return the path to project root relative to `repo_root`.
@@ -118,11 +132,10 @@ class RevOptions:
def __init__(
self,
vc_class, # type: Type[VersionControl]
rev=None, # type: Optional[str]
extra_args=None, # type: Optional[CommandArgs]
):
# type: (...) -> None
vc_class: Type["VersionControl"],
rev: Optional[str] = None,
extra_args: Optional[CommandArgs] = None,
) -> None:
"""
Args:
vc_class: a VersionControl subclass.
@@ -135,26 +148,23 @@ class RevOptions:
self.extra_args = extra_args
self.rev = rev
self.vc_class = vc_class
self.branch_name = None # type: Optional[str]
self.branch_name: Optional[str] = None
def __repr__(self):
# type: () -> str
return f'<RevOptions {self.vc_class.name}: rev={self.rev!r}>'
def __repr__(self) -> str:
return f"<RevOptions {self.vc_class.name}: rev={self.rev!r}>"
@property
def arg_rev(self):
# type: () -> Optional[str]
def arg_rev(self) -> Optional[str]:
if self.rev is None:
return self.vc_class.default_arg_rev
return self.rev
def to_args(self):
# type: () -> CommandArgs
def to_args(self) -> CommandArgs:
"""
Return the VCS-specific command arguments.
"""
args = [] # type: CommandArgs
args: CommandArgs = []
rev = self.arg_rev
if rev is not None:
args += self.vc_class.get_base_rev_args(rev)
@@ -162,15 +172,13 @@ class RevOptions:
return args
def to_display(self):
# type: () -> str
def to_display(self) -> str:
if not self.rev:
return ''
return ""
return f' (to revision {self.rev})'
return f" (to revision {self.rev})"
def make_new(self, rev):
# type: (str) -> RevOptions
def make_new(self, rev: str) -> "RevOptions":
"""
Make a copy of the current instance, but with a new rev.
@@ -181,54 +189,46 @@ class RevOptions:
class VcsSupport:
_registry = {} # type: Dict[str, VersionControl]
schemes = ['ssh', 'git', 'hg', 'bzr', 'sftp', 'svn']
_registry: Dict[str, "VersionControl"] = {}
schemes = ["ssh", "git", "hg", "bzr", "sftp", "svn"]
def __init__(self):
# type: () -> None
def __init__(self) -> None:
# Register more schemes with urlparse for various version control
# systems
urllib.parse.uses_netloc.extend(self.schemes)
super().__init__()
def __iter__(self):
# type: () -> Iterator[str]
def __iter__(self) -> Iterator[str]:
return self._registry.__iter__()
@property
def backends(self):
# type: () -> List[VersionControl]
def backends(self) -> List["VersionControl"]:
return list(self._registry.values())
@property
def dirnames(self):
# type: () -> List[str]
def dirnames(self) -> List[str]:
return [backend.dirname for backend in self.backends]
@property
def all_schemes(self):
# type: () -> List[str]
schemes = [] # type: List[str]
def all_schemes(self) -> List[str]:
schemes: List[str] = []
for backend in self.backends:
schemes.extend(backend.schemes)
return schemes
def register(self, cls):
# type: (Type[VersionControl]) -> None
if not hasattr(cls, 'name'):
logger.warning('Cannot register VCS %s', cls.__name__)
def register(self, cls: Type["VersionControl"]) -> None:
if not hasattr(cls, "name"):
logger.warning("Cannot register VCS %s", cls.__name__)
return
if cls.name not in self._registry:
self._registry[cls.name] = cls()
logger.debug('Registered VCS backend: %s', cls.name)
logger.debug("Registered VCS backend: %s", cls.name)
def unregister(self, name):
# type: (str) -> None
def unregister(self, name: str) -> None:
if name in self._registry:
del self._registry[name]
def get_backend_for_dir(self, location):
# type: (str) -> Optional[VersionControl]
def get_backend_for_dir(self, location: str) -> Optional["VersionControl"]:
"""
Return a VersionControl object if a repository of that type is found
at the given directory.
@@ -238,8 +238,7 @@ class VcsSupport:
repo_path = vcs_backend.get_repository_root(location)
if not repo_path:
continue
logger.debug('Determine that %s uses VCS: %s',
location, vcs_backend.name)
logger.debug("Determine that %s uses VCS: %s", location, vcs_backend.name)
vcs_backends[repo_path] = vcs_backend
if not vcs_backends:
@@ -252,8 +251,7 @@ class VcsSupport:
inner_most_repo_path = max(vcs_backends, key=len)
return vcs_backends[inner_most_repo_path]
def get_backend_for_scheme(self, scheme):
# type: (str) -> Optional[VersionControl]
def get_backend_for_scheme(self, scheme: str) -> Optional["VersionControl"]:
"""
Return a VersionControl object or None.
"""
@@ -262,8 +260,7 @@ class VcsSupport:
return vcs_backend
return None
def get_backend(self, name):
# type: (str) -> Optional[VersionControl]
def get_backend(self, name: str) -> Optional["VersionControl"]:
"""
Return a VersionControl object or None.
"""
@@ -275,27 +272,25 @@ vcs = VcsSupport()
class VersionControl:
name = ''
dirname = ''
repo_name = ''
name = ""
dirname = ""
repo_name = ""
# List of supported schemes for this Version Control
schemes = () # type: Tuple[str, ...]
schemes: Tuple[str, ...] = ()
# Iterable of environment variable names to pass to call_subprocess().
unset_environ = () # type: Tuple[str, ...]
default_arg_rev = None # type: Optional[str]
unset_environ: Tuple[str, ...] = ()
default_arg_rev: Optional[str] = None
@classmethod
def should_add_vcs_url_prefix(cls, remote_url):
# type: (str) -> bool
def should_add_vcs_url_prefix(cls, remote_url: str) -> bool:
"""
Return whether the vcs prefix (e.g. "git+") should be added to a
repository's remote url when used in a requirement.
"""
return not remote_url.lower().startswith(f'{cls.name}:')
return not remote_url.lower().startswith(f"{cls.name}:")
@classmethod
def get_subdirectory(cls, location):
# type: (str) -> Optional[str]
def get_subdirectory(cls, location: str) -> Optional[str]:
"""
Return the path to Python project root, relative to the repo root.
Return None if the project root is in the repo root.
@@ -303,16 +298,14 @@ class VersionControl:
return None
@classmethod
def get_requirement_revision(cls, repo_dir):
# type: (str) -> str
def get_requirement_revision(cls, repo_dir: str) -> str:
"""
Return the revision string that should be used in a requirement.
"""
return cls.get_revision(repo_dir)
@classmethod
def get_src_requirement(cls, repo_dir, project_name):
# type: (str, str) -> str
def get_src_requirement(cls, repo_dir: str, project_name: str) -> str:
"""
Return the requirement string to use to redownload the files
currently at the given repository directory.
@@ -327,18 +320,16 @@ class VersionControl:
repo_url = cls.get_remote_url(repo_dir)
if cls.should_add_vcs_url_prefix(repo_url):
repo_url = f'{cls.name}+{repo_url}'
repo_url = f"{cls.name}+{repo_url}"
revision = cls.get_requirement_revision(repo_dir)
subdir = cls.get_subdirectory(repo_dir)
req = make_vcs_requirement_url(repo_url, revision, project_name,
subdir=subdir)
req = make_vcs_requirement_url(repo_url, revision, project_name, subdir=subdir)
return req
@staticmethod
def get_base_rev_args(rev):
# type: (str) -> List[str]
def get_base_rev_args(rev: str) -> List[str]:
"""
Return the base revision arguments for a vcs command.
@@ -347,8 +338,7 @@ class VersionControl:
"""
raise NotImplementedError
def is_immutable_rev_checkout(self, url, dest):
# type: (str, str) -> bool
def is_immutable_rev_checkout(self, url: str, dest: str) -> bool:
"""
Return true if the commit hash checked out at dest matches
the revision in url.
@@ -362,8 +352,9 @@ class VersionControl:
return False
@classmethod
def make_rev_options(cls, rev=None, extra_args=None):
# type: (Optional[str], Optional[CommandArgs]) -> RevOptions
def make_rev_options(
cls, rev: Optional[str] = None, extra_args: Optional[CommandArgs] = None
) -> RevOptions:
"""
Return a RevOptions object.
@@ -374,18 +365,18 @@ class VersionControl:
return RevOptions(cls, rev, extra_args=extra_args)
@classmethod
def _is_local_repository(cls, repo):
# type: (str) -> bool
def _is_local_repository(cls, repo: str) -> bool:
"""
posix absolute paths start with os.path.sep,
win32 ones start with drive (like c:\\folder)
posix absolute paths start with os.path.sep,
win32 ones start with drive (like c:\\folder)
"""
drive, tail = os.path.splitdrive(repo)
return repo.startswith(os.path.sep) or bool(drive)
@classmethod
def get_netloc_and_auth(cls, netloc, scheme):
# type: (str, str) -> Tuple[str, Tuple[Optional[str], Optional[str]]]
def get_netloc_and_auth(
cls, netloc: str, scheme: str
) -> Tuple[str, Tuple[Optional[str], Optional[str]]]:
"""
Parse the repository URL's netloc, and return the new netloc to use
along with auth information.
@@ -404,8 +395,7 @@ class VersionControl:
return netloc, (None, None)
@classmethod
def get_url_rev_and_auth(cls, url):
# type: (str) -> Tuple[str, Optional[str], AuthInfo]
def get_url_rev_and_auth(cls, url: str) -> Tuple[str, Optional[str], AuthInfo]:
"""
Parse the repository URL to use, and return the URL, revision,
and auth info to use.
@@ -413,44 +403,44 @@ class VersionControl:
Returns: (url, rev, (username, password)).
"""
scheme, netloc, path, query, frag = urllib.parse.urlsplit(url)
if '+' not in scheme:
if "+" not in scheme:
raise ValueError(
"Sorry, {!r} is a malformed VCS url. "
"The format is <vcs>+<protocol>://<url>, "
"e.g. svn+http://myrepo/svn/MyApp#egg=MyApp".format(url)
)
# Remove the vcs prefix.
scheme = scheme.split('+', 1)[1]
scheme = scheme.split("+", 1)[1]
netloc, user_pass = cls.get_netloc_and_auth(netloc, scheme)
rev = None
if '@' in path:
path, rev = path.rsplit('@', 1)
if "@" in path:
path, rev = path.rsplit("@", 1)
if not rev:
raise InstallationError(
"The URL {!r} has an empty revision (after @) "
"which is not supported. Include a revision after @ "
"or remove @ from the URL.".format(url)
)
url = urllib.parse.urlunsplit((scheme, netloc, path, query, ''))
url = urllib.parse.urlunsplit((scheme, netloc, path, query, ""))
return url, rev, user_pass
@staticmethod
def make_rev_args(username, password):
# type: (Optional[str], Optional[HiddenText]) -> CommandArgs
def make_rev_args(
username: Optional[str], password: Optional[HiddenText]
) -> CommandArgs:
"""
Return the RevOptions "extra arguments" to use in obtain().
"""
return []
def get_url_rev_options(self, url):
# type: (HiddenText) -> Tuple[HiddenText, RevOptions]
def get_url_rev_options(self, url: HiddenText) -> Tuple[HiddenText, RevOptions]:
"""
Return the URL and RevOptions object to use in obtain(),
as a tuple (url, rev_options).
"""
secret_url, rev, user_pass = self.get_url_rev_and_auth(url.secret)
username, secret_password = user_pass
password = None # type: Optional[HiddenText]
password: Optional[HiddenText] = None
if secret_password is not None:
password = hide_value(secret_password)
extra_args = self.make_rev_args(username, password)
@@ -459,24 +449,23 @@ class VersionControl:
return hide_url(secret_url), rev_options
@staticmethod
def normalize_url(url):
# type: (str) -> str
def normalize_url(url: str) -> str:
"""
Normalize a URL for comparison by unquoting it and removing any
trailing slash.
"""
return urllib.parse.unquote(url).rstrip('/')
return urllib.parse.unquote(url).rstrip("/")
@classmethod
def compare_urls(cls, url1, url2):
# type: (str, str) -> bool
def compare_urls(cls, url1: str, url2: str) -> bool:
"""
Compare two repo URLs for identity, ignoring incidental differences.
"""
return (cls.normalize_url(url1) == cls.normalize_url(url2))
return cls.normalize_url(url1) == cls.normalize_url(url2)
def fetch_new(self, dest, url, rev_options):
# type: (str, HiddenText, RevOptions) -> None
def fetch_new(
self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int
) -> None:
"""
Fetch a revision from a repository, in the case that this is the
first fetch from the repository.
@@ -484,11 +473,11 @@ class VersionControl:
Args:
dest: the directory to fetch the repository to.
rev_options: a RevOptions object.
verbosity: verbosity level.
"""
raise NotImplementedError
def switch(self, dest, url, rev_options):
# type: (str, HiddenText, RevOptions) -> None
def switch(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
"""
Switch the repo at ``dest`` to point to ``URL``.
@@ -497,8 +486,7 @@ class VersionControl:
"""
raise NotImplementedError
def update(self, dest, url, rev_options):
# type: (str, HiddenText, RevOptions) -> None
def update(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
"""
Update an already-existing repo to the given ``rev_options``.
@@ -508,8 +496,7 @@ class VersionControl:
raise NotImplementedError
@classmethod
def is_commit_id_equal(cls, dest, name):
# type: (str, Optional[str]) -> bool
def is_commit_id_equal(cls, dest: str, name: Optional[str]) -> bool:
"""
Return whether the id of the current commit equals the given name.
@@ -519,19 +506,19 @@ class VersionControl:
"""
raise NotImplementedError
def obtain(self, dest, url):
# type: (str, HiddenText) -> None
def obtain(self, dest: str, url: HiddenText, verbosity: int) -> None:
"""
Install or update in editable mode the package represented by this
VersionControl object.
:param dest: the repository directory in which to install or update.
:param url: the repository URL starting with a vcs prefix.
:param verbosity: verbosity level.
"""
url, rev_options = self.get_url_rev_options(url)
if not os.path.exists(dest):
self.fetch_new(dest, url, rev_options)
self.fetch_new(dest, url, rev_options, verbosity=verbosity)
return
rev_display = rev_options.to_display()
@@ -539,73 +526,68 @@ class VersionControl:
existing_url = self.get_remote_url(dest)
if self.compare_urls(existing_url, url.secret):
logger.debug(
'%s in %s exists, and has correct URL (%s)',
"%s in %s exists, and has correct URL (%s)",
self.repo_name.title(),
display_path(dest),
url,
)
if not self.is_commit_id_equal(dest, rev_options.rev):
logger.info(
'Updating %s %s%s',
"Updating %s %s%s",
display_path(dest),
self.repo_name,
rev_display,
)
self.update(dest, url, rev_options)
else:
logger.info('Skipping because already up-to-date.')
logger.info("Skipping because already up-to-date.")
return
logger.warning(
'%s %s in %s exists with URL %s',
"%s %s in %s exists with URL %s",
self.name,
self.repo_name,
display_path(dest),
existing_url,
)
prompt = ('(s)witch, (i)gnore, (w)ipe, (b)ackup ',
('s', 'i', 'w', 'b'))
prompt = ("(s)witch, (i)gnore, (w)ipe, (b)ackup ", ("s", "i", "w", "b"))
else:
logger.warning(
'Directory %s already exists, and is not a %s %s.',
"Directory %s already exists, and is not a %s %s.",
dest,
self.name,
self.repo_name,
)
# https://github.com/python/mypy/issues/1174
prompt = ('(i)gnore, (w)ipe, (b)ackup ', # type: ignore
('i', 'w', 'b'))
prompt = ("(i)gnore, (w)ipe, (b)ackup ", ("i", "w", "b")) # type: ignore
logger.warning(
'The plan is to install the %s repository %s',
"The plan is to install the %s repository %s",
self.name,
url,
)
response = ask_path_exists('What to do? {}'.format(
prompt[0]), prompt[1])
response = ask_path_exists("What to do? {}".format(prompt[0]), prompt[1])
if response == 'a':
if response == "a":
sys.exit(-1)
if response == 'w':
logger.warning('Deleting %s', display_path(dest))
if response == "w":
logger.warning("Deleting %s", display_path(dest))
rmtree(dest)
self.fetch_new(dest, url, rev_options)
self.fetch_new(dest, url, rev_options, verbosity=verbosity)
return
if response == 'b':
if response == "b":
dest_dir = backup_dir(dest)
logger.warning(
'Backing up %s to %s', display_path(dest), dest_dir,
)
logger.warning("Backing up %s to %s", display_path(dest), dest_dir)
shutil.move(dest, dest_dir)
self.fetch_new(dest, url, rev_options)
self.fetch_new(dest, url, rev_options, verbosity=verbosity)
return
# Do nothing if the response is "i".
if response == 's':
if response == "s":
logger.info(
'Switching %s %s to %s%s',
"Switching %s %s to %s%s",
self.repo_name,
display_path(dest),
url,
@@ -613,21 +595,20 @@ class VersionControl:
)
self.switch(dest, url, rev_options)
def unpack(self, location, url):
# type: (str, HiddenText) -> None
def unpack(self, location: str, url: HiddenText, verbosity: int) -> None:
"""
Clean up current location and download the url repository
(and vcs infos) into location
:param url: the repository URL starting with a vcs prefix.
:param verbosity: verbosity level.
"""
if os.path.exists(location):
rmtree(location)
self.obtain(location, url=url)
self.obtain(location, url=url, verbosity=verbosity)
@classmethod
def get_remote_url(cls, location):
# type: (str) -> str
def get_remote_url(cls, location: str) -> str:
"""
Return the url used at location
@@ -637,8 +618,7 @@ class VersionControl:
raise NotImplementedError
@classmethod
def get_revision(cls, location):
# type: (str) -> str
def get_revision(cls, location: str) -> str:
"""
Return the current commit id of the files at the given location.
"""
@@ -647,40 +627,46 @@ class VersionControl:
@classmethod
def run_command(
cls,
cmd, # type: Union[List[str], CommandArgs]
show_stdout=True, # type: bool
cwd=None, # type: Optional[str]
on_returncode='raise', # type: str
extra_ok_returncodes=None, # type: Optional[Iterable[int]]
command_desc=None, # type: Optional[str]
extra_environ=None, # type: Optional[Mapping[str, Any]]
spinner=None, # type: Optional[SpinnerInterface]
log_failed_cmd=True, # type: bool
stdout_only=False, # type: bool
):
# type: (...) -> str
cmd: Union[List[str], CommandArgs],
show_stdout: bool = True,
cwd: Optional[str] = None,
on_returncode: 'Literal["raise", "warn", "ignore"]' = "raise",
extra_ok_returncodes: Optional[Iterable[int]] = None,
command_desc: Optional[str] = None,
extra_environ: Optional[Mapping[str, Any]] = None,
spinner: Optional[SpinnerInterface] = None,
log_failed_cmd: bool = True,
stdout_only: bool = False,
) -> str:
"""
Run a VCS subcommand
This is simply a wrapper around call_subprocess that adds the VCS
command name, and checks that the VCS is available
"""
cmd = make_command(cls.name, *cmd)
if command_desc is None:
command_desc = format_command_args(cmd)
try:
return call_subprocess(cmd, show_stdout, cwd,
on_returncode=on_returncode,
extra_ok_returncodes=extra_ok_returncodes,
command_desc=command_desc,
extra_environ=extra_environ,
unset_environ=cls.unset_environ,
spinner=spinner,
log_failed_cmd=log_failed_cmd,
stdout_only=stdout_only)
return call_subprocess(
cmd,
show_stdout,
cwd,
on_returncode=on_returncode,
extra_ok_returncodes=extra_ok_returncodes,
command_desc=command_desc,
extra_environ=extra_environ,
unset_environ=cls.unset_environ,
spinner=spinner,
log_failed_cmd=log_failed_cmd,
stdout_only=stdout_only,
)
except FileNotFoundError:
# errno.ENOENT = no such file or directory
# In other words, the VCS executable isn't available
raise BadCommand(
f'Cannot find command {cls.name!r} - do you have '
f'{cls.name!r} installed and in your PATH?')
f"Cannot find command {cls.name!r} - do you have "
f"{cls.name!r} installed and in your PATH?"
)
except PermissionError:
# errno.EACCES = Permission denied
# This error occurs, for instance, when the command is installed
@@ -695,18 +681,15 @@ class VersionControl:
)
@classmethod
def is_repository_directory(cls, path):
# type: (str) -> bool
def is_repository_directory(cls, path: str) -> bool:
"""
Return whether a directory path is a repository directory.
"""
logger.debug('Checking in %s for %s (%s)...',
path, cls.dirname, cls.name)
logger.debug("Checking in %s for %s (%s)...", path, cls.dirname, cls.name)
return os.path.exists(os.path.join(path, cls.dirname))
@classmethod
def get_repository_root(cls, location):
# type: (str) -> Optional[str]
def get_repository_root(cls, location: str) -> Optional[str]:
"""
Return the "root" (top-level) directory controlled by the vcs,
or `None` if the directory is not in any.