8
8
9
9
import logging
10
10
import os
11
+ import re
11
12
import stat
12
13
import subprocess
14
+ import sys
13
15
14
16
import pkg_resources
15
17
16
18
log = logging .getLogger (__name__ )
17
19
20
+
21
+ # Are we running under msys
22
+ if sys .platform == 'win32' and os .environ .get ('OS' ) == 'Windows_NT' and os .environ .get ('MSYSTEM' ) == 'MINGW32' :
23
+ is_msys = True
24
+ script_folder = 'Scripts'
25
+ else :
26
+ is_msys = False
27
+ script_folder = 'bin'
28
+
18
29
19
30
def run_script (script_path , * args ):
20
31
"""Execute a script in a subshell.
21
32
"""
22
33
if os .path .exists (script_path ):
23
34
cmd = [script_path ] + list (args )
35
+ if is_msys :
36
+ cmd = [get_path (os .environ ['MSYS_HOME' ],'bin' ,'sh.exe' )] + cmd
24
37
log .debug ('running %s' , str (cmd ))
25
38
try :
26
39
return_code = subprocess .call (cmd )
@@ -33,7 +46,7 @@ def run_script(script_path, *args):
33
46
def run_global (script_name , * args ):
34
47
"""Run a script from $VIRTUALENVWRAPPER_HOOK_DIR.
35
48
"""
36
- script_path = os . path . expandvars ( os . path . join ( '$VIRTUALENVWRAPPER_HOOK_DIR' , script_name ) )
49
+ script_path = get_path ( '$VIRTUALENVWRAPPER_HOOK_DIR' , script_name )
37
50
run_script (script_path , * args )
38
51
return
39
52
@@ -101,7 +114,7 @@ def make_hook(filename, comment):
101
114
:param filename: The name of the file to write.
102
115
:param comment: The comment to insert into the file.
103
116
"""
104
- filename = os . path . expanduser ( os . path . expandvars ( filename ) )
117
+ filename = get_path ( filename )
105
118
if not os .path .exists (filename ):
106
119
log .info ('creating %s' , filename )
107
120
f = open (filename , 'w' )
@@ -122,7 +135,7 @@ def make_hook(filename, comment):
122
135
123
136
def initialize (args ):
124
137
for filename , comment in GLOBAL_HOOKS :
125
- make_hook (os . path . join ('$VIRTUALENVWRAPPER_HOOK_DIR' , filename ), comment )
138
+ make_hook (get_path ('$VIRTUALENVWRAPPER_HOOK_DIR' , filename ), comment )
126
139
return
127
140
128
141
@@ -138,7 +151,7 @@ def pre_mkvirtualenv(args):
138
151
log .debug ('pre_mkvirtualenv %s' , str (args ))
139
152
envname = args [0 ]
140
153
for filename , comment in LOCAL_HOOKS :
141
- make_hook (os . path . join ('$WORKON_HOME' , envname , 'bin' , filename ), comment )
154
+ make_hook (get_path ('$WORKON_HOME' , envname , script_folder , filename ), comment )
142
155
run_global ('premkvirtualenv' , * args )
143
156
return
144
157
@@ -155,7 +168,7 @@ def pre_cpvirtualenv(args):
155
168
log .debug ('pre_cpvirtualenv %s' , str (args ))
156
169
envname = args [0 ]
157
170
for filename , comment in LOCAL_HOOKS :
158
- make_hook (os . path . join ('$WORKON_HOME' , envname , 'bin' , filename ), comment )
171
+ make_hook (get_path ('$WORKON_HOME' , envname , script_folder , filename ), comment )
159
172
run_global ('precpvirtualenv' , * args )
160
173
return
161
174
@@ -184,7 +197,7 @@ def post_rmvirtualenv(args):
184
197
def pre_activate (args ):
185
198
log .debug ('pre_activate' )
186
199
run_global ('preactivate' , * args )
187
- script_path = os . path . expandvars ( os . path . join ( '$WORKON_HOME' , args [0 ], 'bin' , 'preactivate' ) )
200
+ script_path = get_path ( '$WORKON_HOME' , args [0 ], script_folder , 'preactivate' )
188
201
run_script (script_path , * args )
189
202
return
190
203
@@ -196,7 +209,7 @@ def post_activate_source(args):
196
209
# Run user-provided scripts
197
210
#
198
211
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postactivate"
199
- [ -f "$VIRTUAL_ENV/bin /postactivate" ] && source "$VIRTUAL_ENV/bin /postactivate"
212
+ [ -f "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR /postactivate" ] && source "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR /postactivate"
200
213
"""
201
214
202
215
@@ -206,7 +219,7 @@ def pre_deactivate_source(args):
206
219
#
207
220
# Run user-provided scripts
208
221
#
209
- [ -f "$VIRTUAL_ENV/bin /predeactivate" ] && source "$VIRTUAL_ENV/bin /predeactivate"
222
+ [ -f "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR /predeactivate" ] && source "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR /predeactivate"
210
223
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate"
211
224
"""
212
225
@@ -227,6 +240,22 @@ def post_deactivate_source(args):
227
240
def get_env_details (args ):
228
241
log .debug ('get_env_details' )
229
242
run_global ('get_env_details' , * args )
230
- script_path = os . path . expandvars ( os . path . join ( '$WORKON_HOME' , args [0 ], 'bin' , 'get_env_details' ) )
243
+ script_path = get_path ( '$WORKON_HOME' , args [0 ], script_folder , 'get_env_details' )
231
244
run_script (script_path , * args )
232
245
return
246
+
247
+ def get_path (* args ):
248
+ '''
249
+ Get a full path from args.
250
+ Path separator is determined according to the os and the shell and allow to use is_msys.
251
+ Variables and user are expanded during the process.
252
+ '''
253
+ path = os .path .expanduser (os .path .expandvars (os .path .join (* args )))
254
+ if is_msys :
255
+ # MSYS accept unix or Win32 and sometimes it drives to mixed style paths
256
+ if re .match (r'^/[a-zA-Z](/|^)' , path ):
257
+ # msys path could starts with '/c/'-form drive letter
258
+ path = '' .join ((path [1 ],':' ,path [2 :]))
259
+ path = path .replace ('/' , os .sep )
260
+
261
+ return os .path .abspath (path )
0 commit comments