88
99import logging
1010import os
11+ import re
1112import stat
1213import subprocess
14+ import sys
1315
1416import pkg_resources
1517
1618log = logging .getLogger (__name__ )
1719
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+
1829
1930def run_script (script_path , * args ):
2031 """Execute a script in a subshell.
2132 """
2233 if os .path .exists (script_path ):
2334 cmd = [script_path ] + list (args )
35+ if is_msys :
36+ cmd = [get_path (os .environ ['MSYS_HOME' ],'bin' ,'sh.exe' )] + cmd
2437 log .debug ('running %s' , str (cmd ))
2538 try :
2639 return_code = subprocess .call (cmd )
@@ -33,7 +46,7 @@ def run_script(script_path, *args):
3346def run_global (script_name , * args ):
3447 """Run a script from $VIRTUALENVWRAPPER_HOOK_DIR.
3548 """
36- script_path = os . path . expandvars ( os . path . join ( '$VIRTUALENVWRAPPER_HOOK_DIR' , script_name ) )
49+ script_path = get_path ( '$VIRTUALENVWRAPPER_HOOK_DIR' , script_name )
3750 run_script (script_path , * args )
3851 return
3952
@@ -101,7 +114,7 @@ def make_hook(filename, comment):
101114 :param filename: The name of the file to write.
102115 :param comment: The comment to insert into the file.
103116 """
104- filename = os . path . expanduser ( os . path . expandvars ( filename ) )
117+ filename = get_path ( filename )
105118 if not os .path .exists (filename ):
106119 log .info ('creating %s' , filename )
107120 f = open (filename , 'w' )
@@ -122,7 +135,7 @@ def make_hook(filename, comment):
122135
123136def initialize (args ):
124137 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 )
126139 return
127140
128141
@@ -138,7 +151,7 @@ def pre_mkvirtualenv(args):
138151 log .debug ('pre_mkvirtualenv %s' , str (args ))
139152 envname = args [0 ]
140153 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 )
142155 run_global ('premkvirtualenv' , * args )
143156 return
144157
@@ -155,7 +168,7 @@ def pre_cpvirtualenv(args):
155168 log .debug ('pre_cpvirtualenv %s' , str (args ))
156169 envname = args [0 ]
157170 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 )
159172 run_global ('precpvirtualenv' , * args )
160173 return
161174
@@ -184,7 +197,7 @@ def post_rmvirtualenv(args):
184197def pre_activate (args ):
185198 log .debug ('pre_activate' )
186199 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' )
188201 run_script (script_path , * args )
189202 return
190203
@@ -196,7 +209,7 @@ def post_activate_source(args):
196209# Run user-provided scripts
197210#
198211[ -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"
200213"""
201214
202215
@@ -206,7 +219,7 @@ def pre_deactivate_source(args):
206219#
207220# Run user-provided scripts
208221#
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"
210223[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate"
211224"""
212225
@@ -227,6 +240,22 @@ def post_deactivate_source(args):
227240def get_env_details (args ):
228241 log .debug ('get_env_details' )
229242 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' )
231244 run_script (script_path , * args )
232245 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