1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Merge pull request #3252 from domenkozar/install/prefix

Support pip install --prefix
This commit is contained in:
Marcus Smith 2015-11-26 09:30:20 -08:00
commit ca47dc9949
7 changed files with 130 additions and 10 deletions

View file

@ -136,6 +136,14 @@ class InstallCommand(RequirementCommand):
help="Install everything relative to this alternate root "
"directory.")
cmd_opts.add_option(
'--prefix',
dest='prefix_path',
metavar='dir',
default=None,
help="Installation prefix where lib, bin and other top-level "
"folders are placed")
cmd_opts.add_option(
"--compile",
action="store_true",
@ -210,6 +218,11 @@ class InstallCommand(RequirementCommand):
options.src_dir = os.path.abspath(options.src_dir)
install_options = options.install_options or []
if options.use_user_site:
if options.prefix_path:
raise CommandError(
"Can not combine '--user' and '--prefix' as they imply "
"different installation locations"
)
if virtualenv_no_global():
raise InstallationError(
"Can not perform a '--user' install. User site-packages "
@ -301,6 +314,7 @@ class InstallCommand(RequirementCommand):
install_options,
global_options,
root=options.root_path,
prefix=options.prefix_path,
)
reqs = sorted(
requirement_set.successfully_installed,

View file

@ -154,7 +154,7 @@ site_config_files = [
def distutils_scheme(dist_name, user=False, home=None, root=None,
isolated=False):
isolated=False, prefix=None):
"""
Return a distutils install scheme
"""
@ -175,9 +175,9 @@ def distutils_scheme(dist_name, user=False, home=None, root=None,
# NOTE: setting user or home has the side-effect of creating the home dir
# or user base for installations during finalize_options()
# ideally, we'd prefer a scheme class that has no side-effects.
assert not (user and prefix), "Got user and prefix {}".format(user, prefix)
i.user = user or i.user
if user:
i.prefix = ""
i.prefix = prefix or i.prefix
i.home = home or i.home
i.root = root or i.root
i.finalize_options()

View file

@ -824,15 +824,17 @@ exec(compile(
else:
return True
def install(self, install_options, global_options=[], root=None):
def install(self, install_options, global_options=[], root=None,
prefix=None):
if self.editable:
self.install_editable(install_options, global_options)
self.install_editable(
install_options, global_options, prefix=prefix)
return
if self.is_wheel:
version = pip.wheel.wheel_version(self.source_dir)
pip.wheel.check_compatibility(version, self.name)
self.move_wheel_files(self.source_dir, root=root)
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
self.install_succeeded = True
return
@ -865,6 +867,8 @@ exec(compile(
if root is not None:
install_args += ['--root', root]
if prefix is not None:
install_args += ['--prefix', prefix]
if self.pycompile:
install_args += ["--compile"]
@ -960,12 +964,17 @@ exec(compile(
rmtree(self._temp_build_dir)
self._temp_build_dir = None
def install_editable(self, install_options, global_options=()):
def install_editable(self, install_options,
global_options=(), prefix=None):
logger.info('Running setup.py develop for %s', self.name)
if self.isolated:
global_options = list(global_options) + ["--no-user-cfg"]
if prefix:
prefix_param = ['--prefix={0}'.format(prefix)]
install_options = list(install_options) + prefix_param
with indent_log():
# FIXME: should we do --install-headers here too?
cwd = self.source_dir
@ -1022,12 +1031,13 @@ exec(compile(
def is_wheel(self):
return self.link and self.link.is_wheel
def move_wheel_files(self, wheeldir, root=None):
def move_wheel_files(self, wheeldir, root=None, prefix=None):
move_wheel_files(
self.name, self.req, wheeldir,
user=self.use_user_site,
home=self.target_dir,
root=root,
prefix=prefix,
pycompile=self.pycompile,
isolated=self.isolated,
)

View file

@ -235,12 +235,13 @@ def get_entrypoints(filename):
def move_wheel_files(name, req, wheeldir, user=False, home=None, root=None,
pycompile=True, scheme=None, isolated=False):
pycompile=True, scheme=None, isolated=False, prefix=None):
"""Install a wheel"""
if not scheme:
scheme = distutils_scheme(
name, user=user, home=home, root=root, isolated=isolated
name, user=user, home=home, root=root, isolated=isolated,
prefix=prefix,
)
if root_is_purelib(name, wheeldir):

View file

@ -1,4 +1,5 @@
import os
import sys
import textwrap
import glob
@ -550,6 +551,69 @@ def test_install_package_with_root(script, data):
assert root_path in result.files_created, str(result)
def test_install_package_with_prefix(script, data):
"""
Test installing a package using pip install --prefix
"""
prefix_path = script.scratch_path / 'prefix'
result = script.pip(
'install', '--prefix', prefix_path, '-f', data.find_links,
'--no-binary', 'simple', '--no-index', 'simple==1.0',
)
if hasattr(sys, "pypy_version_info"):
path = script.scratch / 'prefix'
else:
path = script.scratch / 'prefix' / 'lib' / 'python{0}'.format(pyversion) # noqa
install_path = (
path / 'site-packages' / 'simple-1.0-py{0}.egg-info'.format(pyversion)
)
assert install_path in result.files_created, str(result)
def test_install_editable_with_prefix(script):
# make a dummy project
pkga_path = script.scratch_path / 'pkga'
pkga_path.mkdir()
pkga_path.join("setup.py").write(textwrap.dedent("""
from setuptools import setup
setup(name='pkga',
version='0.1')
"""))
site_packages = os.path.join(
'prefix', 'lib', 'python{0}'.format(pyversion), 'site-packages')
# make sure target path is in PYTHONPATH
pythonpath = script.scratch_path / site_packages
pythonpath.makedirs()
script.environ["PYTHONPATH"] = pythonpath
# install pkga package into the absolute prefix directory
prefix_path = script.scratch_path / 'prefix'
result = script.pip(
'install', '--editable', pkga_path, '--prefix', prefix_path)
# assert pkga is installed at correct location
install_path = script.scratch / site_packages / 'pkga.egg-link'
assert install_path in result.files_created, str(result)
def test_install_package_conflict_prefix_and_user(script, data):
"""
Test installing a package using pip install --prefix --user errors out
"""
prefix_path = script.scratch_path / 'prefix'
result = script.pip(
'install', '-f', data.find_links, '--no-index', '--user',
'--prefix', prefix_path, 'simple==1.0',
expect_error=True, quiet=True,
)
assert (
"Can not combine '--user' and '--prefix'" in result.stderr
)
# skip on win/py3 for now, see issue #782
@pytest.mark.skipif("sys.platform == 'win32' and sys.version_info >= (3,)")
def test_install_package_that_emits_unicode(script, data):

View file

@ -1,4 +1,5 @@
import os
import sys
import pytest
import glob
@ -127,6 +128,22 @@ def test_install_wheel_with_root(script, data):
assert Path('scratch') / 'root' in result.files_created
def test_install_wheel_with_prefix(script, data):
"""
Test installing a wheel using pip install --prefix
"""
prefix_dir = script.scratch_path / 'prefix'
result = script.pip(
'install', 'simple.dist==0.1', '--prefix', prefix_dir,
'--no-index', '--find-links=' + data.find_links,
)
if hasattr(sys, "pypy_version_info"):
lib = Path('scratch') / 'prefix' / 'site-packages'
else:
lib = Path('scratch') / 'prefix' / 'lib'
assert lib in result.files_created
def test_install_from_wheel_installs_deps(script, data):
"""
Test can install dependencies of wheels

View file

@ -440,6 +440,20 @@ class TestMoveWheelFiles(object):
self.name, self.req, self.src, scheme=self.scheme)
self.assert_installed()
def test_install_prefix(self, data, tmpdir):
prefix = os.path.join(os.path.sep, 'some', 'path')
self.prep(data, tmpdir)
wheel.move_wheel_files(
self.name,
self.req,
self.src,
root=tmpdir,
prefix=prefix,
)
assert os.path.exists(os.path.join(tmpdir, 'some', 'path', 'bin'))
assert os.path.exists(os.path.join(tmpdir, 'some', 'path', 'my_data'))
def test_dist_info_contains_empty_dir(self, data, tmpdir):
"""
Test that empty dirs are not installed