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