ยปCore Development>Code coverage>Lib/ensurepip/__init__.py

Python code coverage for Lib/ensurepip/__init__.py

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