import logging
import os
import shutil
import sys
import urllib.parse
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
Type,
Union,
)
from pip._internal.cli.spinners import SpinnerInterface
from pip._internal.exceptions import BadCommand, InstallationError
from pip._internal.utils.misc import (
HiddenText,
ask_path_exists,
backup_dir,
display_path,
hide_url,
hide_value,
is_installable_dir,
rmtree,
)
from pip._internal.utils.subprocess import (
CommandArgs,
call_subprocess,
format_command_args,
make_command,
)
from pip._internal.utils.urls import get_url_scheme
if TYPE_CHECKING:
from typing import Literal
__all__ = ["vcs"]
logger = logging.getLogger(__name__)
AuthInfo = Tuple[Optional[str], Optional[str]]
def is_url(name: str) -> bool:
scheme = get_url_scheme(name)
if scheme is None:
return False
return scheme in ["http", "https", "file", "ftp"] + vcs.all_schemes
def make_vcs_requirement_url(
repo_url: str, rev: str, project_name: str, subdir: Optional[str] = None
) -> str:
egg_project_name = project_name.replace("-", "_")
req = f"{repo_url}@{rev}#egg={egg_project_name}"
if subdir:
req += f"&subdirectory={subdir}"
return req
def find_path_to_project_root_from_repo_root(
location: str, repo_root: str
) -> Optional[str]:
orig_location = location
while not is_installable_dir(location):
last_location = location
location = os.path.dirname(location)
if location == last_location:
logger.warning(
"Could not find a Python project for directory %s (tried all "
"parent directories)",
orig_location,
)
return None
if os.path.samefile(repo_root, location):
return None
return os.path.relpath(location, repo_root)
class RemoteNotFoundError(Exception):
pass
class RemoteNotValidError(Exception):
def __init__(self, url: str):
super().__init__(url)
self.url = url
class RevOptions:
def __init__(
self,
vc_class: Type["VersionControl"],
rev: Optional[str] = None,
extra_args: Optional[CommandArgs] = None,
) -> None:
if extra_args is None:
extra_args = []
self.extra_args = extra_args
self.rev = rev
self.vc_class = vc_class
self.branch_name: Optional[str] = None
def __repr__(self) -> str:
return f"<RevOptions {self.vc_class.name}: rev={self.rev!r}>"
@property
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) -> CommandArgs:
args: CommandArgs = []
rev = self.arg_rev
if rev is not None:
args += self.vc_class.get_base_rev_args(rev)
args += self.extra_args
return args
def to_display(self) -> str:
if not self.rev:
return ""
return f" (to revision {self.rev})"
def make_new(self, rev: str) -> "RevOptions":
return self.vc_class.make_rev_options(rev, extra_args=self.extra_args)
class VcsSupport:
_registry: Dict[str, "VersionControl"] = {}
schemes = ["ssh", "git", "hg", "bzr", "sftp", "svn"]
def __init__(self) -> None:
urllib.parse.uses_netloc.extend(self.schemes)
super().__init__()
def __iter__(self) -> Iterator[str]:
return self._registry.__iter__()
@property
def backends(self) -> List["VersionControl"]:
return list(self._registry.values())
@property
def dirnames(self) -> List[str]:
return [backend.dirname for backend in self.backends]
@property
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["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)
def unregister(self, name: str) -> None:
if name in self._registry:
del self._registry[name]
def get_backend_for_dir(self, location: str) -> Optional["VersionControl"]:
vcs_backends = {}
for vcs_backend in self._registry.values():
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)
vcs_backends[repo_path] = vcs_backend
if not vcs_backends:
return None
inner_most_repo_path = max(vcs_backends, key=len)
return vcs_backends[inner_most_repo_path]
def get_backend_for_scheme(self, scheme: str) -> Optional["VersionControl"]:
for vcs_backend in self._registry.values():
if scheme in vcs_backend.schemes:
return vcs_backend
return None
def get_backend(self, name: str) -> Optional["VersionControl"]:
name = name.lower()
return self._registry.get(name)
vcs = VcsSupport()
class VersionControl:
name = ""
dirname = ""
repo_name = ""
schemes: Tuple[str, ...] = ()
unset_environ: Tuple[str, ...] = ()
default_arg_rev: Optional[str] = None
@classmethod
def should_add_vcs_url_prefix(cls, remote_url: str) -> bool:
return not remote_url.lower().startswith(f"{cls.name}:")
@classmethod
def get_subdirectory(cls, location: str) -> Optional[str]:
return None
@classmethod
def get_requirement_revision(cls, repo_dir: str) -> str:
return cls.get_revision(repo_dir)
@classmethod
def get_src_requirement(cls, repo_dir: str, project_name: str) -> str:
repo_url = cls.get_remote_url(repo_dir)
if cls.should_add_vcs_url_prefix(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)
return req
@staticmethod
def get_base_rev_args(rev: str) -> List[str]:
raise NotImplementedError
def is_immutable_rev_checkout(self, url: str, dest: str) -> bool:
return False
@classmethod
def make_rev_options(
cls, rev: Optional[str] = None, extra_args: Optional[CommandArgs] = None
) -> RevOptions:
return RevOptions(cls, rev, extra_args=extra_args)
@classmethod
def _is_local_repository(cls, repo: str) -> bool:
drive, tail = os.path.splitdrive(repo)
return repo.startswith(os.path.sep) or bool(drive)
@classmethod
def get_netloc_and_auth(
cls, netloc: str, scheme: str
) -> Tuple[str, Tuple[Optional[str], Optional[str]]]:
return netloc, (None, None)
@classmethod
def get_url_rev_and_auth(cls, url: str) -> Tuple[str, Optional[str], AuthInfo]:
scheme, netloc, path, query, frag = urllib.parse.urlsplit(url)
if "+" not in scheme:
raise ValueError(
f"Sorry, {url!r} is a malformed VCS url. "
"The format is <vcs>+<protocol>://<url>, "
"e.g. svn+http://myrepo/svn/MyApp#egg=MyApp"
)
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 not rev:
raise InstallationError(
f"The URL {url!r} has an empty revision (after @) "
"which is not supported. Include a revision after @ "
"or remove @ from the URL."
)
url = urllib.parse.urlunsplit((scheme, netloc, path, query, ""))
return url, rev, user_pass
@staticmethod
def make_rev_args(
username: Optional[str], password: Optional[HiddenText]
) -> CommandArgs:
return []
def get_url_rev_options(self, url: HiddenText) -> Tuple[HiddenText, RevOptions]:
secret_url, rev, user_pass = self.get_url_rev_and_auth(url.secret)
username, secret_password = user_pass
password: Optional[HiddenText] = None
if secret_password is not None:
password = hide_value(secret_password)
extra_args = self.make_rev_args(username, password)
rev_options = self.make_rev_options(rev, extra_args=extra_args)
return hide_url(secret_url), rev_options
@staticmethod
def normalize_url(url: str) -> str:
return urllib.parse.unquote(url).rstrip("/")
@classmethod
def compare_urls(cls, url1: str, url2: str) -> bool:
return cls.normalize_url(url1) == cls.normalize_url(url2)
def fetch_new(
self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int
) -> None:
raise NotImplementedError
def switch(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
raise NotImplementedError
def update(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
raise NotImplementedError
@classmethod
def is_commit_id_equal(cls, dest: str, name: Optional[str]) -> bool:
raise NotImplementedError
def obtain(self, dest: str, url: HiddenText, verbosity: int) -> None:
url, rev_options = self.get_url_rev_options(url)
if not os.path.exists(dest):
self.fetch_new(dest, url, rev_options, verbosity=verbosity)
return
rev_display = rev_options.to_display()
if self.is_repository_directory(dest):
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)",
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",
display_path(dest),
self.repo_name,
rev_display,
)
self.update(dest, url, rev_options)
else:
logger.info("Skipping because already up-to-date.")
return
logger.warning(
"%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"))
else:
logger.warning(
"Directory %s already exists, and is not a %s %s.",
dest,
self.name,
self.repo_name,
)
prompt = ("(i)gnore, (w)ipe, (b)ackup ", ("i", "w", "b"))
logger.warning(
"The plan is to install the %s repository %s",
self.name,
url,
)
response = ask_path_exists(f"What to do? {prompt[0]}", prompt[1])
if response == "a":
sys.exit(-1)
if response == "w":
logger.warning("Deleting %s", display_path(dest))
rmtree(dest)
self.fetch_new(dest, url, rev_options, verbosity=verbosity)
return
if response == "b":
dest_dir = backup_dir(dest)
logger.warning("Backing up %s to %s", display_path(dest), dest_dir)
shutil.move(dest, dest_dir)
self.fetch_new(dest, url, rev_options, verbosity=verbosity)
return
if response == "s":
logger.info(
"Switching %s %s to %s%s",
self.repo_name,
display_path(dest),
url,
rev_display,
)
self.switch(dest, url, rev_options)
def unpack(self, location: str, url: HiddenText, verbosity: int) -> None:
if os.path.exists(location):
rmtree(location)
self.obtain(location, url=url, verbosity=verbosity)
@classmethod
def get_remote_url(cls, location: str) -> str:
raise NotImplementedError
@classmethod
def get_revision(cls, location: str) -> str:
raise NotImplementedError
@classmethod
def run_command(
cls,
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:
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,
)
except FileNotFoundError:
raise BadCommand(
f"Cannot find command {cls.name!r} - do you have "
f"{cls.name!r} installed and in your PATH?"
)
except PermissionError:
raise BadCommand(
f"No permission to execute {cls.name!r} - install it "
f"locally, globally (ask admin), or check your PATH. "
f"See possible solutions at "
f"https://pip.pypa.io/en/latest/reference/pip_freeze/"
f"#fixing-permission-denied."
)
@classmethod
def is_repository_directory(cls, path: str) -> bool:
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: str) -> Optional[str]:
if cls.is_repository_directory(location):
return location
return None