1
"""Tools for working with virtualenv environments"""
6
from pip.exceptions import BadCommand
7
from pip.log import logger
10
def restart_in_venv(venv, base, site_packages, args):
12
Restart this script using the interpreter in the given virtual environment
14
if base and not os.path.isabs(venv) and not venv.startswith('~'):
15
base = os.path.expanduser(base)
16
# ensure we have an abs basepath at this point:
17
# a relative one makes no sense (or does it?)
18
if os.path.isabs(base):
19
venv = os.path.join(base, venv)
21
if venv.startswith('~'):
22
venv = os.path.expanduser(venv)
24
if not os.path.exists(venv):
28
print 'The virtual environment does not exist: %s' % venv
29
print 'and virtualenv is not installed, so a new environment cannot be created'
31
print 'Creating new virtualenv environment in %s' % venv
32
virtualenv.logger = logger
34
virtualenv.create_environment(venv, site_packages=site_packages)
35
if sys.platform == 'win32':
36
python = os.path.join(venv, 'Scripts', 'python.exe')
37
# check for bin directory which is used in buildouts
38
if not os.path.exists(python):
39
python = os.path.join(venv, 'bin', 'python.exe')
41
python = os.path.join(venv, 'bin', 'python')
42
if not os.path.exists(python):
44
if not os.path.exists(python):
45
raise BadCommand('Cannot find virtual environment interpreter at %s' % python)
46
base = os.path.dirname(os.path.dirname(python))
47
file = os.path.join(os.path.dirname(__file__), 'runner.py')
48
if file.endswith('.pyc'):
50
proc = subprocess.Popen(
51
[python, file] + args + [base, '___VENV_RESTART___'])
53
sys.exit(proc.returncode)