__pip-runner__.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Execute exactly this copy of pip, within a different environment.
  2. This file is named as it is, to ensure that this module can't be imported via
  3. an import statement.
  4. """
  5. # /!\ This version compatibility check section must be Python 2 compatible. /!\
  6. import sys
  7. # Copied from setup.py
  8. PYTHON_REQUIRES = (3, 7)
  9. def version_str(version): # type: ignore
  10. return ".".join(str(v) for v in version)
  11. if sys.version_info[:2] < PYTHON_REQUIRES:
  12. raise SystemExit(
  13. "This version of pip does not support python {} (requires >={}).".format(
  14. version_str(sys.version_info[:2]), version_str(PYTHON_REQUIRES)
  15. )
  16. )
  17. # From here on, we can use Python 3 features, but the syntax must remain
  18. # Python 2 compatible.
  19. import runpy # noqa: E402
  20. from importlib.machinery import PathFinder # noqa: E402
  21. from os.path import dirname # noqa: E402
  22. PIP_SOURCES_ROOT = dirname(dirname(__file__))
  23. class PipImportRedirectingFinder:
  24. @classmethod
  25. def find_spec(self, fullname, path=None, target=None): # type: ignore
  26. if fullname != "pip":
  27. return None
  28. spec = PathFinder.find_spec(fullname, [PIP_SOURCES_ROOT], target)
  29. assert spec, (PIP_SOURCES_ROOT, fullname)
  30. return spec
  31. sys.meta_path.insert(0, PipImportRedirectingFinder())
  32. assert __name__ == "__main__", "Cannot run __pip-runner__.py as a non-main module"
  33. runpy.run_module("pip", run_name="__main__", alter_sys=True)