blob: 1a644da6349afec34996b1cd7c943a0fa1dd2ddd [file] [log] [blame]
Nick Coghland0cf0632013-11-11 22:11:55 +10001import os
2import os.path
3import pkgutil
4import sys
5import tempfile
6
7# TODO: Remove the --pre flag when a pip 1.5 final copy is available
8
9
10__all__ = ["version", "bootstrap"]
11
12
13_SETUPTOOLS_VERSION = "1.3.2"
14
Nick Coghlan1b1b1782013-11-30 15:56:58 +100015_PIP_VERSION = "1.5rc1"
Nick Coghland0cf0632013-11-11 22:11:55 +100016
17_PROJECTS = [
18 ("setuptools", _SETUPTOOLS_VERSION),
19 ("pip", _PIP_VERSION),
20]
21
22
23def _run_pip(args, additional_paths):
24 # Add our bundled software to the sys.path so we can import it
25 sys.path = additional_paths + sys.path
26
27 # Install the bundled software
28 import pip
29 pip.main(args)
30
31
32def version():
33 """
34 Returns a string specifying the bundled version of pip.
35 """
36 return _PIP_VERSION
37
38
39def bootstrap(*, root=None, upgrade=False, user=False,
40 altinstall=False, default_pip=False,
41 verbosity=0):
42 """
43 Bootstrap pip into the current Python installation (or the given root
44 directory).
45 """
46 if altinstall and default_pip:
47 raise ValueError("Cannot use altinstall and default_pip together")
48
49 # By default, installing pip and setuptools installs all of the
50 # following scripts (X.Y == running Python version):
51 #
52 # pip, pipX, pipX.Y, easy_install, easy_install-X.Y
53 #
54 # pip 1.5+ allows ensurepip to request that some of those be left out
55 if altinstall:
56 # omit pip, pipX and easy_install
57 os.environ["ENSUREPIP_OPTIONS"] = "altinstall"
58 elif not default_pip:
59 # omit pip and easy_install
60 os.environ["ENSUREPIP_OPTIONS"] = "install"
61
62 with tempfile.TemporaryDirectory() as tmpdir:
63 # Put our bundled wheels into a temporary directory and construct the
64 # additional paths that need added to sys.path
65 additional_paths = []
66 for project, version in _PROJECTS:
67 wheel_name = "{}-{}-py2.py3-none-any.whl".format(project, version)
68 whl = pkgutil.get_data(
69 "ensurepip",
70 "_bundled/{}".format(wheel_name),
71 )
72 with open(os.path.join(tmpdir, wheel_name), "wb") as fp:
73 fp.write(whl)
74
75 additional_paths.append(os.path.join(tmpdir, wheel_name))
76
77 # Construct the arguments to be passed to the pip command
78 args = [
79 "install", "--no-index", "--find-links", tmpdir,
80 # Temporary until pip 1.5 is final
81 "--pre",
82 ]
83 if root:
84 args += ["--root", root]
85 if upgrade:
86 args += ["--upgrade"]
87 if user:
88 args += ["--user"]
89 if verbosity:
90 args += ["-" + "v" * verbosity]
91
92 _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)