__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import os
  2. import os.path
  3. import pkgutil
  4. import sys
  5. import runpy
  6. import tempfile
  7. __all__ = ["version", "bootstrap"]
  8. _PACKAGE_NAMES = ('setuptools', 'pip')
  9. _SETUPTOOLS_VERSION = "47.1.0"
  10. _PIP_VERSION = "22.0.4"
  11. _PROJECTS = [
  12. ("setuptools", _SETUPTOOLS_VERSION, "py3"),
  13. ("pip", _PIP_VERSION, "py3"),
  14. ]
  15. def _run_pip(args, additional_paths=None):
  16. # Add our bundled software to the sys.path so we can import it
  17. if additional_paths is not None:
  18. sys.path = additional_paths + sys.path
  19. # Invoke pip as if it's the main module, and catch the exit.
  20. backup_argv = sys.argv[:]
  21. sys.argv[1:] = args
  22. try:
  23. # run_module() alters sys.modules and sys.argv, but restores them at exit
  24. runpy.run_module("pip", run_name="__main__", alter_sys=True)
  25. except SystemExit as exc:
  26. return exc.code
  27. finally:
  28. sys.argv[:] = backup_argv
  29. raise SystemError("pip did not exit, this should never happen")
  30. def version():
  31. """
  32. Returns a string specifying the bundled version of pip.
  33. """
  34. return _PIP_VERSION
  35. def _disable_pip_configuration_settings():
  36. # We deliberately ignore all pip environment variables
  37. # when invoking pip
  38. # See http://bugs.python.org/issue19734 for details
  39. keys_to_remove = [k for k in os.environ if k.startswith("PIP_")]
  40. for k in keys_to_remove:
  41. del os.environ[k]
  42. # We also ignore the settings in the default pip configuration file
  43. # See http://bugs.python.org/issue20053 for details
  44. os.environ['PIP_CONFIG_FILE'] = os.devnull
  45. def bootstrap(*, root=None, upgrade=False, user=False,
  46. altinstall=False, default_pip=False,
  47. verbosity=0):
  48. """
  49. Bootstrap pip into the current Python installation (or the given root
  50. directory).
  51. Note that calling this function will alter both sys.path and os.environ.
  52. """
  53. # Discard the return value
  54. _bootstrap(root=root, upgrade=upgrade, user=user,
  55. altinstall=altinstall, default_pip=default_pip,
  56. verbosity=verbosity)
  57. def _bootstrap(*, root=None, upgrade=False, user=False,
  58. altinstall=False, default_pip=False,
  59. verbosity=0):
  60. """
  61. Bootstrap pip into the current Python installation (or the given root
  62. directory). Returns pip command status code.
  63. Note that calling this function will alter both sys.path and os.environ.
  64. """
  65. if altinstall and default_pip:
  66. raise ValueError("Cannot use altinstall and default_pip together")
  67. _disable_pip_configuration_settings()
  68. # By default, installing pip and setuptools installs all of the
  69. # following scripts (X.Y == running Python version):
  70. #
  71. # pip, pipX, pipX.Y, easy_install, easy_install-X.Y
  72. #
  73. # pip 1.5+ allows ensurepip to request that some of those be left out
  74. if altinstall:
  75. # omit pip, pipX and easy_install
  76. os.environ["ENSUREPIP_OPTIONS"] = "altinstall"
  77. elif not default_pip:
  78. # omit pip and easy_install
  79. os.environ["ENSUREPIP_OPTIONS"] = "install"
  80. with tempfile.TemporaryDirectory() as tmpdir:
  81. # Put our bundled wheels into a temporary directory and construct the
  82. # additional paths that need added to sys.path
  83. additional_paths = []
  84. for project, version, py_tag in _PROJECTS:
  85. wheel_name = "{}-{}-{}-none-any.whl".format(project, version, py_tag)
  86. whl = pkgutil.get_data(
  87. "ensurepip",
  88. "_bundled/{}".format(wheel_name),
  89. )
  90. with open(os.path.join(tmpdir, wheel_name), "wb") as fp:
  91. fp.write(whl)
  92. additional_paths.append(os.path.join(tmpdir, wheel_name))
  93. # Construct the arguments to be passed to the pip command
  94. args = ["install", "--no-cache-dir", "--no-index", "--find-links", tmpdir]
  95. if root:
  96. args += ["--root", root]
  97. if upgrade:
  98. args += ["--upgrade"]
  99. if user:
  100. args += ["--user"]
  101. if verbosity:
  102. args += ["-" + "v" * verbosity]
  103. return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  104. def _uninstall_helper(*, verbosity=0):
  105. """Helper to support a clean default uninstall process on Windows
  106. Note that calling this function may alter os.environ.
  107. """
  108. # Nothing to do if pip was never installed, or has been removed
  109. try:
  110. import pip
  111. except ImportError:
  112. return
  113. # If the pip version doesn't match the bundled one, leave it alone
  114. if pip.__version__ != _PIP_VERSION:
  115. msg = ("ensurepip will only uninstall a matching version "
  116. "({!r} installed, {!r} bundled)")
  117. print(msg.format(pip.__version__, _PIP_VERSION), file=sys.stderr)
  118. return
  119. _disable_pip_configuration_settings()
  120. # Construct the arguments to be passed to the pip command
  121. args = ["uninstall", "-y", "--disable-pip-version-check"]
  122. if verbosity:
  123. args += ["-" + "v" * verbosity]
  124. return _run_pip(args + [p[0] for p in reversed(_PROJECTS)])
  125. def _main(argv=None):
  126. import argparse
  127. parser = argparse.ArgumentParser(prog="python -m ensurepip")
  128. parser.add_argument(
  129. "--version",
  130. action="version",
  131. version="pip {}".format(version()),
  132. help="Show the version of pip that is bundled with this Python.",
  133. )
  134. parser.add_argument(
  135. "-v", "--verbose",
  136. action="count",
  137. default=0,
  138. dest="verbosity",
  139. help=("Give more output. Option is additive, and can be used up to 3 "
  140. "times."),
  141. )
  142. parser.add_argument(
  143. "-U", "--upgrade",
  144. action="store_true",
  145. default=False,
  146. help="Upgrade pip and dependencies, even if already installed.",
  147. )
  148. parser.add_argument(
  149. "--user",
  150. action="store_true",
  151. default=False,
  152. help="Install using the user scheme.",
  153. )
  154. parser.add_argument(
  155. "--root",
  156. default=None,
  157. help="Install everything relative to this alternate root directory.",
  158. )
  159. parser.add_argument(
  160. "--altinstall",
  161. action="store_true",
  162. default=False,
  163. help=("Make an alternate install, installing only the X.Y versioned "
  164. "scripts (Default: pipX, pipX.Y, easy_install-X.Y)."),
  165. )
  166. parser.add_argument(
  167. "--default-pip",
  168. action="store_true",
  169. default=False,
  170. help=("Make a default pip install, installing the unqualified pip "
  171. "and easy_install in addition to the versioned scripts."),
  172. )
  173. args = parser.parse_args(argv)
  174. return _bootstrap(
  175. root=args.root,
  176. upgrade=args.upgrade,
  177. user=args.user,
  178. verbosity=args.verbosity,
  179. altinstall=args.altinstall,
  180. default_pip=args.default_pip,
  181. )