__init__.py 9.4 KB

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