Skip to content

Commit 19b3de7

Browse files
committed
add VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV variable for postdeactivate scripts
1 parent 7bc348b commit 19b3de7

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

virtualenvwrapper/user_scripts.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,32 @@
1414

1515
log = logging.getLogger(__name__)
1616

17-
def get_script_source(script_name):
18-
"""Retrieve the source code for a script.
19-
"""
20-
script_path = pkg_resources.resource_filename(__name__, script_name)
21-
if not script_path:
22-
raise RuntimeError('Missing script for %s', script_name)
23-
log.debug('Looking for %s in %s', script_name, script_path)
24-
return pkg_resources.resource_string(__name__, script_name)
25-
2617

2718
def run_script(script_path, *args):
2819
"""Execute a script in a subshell.
2920
"""
3021
if os.path.exists(script_path):
31-
log.debug('Running %s', script_path)
32-
subprocess.call([script_path] + list(args), shell=True)
22+
# with open(script_path, 'rt') as f:
23+
# print '+' * 80
24+
# print f.read()
25+
# print '+' * 80
26+
cmd = [script_path] + list(args)
27+
log.debug('Running %s', str(cmd))
28+
try:
29+
return_code = subprocess.call(cmd)
30+
except OSError, msg:
31+
log.error('ERROR: Could not run %s. %s', script_path, str(msg))
32+
#log.debug('Returned %s', return_code)
3333
return
3434

35+
3536
def run_global(script_name, *args):
37+
"""Run a script from $WORKON_HOME.
38+
"""
3639
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', script_name))
3740
run_script(script_path, *args)
3841
return
3942

40-
def run_local_from_arg(script_name, *args):
41-
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', args[0], 'bin', script_name))
42-
run_script(script_path, *args)
43-
return
44-
45-
def run_local_from_env(script_name, *args):
46-
script_path = os.path.expandvars(os.path.join('$VIRTUAL_ENV', 'bin', script_name))
47-
run_script(script_path, *args)
48-
return
4943

5044

5145
# HOOKS
@@ -59,35 +53,40 @@ def initialize_source(args):
5953
"""
6054

6155
def pre_mkvirtualenv(args):
62-
log.debug('pre_mkvirtualenv')
63-
run_global('premkvirtualenv')
56+
log.debug('pre_mkvirtualenv %s', str(args))
57+
run_global('premkvirtualenv', *args)
6458
return
6559

60+
6661
def post_mkvirtualenv_source(args):
6762
return """
6863
#
6964
# Run user-provided scripts
7065
#
7166
[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
72-
[ -f "$VIRTUAL_ENV/bin/postmkvirtualenv" ] && source "$VIRTUAL_ENV/bin/postmkvirtualenv"
7367
"""
7468

69+
7570
def pre_rmvirtualenv(args):
7671
log.debug('pre_rmvirtualenv')
7772
run_global('prermvirtualenv', *args)
7873
return
7974

75+
8076
def post_rmvirtualenv(args):
8177
log.debug('post_rmvirtualenv')
8278
run_global('postrmvirtualenv', *args)
8379
return
8480

81+
8582
def pre_activate(args):
8683
log.debug('pre_activate')
8784
run_global('preactivate', *args)
88-
run_local_from_arg('preactivate', *args)
85+
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', args[0], 'bin', 'preactivate'))
86+
run_script(script_path, *args)
8987
return
9088

89+
9190
def post_activate_source(args):
9291
log.debug('post_activate')
9392
return """
@@ -98,6 +97,7 @@ def post_activate_source(args):
9897
[ -f "$VIRTUAL_ENV/bin/postactivate" ] && source "$VIRTUAL_ENV/bin/postactivate"
9998
"""
10099

100+
101101
def pre_deactivate_source(args):
102102
log.debug('pre_deactivate')
103103
return """
@@ -108,12 +108,15 @@ def pre_deactivate_source(args):
108108
[ -f "$WORKON_HOME/predeactivate" ] && source "$WORKON_HOME/predeactivate"
109109
"""
110110

111+
111112
def post_deactivate_source(args):
112113
log.debug('post_deactivate')
113114
return """
114115
#
115116
# Run user-provided scripts
116117
#
118+
VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV="$WORKON_HOME/%(env_name)s"
117119
[ -f "$WORKON_HOME/%(env_name)s/bin/postdeactivate" ] && source "$WORKON_HOME/%(env_name)s/bin/postdeactivate"
118120
[ -f "$WORKON_HOME/postdeactivate" ] && source "$WORKON_HOME/postdeactivate"
121+
unset VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV
119122
""" % { 'env_name':args[0] }

0 commit comments

Comments
 (0)