1 #ˆl–äY>”*e¶…6 Ap-Ümµ1hßÅ ƒ}ö\ÄÃÂ&Ðimport distutils.version import glob import collections import os import os.path import subprocess import sys import sysconfig import tempfile from importlib import resources __all__ = ["version", "bootstrap"] _PACKAGE_NAMES = ('setuptools', 'pip') # Packages bundled in ensurepip._bundled have wheel_name set. # Packages from WHEEL_PKG_DIR have wheel_path set. _Package = collections.namedtuple('Package', ('version', 'wheel_name', 'wheel_path')) # Directory of system wheel packages. Some Linux distribution packaging # policies recommend against bundling dependencies. For example, Fedora # installs wheel packages in the /usr/share/python-wheels/ directory and don't # install the ensurepip._bundled package. _WHEEL_PKG_DIR = "/opt/alt/python-internal/share/python-wheels/" _wheels = {} def _get_most_recent_wheel_version(pkg): prefix = os.path.join(_WHEEL_PKG_DIR, "{}-".format(pkg)) _wheels[pkg] = {} for suffix in "-py2.py3-none-any.whl", "-py3-none-any.whl": pattern = "{}*{}".format(prefix, suffix) for path in glob.glob(pattern): version_str = path[len(prefix):-len(suffix)] _wheels[pkg][version_str] = os.path.basename(path) return str(max(_wheels[pkg], key=distutils.version.LooseVersion)) _SETUPTOOLS_VERSION = _get_most_recent_wheel_version("setuptools") _PIP_VERSION = _get_most_recent_wheel_version("pip") _PROJECTS = [ ("setuptools", _SETUPTOOLS_VERSION, "py3"), ("pip", _PIP_VERSION, "py3"), ] def _find_packages(path): packages = {} try: filenames = os.listdir(path) except OSError: # Ignore: path doesn't exist or permission error filenames = () # Make the code deterministic if a directory contains multiple wheel files # of the same package, but don't attempt to implement correct version # comparison since this case should not happen. filenames = sorted(filenames) for filename in filenames: # filename is like 'pip-21.2.4-py3-none-any.whl' if not filename.endswith(".whl"): continue for name in _PACKAGE_NAMES: prefix = name + '-' if filename.startswith(prefix): break else: continue # Extract '21.2.4' from 'pip-21.2.4-py3-none-any.whl' version = filename.removeprefix(prefix).partition('-')[0] wheel_path = os.path.join(path, filename) packages[name] = _Package(version, None, wheel_path) return packages def _get_packages(): global _PACKAGES, _WHEEL_PKG_DIR if _PACKAGES is not None: return _PACKAGES packages = {} for name, version, py_tag in _PROJECTS: wheel_name = _wheels[name][version] packages[name] = _Package(version, wheel_name, None) if _WHEEL_PKG_DIR: dir_packages = _find_packages(_WHEEL_PKG_DIR) # only used the wheel package directory if all packages are found there if all(name in dir_packages for name in _PACKAGE_NAMES): packages = dir_packages _PACKAGES = packages return packages _PACKAGES = None def _run_pip(args, additional_paths=None): # Run the bootstrapping in a subprocess to avoid leaking any state that happens # after pip has executed. Particularly, this avoids the case when pip holds onto # the files in *additional_paths*, preventing us to remove them at the end of the # invocation. code = f""" import runpy import sys sys.path = {additional_paths or []} + sys.path sys.argv[1:] = {args} runpy.run_module("pip", run_name="__main__", alter_sys=True) """ cmd = [ sys.executable, '-W', 'ignore::DeprecationWarning', '-c', code, ] if sys.flags.isolated: # run code in isolated mode if currently running isolated cmd.insert(1, '-I') return subprocess.run(cmd, check=True).returncode def version(): """ Returns a string specifying the bundled version of pip. """ return _get_packages()