__init__.py 6.5 KB

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