14
14
15
15
log = logging .getLogger (__name__ )
16
16
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
-
26
17
27
18
def run_script (script_path , * args ):
28
19
"""Execute a script in a subshell.
29
20
"""
30
21
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)
33
33
return
34
34
35
+
35
36
def run_global (script_name , * args ):
37
+ """Run a script from $WORKON_HOME.
38
+ """
36
39
script_path = os .path .expandvars (os .path .join ('$WORKON_HOME' , script_name ))
37
40
run_script (script_path , * args )
38
41
return
39
42
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
49
43
50
44
51
45
# HOOKS
@@ -59,35 +53,40 @@ def initialize_source(args):
59
53
"""
60
54
61
55
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 )
64
58
return
65
59
60
+
66
61
def post_mkvirtualenv_source (args ):
67
62
return """
68
63
#
69
64
# Run user-provided scripts
70
65
#
71
66
[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
72
- [ -f "$VIRTUAL_ENV/bin/postmkvirtualenv" ] && source "$VIRTUAL_ENV/bin/postmkvirtualenv"
73
67
"""
74
68
69
+
75
70
def pre_rmvirtualenv (args ):
76
71
log .debug ('pre_rmvirtualenv' )
77
72
run_global ('prermvirtualenv' , * args )
78
73
return
79
74
75
+
80
76
def post_rmvirtualenv (args ):
81
77
log .debug ('post_rmvirtualenv' )
82
78
run_global ('postrmvirtualenv' , * args )
83
79
return
84
80
81
+
85
82
def pre_activate (args ):
86
83
log .debug ('pre_activate' )
87
84
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 )
89
87
return
90
88
89
+
91
90
def post_activate_source (args ):
92
91
log .debug ('post_activate' )
93
92
return """
@@ -98,6 +97,7 @@ def post_activate_source(args):
98
97
[ -f "$VIRTUAL_ENV/bin/postactivate" ] && source "$VIRTUAL_ENV/bin/postactivate"
99
98
"""
100
99
100
+
101
101
def pre_deactivate_source (args ):
102
102
log .debug ('pre_deactivate' )
103
103
return """
@@ -108,12 +108,15 @@ def pre_deactivate_source(args):
108
108
[ -f "$WORKON_HOME/predeactivate" ] && source "$WORKON_HOME/predeactivate"
109
109
"""
110
110
111
+
111
112
def post_deactivate_source (args ):
112
113
log .debug ('post_deactivate' )
113
114
return """
114
115
#
115
116
# Run user-provided scripts
116
117
#
118
+ VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV="$WORKON_HOME/%(env_name)s"
117
119
[ -f "$WORKON_HOME/%(env_name)s/bin/postdeactivate" ] && source "$WORKON_HOME/%(env_name)s/bin/postdeactivate"
118
120
[ -f "$WORKON_HOME/postdeactivate" ] && source "$WORKON_HOME/postdeactivate"
121
+ unset VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV
119
122
""" % { 'env_name' :args [0 ] }
0 commit comments