bpo-31046: ensurepip does not honour the value of $(prefix)

Co-Authored-By: Xavier de Gaye <xdegaye@gmail.com>
This commit is contained in:
Zackery Spytz 2019-12-16 18:24:08 -07:00
parent f501db2b93
commit 5754521af1
5 changed files with 34 additions and 9 deletions

View File

@ -56,8 +56,9 @@ is at least as recent as the one bundled with ``ensurepip``, pass the
By default, ``pip`` is installed into the current virtual environment By default, ``pip`` is installed into the current virtual environment
(if one is active) or into the system site packages (if there is no (if one is active) or into the system site packages (if there is no
active virtual environment). The installation location can be controlled active virtual environment). The installation location can be controlled
through two additional command line options: through some additional command line options:
* ``--prefix <dir>``: Installs ``pip`` using the given directory prefix.
* ``--root <dir>``: Installs ``pip`` relative to the given root directory * ``--root <dir>``: Installs ``pip`` relative to the given root directory
rather than the root of the currently active virtual environment (if any) rather than the root of the currently active virtual environment (if any)
or the default root for the current Python installation. or the default root for the current Python installation.
@ -89,7 +90,7 @@ Module API
Returns a string specifying the bundled version of pip that will be Returns a string specifying the bundled version of pip that will be
installed when bootstrapping an environment. installed when bootstrapping an environment.
.. function:: bootstrap(root=None, upgrade=False, user=False, \ .. function:: bootstrap(root=None, prefix=None, upgrade=False, user=False, \
altinstall=False, default_pip=False, \ altinstall=False, default_pip=False, \
verbosity=0) verbosity=0)
@ -99,6 +100,8 @@ Module API
If *root* is ``None``, then installation uses the default install location If *root* is ``None``, then installation uses the default install location
for the current environment. for the current environment.
*prefix* specifies the directory prefix to use when installing.
*upgrade* indicates whether or not to upgrade an existing installation *upgrade* indicates whether or not to upgrade an existing installation
of an earlier version of ``pip`` to the bundled version. of an earlier version of ``pip`` to the bundled version.
@ -119,6 +122,8 @@ Module API
*verbosity* controls the level of output to :data:`sys.stdout` from the *verbosity* controls the level of output to :data:`sys.stdout` from the
bootstrapping operation. bootstrapping operation.
.. versionchanged:: 3.9 the *prefix* parameter was added.
.. audit-event:: ensurepip.bootstrap root ensurepip.bootstrap .. audit-event:: ensurepip.bootstrap root ensurepip.bootstrap
.. note:: .. note::

View File

@ -49,27 +49,27 @@ def _disable_pip_configuration_settings():
os.environ['PIP_CONFIG_FILE'] = os.devnull os.environ['PIP_CONFIG_FILE'] = os.devnull
def bootstrap(*, root=None, upgrade=False, user=False, def bootstrap(*, root=None, prefix=None, upgrade=False, user=False,
altinstall=False, default_pip=False, altinstall=False, default_pip=False,
verbosity=0): verbosity=0):
""" """
Bootstrap pip into the current Python installation (or the given root Bootstrap pip into the current Python installation (or the given root
directory). and directory prefix).
Note that calling this function will alter both sys.path and os.environ. Note that calling this function will alter both sys.path and os.environ.
""" """
# Discard the return value # Discard the return value
_bootstrap(root=root, upgrade=upgrade, user=user, _bootstrap(root=root, prefix=prefix, upgrade=upgrade, user=user,
altinstall=altinstall, default_pip=default_pip, altinstall=altinstall, default_pip=default_pip,
verbosity=verbosity) verbosity=verbosity)
def _bootstrap(*, root=None, upgrade=False, user=False, def _bootstrap(*, root=None, prefix=None, upgrade=False, user=False,
altinstall=False, default_pip=False, altinstall=False, default_pip=False,
verbosity=0): verbosity=0):
""" """
Bootstrap pip into the current Python installation (or the given root Bootstrap pip into the current Python installation (or the given root
directory). Returns pip command status code. and directory prefix). Returns pip command status code.
Note that calling this function will alter both sys.path and os.environ. Note that calling this function will alter both sys.path and os.environ.
""" """
@ -112,6 +112,8 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
args = ["install", "--no-index", "--find-links", tmpdir] args = ["install", "--no-index", "--find-links", tmpdir]
if root: if root:
args += ["--root", root] args += ["--root", root]
if prefix:
args += ["--prefix", prefix]
if upgrade: if upgrade:
args += ["--upgrade"] args += ["--upgrade"]
if user: if user:
@ -183,6 +185,11 @@ def _main(argv=None):
default=None, default=None,
help="Install everything relative to this alternate root directory.", help="Install everything relative to this alternate root directory.",
) )
parser.add_argument(
"--prefix",
default=None,
help="Install everything using this prefix.",
)
parser.add_argument( parser.add_argument(
"--altinstall", "--altinstall",
action="store_true", action="store_true",
@ -202,6 +209,7 @@ def _main(argv=None):
return _bootstrap( return _bootstrap(
root=args.root, root=args.root,
prefix=args.prefix,
upgrade=args.upgrade, upgrade=args.upgrade,
user=args.user, user=args.user,
verbosity=args.verbosity, verbosity=args.verbosity,

View File

@ -61,6 +61,17 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY, unittest.mock.ANY,
) )
def test_bootstrapping_with_prefix(self):
ensurepip.bootstrap(prefix="/foo/bar/")
self.run_pip.assert_called_once_with(
[
"install", "--no-index", "--find-links",
unittest.mock.ANY, "--prefix", "/foo/bar/",
"setuptools", "pip",
],
unittest.mock.ANY,
)
def test_bootstrapping_with_user(self): def test_bootstrapping_with_user(self):
ensurepip.bootstrap(user=True) ensurepip.bootstrap(user=True)

View File

@ -1181,7 +1181,7 @@ install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall @FRAMEWORKI
install|*) ensurepip="" ;; \ install|*) ensurepip="" ;; \
esac; \ esac; \
$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \ $(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
$$ensurepip --root=$(DESTDIR)/ ; \ $$ensurepip --root=$(DESTDIR)/ --prefix=$(prefix) ; \
fi fi
altinstall: commoninstall altinstall: commoninstall
@ -1191,7 +1191,7 @@ altinstall: commoninstall
install|*) ensurepip="--altinstall" ;; \ install|*) ensurepip="--altinstall" ;; \
esac; \ esac; \
$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \ $(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
$$ensurepip --root=$(DESTDIR)/ ; \ $$ensurepip --root=$(DESTDIR)/ --prefix=$(prefix) ; \
fi fi
commoninstall: check-clean-src @FRAMEWORKALTINSTALLFIRST@ \ commoninstall: check-clean-src @FRAMEWORKALTINSTALLFIRST@ \

View File

@ -0,0 +1 @@
A directory prefix can now be specified when using :mod:`ensurepip`.