__init__.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import collections
  2. import os
  3. import os.path
  4. import subprocess
  5. import sys
  6. import sysconfig
  7. import tempfile
  8. from importlib import resources
  9. __all__ = ["version", "bootstrap"]
  10. _PACKAGE_NAMES = ('pip',)
  11. _PIP_VERSION = "23.2.1"
  12. _PROJECTS = [
  13. ("pip", _PIP_VERSION, "py3"),
  14. ]
  15. # Packages bundled in ensurepip._bundled have wheel_name set.
  16. # Packages from WHEEL_PKG_DIR have wheel_path set.
  17. _Package = collections.namedtuple('Package',
  18. ('version', 'wheel_name', 'wheel_path'))
  19. # Directory of system wheel packages. Some Linux distribution packaging
  20. # policies recommend against bundling dependencies. For example, Fedora
  21. # installs wheel packages in the /usr/share/python-wheels/ directory and don't
  22. # install the ensurepip._bundled package.
  23. _WHEEL_PKG_DIR = sysconfig.get_config_var('WHEEL_PKG_DIR')
  24. def _find_packages(path):
  25. packages = {}
  26. try:
  27. filenames = os.listdir(path)
  28. except OSError:
  29. # Ignore: path doesn't exist or permission error
  30. filenames = ()
  31. # Make the code deterministic if a directory contains multiple wheel files
  32. # of the same package, but don't attempt to implement correct version
  33. # comparison since this case should not happen.
  34. filenames = sorted(filenames)
  35. for filename in filenames:
  36. # filename is like 'pip-21.2.4-py3-none-any.whl'
  37. if not filename.endswith(".whl"):
  38. continue
  39. for name in _PACKAGE_NAMES:
  40. prefix = name + '-'
  41. if filename.startswith(prefix):
  42. break
  43. else:
  44. continue
  45. # Extract '21.2.4' from 'pip-21.2.4-py3-none-any.whl'
  46. version = filename.removeprefix(prefix).partition('-')[0]
  47. wheel_path = os.path.join(path, filename)
  48. packages[name] = _Package(version, None, wheel_path)
  49. return packages
  50. def _get_packages():
  51. global _PACKAGES, _WHEEL_PKG_DIR
  52. if _PACKAGES is not None:
  53. return _PACKAGES
  54. packages = {}
  55. for name, version, py_tag in _PROJECTS:
  56. wheel_name = f"{name}-{version}-{py_tag}-none-any.whl"
  57. packages[name] = _Package(version, wheel_name, None)
  58. if _WHEEL_PKG_DIR:
  59. dir_packages = _find_packages(_WHEEL_PKG_DIR)
  60. # only used the wheel package directory if all packages are found there
  61. if all(name in dir_packages for name in _PACKAGE_NAMES):
  62. packages = dir_packages
  63. _PACKAGES = packages
  64. return packages
  65. _PACKAGES = None
  66. def _run_pip(args, additional_paths=None):
  67. # Run the bootstrapping in a subprocess to avoid leaking any state that happens
  68. # after pip has executed. Particularly, this avoids the case when pip holds onto
  69. # the files in *additional_paths*, preventing us to remove them at the end of the
  70. # invocation.
  71. code = f"""
  72. import runpy
  73. import sys
  74. sys.path = {additional_paths or []} + sys.path
  75. sys.argv[1:] = {args}
  76. runpy.run_module("pip", run_name="__main__", alter_sys=True)
  77. """
  78. cmd = [
  79. sys.executable,
  80. '-W',
  81. 'ignore::DeprecationWarning',
  82. '-c',
  83. code,
  84. ]
  85. if sys.flags.isolated:
  86. # run code in isolated mode if currently running isolated
  87. cmd.insert(1, '-I')
  88. return subprocess.run(cmd, check=True).returncode
  89. def version():
  90. """
  91. Returns a string specifying the bundled version of pip.
  92. """
  93. return _get_packages()['pip'].version
  94. def _disable_pip_configuration_settings():
  95. # We deliberately ignore all pip environment variables
  96. # when invoking pip
  97. # See http://bugs.python.org/issue19734 for details
  98. keys_to_remove = [k for k in os.environ if k.startswith("PIP_")]
  99. for k in keys_to_remove:
  100. del os.environ[k]
  101. # We also ignore the settings in the default pip configuration file
  102. # See http://bugs.python.org/issue20053 for details
  103. os.environ['PIP_CONFIG_FILE'] = os.devnull
  104. def bootstrap(*, root=None, upgrade=False, user=False,
  105. altinstall=False, default_pip=False,
  106. verbosity=0):
  107. """
  108. Bootstrap pip into the current Python installation (or the given root
  109. directory).
  110. Note that calling this function will alter both sys.path and os.environ.
  111. """
  112. # Discard the return value
  113. _bootstrap(root=root, upgrade=upgrade, user=user,
  114. altinstall=altinstall, default_pip=default_pip,
  115. verbosity=verbosity)
  116. def _bootstrap(*, root=None, upgrade=False, user=False,
  117. altinstall=False, default_pip=False,
  118. verbosity=0):
  119. """
  120. Bootstrap pip into the current Python installation (or the given root
  121. directory). Returns pip command status code.
  122. Note that calling this function will alter both sys.path and os.environ.
  123. """
  124. if altinstall and default_pip:
  125. raise ValueError("Cannot use altinstall and default_pip together")
  126. sys.audit("ensurepip.bootstrap", root)
  127. _disable_pip_configuration_settings()
  128. # By default, installing pip installs all of the
  129. # following scripts (X.Y == running Python version):
  130. #
  131. # pip, pipX, pipX.Y
  132. #
  133. # pip 1.5+ allows ensurepip to request that some of those be left out
  134. if altinstall:
  135. # omit pip, pipX
  136. os.environ["ENSUREPIP_OPTIONS"] = "altinstall"
  137. elif not default_pip:
  138. # omit pip
  139. os.environ["ENSUREPIP_OPTIONS"] = "install"
  140. with tempfile.TemporaryDirectory() as tmpdir:
  141. # Put our bundled wheels into a temporary directory and construct the
  142. # additional paths that need added to sys.path
  143. additional_paths = []
  144. for name, package in _get_packages().items():
  145. if package.wheel_name:
  146. # Use bundled wheel package
  147. wheel_name = package.wheel_name
  148. wheel_path = resources.files("ensurepip") / "_bundled" / wheel_name
  149. whl = wheel_path.read_bytes()
  150. else:
  151. # Use the wheel package directory
  152. with open(package.wheel_path, "rb") as fp:
  153. whl = fp.read()
  154. wheel_name = os.path.basename(package.wheel_path)
  155. filename = os.path.join(tmpdir, wheel_name)
  156. with open(filename, "wb") as fp:
  157. fp.write(whl)
  158. additional_paths.append(filename)
  159. # Construct the arguments to be passed to the pip command
  160. args = ["install", "--no-cache-dir", "--no-index", "--find-links", tmpdir]
  161. if root:
  162. args += ["--root", root]
  163. if upgrade:
  164. args += ["--upgrade"]
  165. if user:
  166. args += ["--user"]
  167. if verbosity:
  168. args += ["-" + "v" * verbosity]
  169. return _run_pip([*args, *_PACKAGE_NAMES], additional_paths)
  170. def _uninstall_helper(*, verbosity=0):
  171. """Helper to support a clean default uninstall process on Windows
  172. Note that calling this function may alter os.environ.
  173. """
  174. # Nothing to do if pip was never installed, or has been removed
  175. try:
  176. import pip
  177. except ImportError:
  178. return
  179. # If the installed pip version doesn't match the available one,
  180. # leave it alone
  181. available_version = version()
  182. if pip.__version__ != available_version:
  183. print(f"ensurepip will only uninstall a matching version "
  184. f"({pip.__version__!r} installed, "
  185. f"{available_version!r} available)",
  186. file=sys.stderr)
  187. return
  188. _disable_pip_configuration_settings()
  189. # Construct the arguments to be passed to the pip command
  190. args = ["uninstall", "-y", "--disable-pip-version-check"]
  191. if verbosity:
  192. args += ["-" + "v" * verbosity]
  193. return _run_pip([*args, *reversed(_PACKAGE_NAMES)])
  194. def _main(argv=None):
  195. import argparse
  196. parser = argparse.ArgumentParser(prog="python -m ensurepip")
  197. parser.add_argument(
  198. "--version",
  199. action="version",
  200. version="pip {}".format(version()),
  201. help="Show the version of pip that is bundled with this Python.",
  202. )
  203. parser.add_argument(
  204. "-v", "--verbose",
  205. action="count",
  206. default=0,
  207. dest="verbosity",
  208. help=("Give more output. Option is additive, and can be used up to 3 "
  209. "times."),
  210. )
  211. parser.add_argument(
  212. "-U", "--upgrade",
  213. action="store_true",
  214. default=False,
  215. help="Upgrade pip and dependencies, even if already installed.",
  216. )
  217. parser.add_argument(
  218. "--user",
  219. action="store_true",
  220. default=False,
  221. help="Install using the user scheme.",
  222. )
  223. parser.add_argument(
  224. "--root",
  225. default=None,
  226. help="Install everything relative to this alternate root directory.",
  227. )
  228. parser.add_argument(
  229. "--altinstall",
  230. action="store_true",
  231. default=False,
  232. help=("Make an alternate install, installing only the X.Y versioned "
  233. "scripts (Default: pipX, pipX.Y)."),
  234. )
  235. parser.add_argument(
  236. "--default-pip",
  237. action="store_true",
  238. default=False,
  239. help=("Make a default pip install, installing the unqualified pip "
  240. "in addition to the versioned scripts."),
  241. )
  242. args = parser.parse_args(argv)
  243. return _bootstrap(
  244. root=args.root,
  245. upgrade=args.upgrade,
  246. user=args.user,
  247. verbosity=args.verbosity,
  248. altinstall=args.altinstall,
  249. default_pip=args.default_pip,
  250. )