def install_package(specifier, prefix): """ Install a pip package (without dependencies) into the prefix directory. """ # TODO(vip): If this command is run as a different user, we can prevent models # from modifying the installed packages (can also do things like read-only mounts, etc.) from pip._internal.cli.main import main as pip_entry_point exit_code = pip_entry_point( [ "-q", "install", "--compile", "--no-deps", "--disable-pip-version-check", "--no-warn-script-location", "--prefix={}".format(prefix), "--ignore-installed", specifier, ] ) if exit_code != 0: raise RuntimeError("Error installing python dependency: {}".format(specifier))
def bootstrap(tmpdir): monkeypatch_for_cert(tmpdir) # Execute the included pip and use it to install the latest pip and # setuptools from PyPI from pip._internal.cli.main import main as pip_entry_point args = determine_pip_install_arguments() sys.exit(pip_entry_point(args))
def pip(self, *args: Union[str, Path]) -> InMemoryPipResult: orig_stdout = sys.stdout stdout = io.StringIO() sys.stdout = stdout try: returncode = pip_entry_point([os.fspath(a) for a in args]) except SystemExit as e: returncode = e.code or 0 finally: sys.stdout = orig_stdout return InMemoryPipResult(returncode, stdout.getvalue())
def pip(self, *args): orig_stdout = sys.stdout stdout = io.StringIO() sys.stdout = stdout try: returncode = pip_entry_point(list(args)) except SystemExit as e: returncode = e.code or 0 finally: sys.stdout = orig_stdout return InMemoryPipResult(returncode, stdout.getvalue())
def bootstrap(tmpdir=None): # Import pip so we can use it to install pip and maybe setuptools too from pip._internal.cli.main import main as pip_entry_point from pip._internal.commands.install import InstallCommand from pip._internal.req.constructors import install_req_from_line # Wrapper to provide default certificate with the lowest priority # Due to pip._internal.commands.commands_dict structure, a monkeypatch # seems the simplest workaround. install_parse_args = InstallCommand.parse_args def cert_parse_args(self, args): # If cert isn't specified in config or environment, we provide our # own certificate through defaults. # This allows user to specify custom cert anywhere one likes: # config, environment variable or argv. if not self.parser.get_default_values().cert: self.parser.defaults["cert"] = cert_path # calculated below return install_parse_args(self, args) InstallCommand.parse_args = cert_parse_args implicit_pip = True implicit_setuptools = True implicit_wheel = True # Check if the user has requested us not to install setuptools if "--no-setuptools" in sys.argv or os.environ.get("PIP_NO_SETUPTOOLS"): args = [x for x in sys.argv[1:] if x != "--no-setuptools"] implicit_setuptools = False else: args = sys.argv[1:] # Check if the user has requested us not to install wheel if "--no-wheel" in args or os.environ.get("PIP_NO_WHEEL"): args = [x for x in args if x != "--no-wheel"] implicit_wheel = False # We only want to implicitly install setuptools and wheel if they don't # already exist on the target platform. if implicit_setuptools: try: import setuptools # noqa implicit_setuptools = False except ImportError: pass if implicit_wheel: try: import wheel # noqa implicit_wheel = False except ImportError: pass # We want to support people passing things like 'pip<8' to get-pip.py which # will let them install a specific version. However because of the dreaded # DoubleRequirement error if any of the args look like they might be a # specific for one of our packages, then we'll turn off the implicit # install of them. for arg in args: try: req = install_req_from_line(arg) except Exception: continue if implicit_pip and req.name == "pip": implicit_pip = False elif implicit_setuptools and req.name == "setuptools": implicit_setuptools = False elif implicit_wheel and req.name == "wheel": implicit_wheel = False # Add any implicit installations to the end of our args if implicit_pip: args += ["pip"] if implicit_setuptools: args += ["setuptools"] if implicit_wheel: args += ["wheel"] # Add our default arguments args = ["install", "--upgrade", "--force-reinstall"] + args delete_tmpdir = False try: # Create a temporary directory to act as a working directory if we were # not given one. if tmpdir is None: tmpdir = tempfile.mkdtemp() delete_tmpdir = True # We need to extract the SSL certificates from requests so that they # can be passed to --cert cert_path = os.path.join(tmpdir, "cacert.pem") with open(cert_path, "wb") as cert: cert.write(pkgutil.get_data("pip._vendor.certifi", "cacert.pem")) # Execute the included pip and use it to install the latest pip and # setuptools from PyPI sys.exit(pip_entry_point(args)) finally: # Remove our temporary directory if delete_tmpdir and tmpdir: shutil.rmtree(tmpdir, ignore_errors=True)