codekingpro/portable-devtools
114k
1"""Execute exactly this copy of pip, within a different environment.2 3This file is named as it is, to ensure that this module can't be imported via4an import statement.5"""6 7# /!\ This version compatibility check section must be Python 2 compatible. /!\8 9import sys10 11# Copied from setup.py12PYTHON_REQUIRES = (3, 7)13 14 15def version_str(version): # type: ignore16 return ".".join(str(v) for v in version)17 18 19if sys.version_info[:2] < PYTHON_REQUIRES:20 raise SystemExit(21 "This version of pip does not support python {} (requires >={}).".format(22 version_str(sys.version_info[:2]), version_str(PYTHON_REQUIRES)23 )24 )25 26# From here on, we can use Python 3 features, but the syntax must remain27# Python 2 compatible.28 29import runpy # noqa: E40230from importlib.machinery import PathFinder # noqa: E40231from os.path import dirname # noqa: E40232 33PIP_SOURCES_ROOT = dirname(dirname(__file__))34 35 36class PipImportRedirectingFinder:37 @classmethod38 def find_spec(self, fullname, path=None, target=None): # type: ignore39 if fullname != "pip":40 return None41 42 spec = PathFinder.find_spec(fullname, [PIP_SOURCES_ROOT], target)43 assert spec, (PIP_SOURCES_ROOT, fullname)44 return spec45 46 47sys.meta_path.insert(0, PipImportRedirectingFinder())48 49assert __name__ == "__main__", "Cannot run __pip-runner__.py as a non-main module"50runpy.run_module("pip", run_name="__main__", alter_sys=True)51 