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