Skip to content

Commit

Permalink
don't allow --user installs in no-global virtualenvs
Browse files Browse the repository at this point in the history
  • Loading branch information
qwcode committed Jun 6, 2012
1 parent 39bbf2d commit 2e1959a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pip/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pip.req import InstallRequirement, RequirementSet
from pip.req import parse_requirements
from pip.log import logger
from pip.locations import build_prefix, src_prefix
from pip.locations import build_prefix, src_prefix, virtualenv_no_global
from pip.basecommand import Command
from pip.index import PackageFinder
from pip.exceptions import InstallationError, CommandError
Expand Down Expand Up @@ -190,6 +190,8 @@ def run(self, options, args):
options.src_dir = os.path.abspath(options.src_dir)
install_options = options.install_options or []
if options.use_user_site:
if virtualenv_no_global():
raise InstallationError("Can not perform a '--user' install. User site-packages are not visible in this virtualenv.")
install_options.append('--user')
if options.target_dir:
options.ignore_installed = True
Expand Down
11 changes: 11 additions & 0 deletions pip/locations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Locations where we look for configs, install stuff, etc"""

import sys
import site
import os
import tempfile
from pip.backwardcompat import get_python_lib
Expand All @@ -13,6 +14,16 @@ def running_under_virtualenv():
"""
return hasattr(sys, 'real_prefix')

def virtualenv_no_global():
"""
Return True if in a venv and no system site packages.
"""
#this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file
site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
no_global_file = os.path.join(site_mod_dir,'no-global-site-packages.txt')
if running_under_virtualenv() and os.path.isfile(no_global_file):
return True


if running_under_virtualenv():
## FIXME: is build/ a good name?
Expand Down
11 changes: 11 additions & 0 deletions tests/test_user_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,14 @@ def test_install_curdir_usersite(self):
assert fspkg_folder in result.files_created, str(result.stdout)

assert egg_info_folder in result.files_created, str(result)


def test_install_user_venv_nositepkgs_fails(self):
"""
user install in virtualenv (with no system packages) fails with message
"""
env = reset_env()
run_from = abspath(join(here, 'packages', 'FSPkg'))
result = run_pip('install', '--user', curdir, cwd=run_from, expect_error=True)
assert "Can not perform a '--user' install. User site-packages are not visible in this virtualenv." in result.stdout

0 comments on commit 2e1959a

Please sign in to comment.