Skip to content

Commit 8a74267

Browse files
committed
Added msys paths support
1 parent f91d717 commit 8a74267

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

virtualenvwrapper.sh

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ then
5656
VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
5757
fi
5858

59+
# Define script folder depending on the platorm (Win32/Unix)
60+
script_folder="bin"
61+
if [ "$OS" = "Windows_NT" ] && [ "$MSYSTEM" = "MINGW32" ]
62+
then
63+
# Only assign this for msys, cygwin use standard Unix paths
64+
# and its own python installation
65+
script_folder="Scripts"
66+
fi
67+
5968
virtualenvwrapper_derive_workon_home() {
6069
typeset workon_home_dir="$WORKON_HOME"
6170

@@ -277,7 +286,7 @@ virtualenvwrapper_show_workon_options () {
277286
# NOTE: DO NOT use ls here because colorized versions spew control characters
278287
# into the output list.
279288
# echo seems a little faster than find, even with -depth 3.
280-
(cd "$WORKON_HOME"; for f in */bin/activate; do echo $f; done) 2>/dev/null | \sed 's|^\./||' | \sed 's|/bin/activate||' | \sort | (unset GREP_OPTIONS; \egrep -v '^\*$')
289+
(cd "$WORKON_HOME"; for f in */$script_folder/activate; do echo $f; done) 2>/dev/null | \sed 's|^\./||' | \sed 's|/bin/activate||' | \sort | (unset GREP_OPTIONS; \egrep -v '^\*$')
281290

282291
# (cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
283292
}
@@ -357,7 +366,7 @@ workon () {
357366
virtualenvwrapper_verify_workon_home || return 1
358367
virtualenvwrapper_verify_workon_environment $env_name || return 1
359368

360-
activate="$WORKON_HOME/$env_name/bin/activate"
369+
activate="$WORKON_HOME/$env_name/$script_folder/activate"
361370
if [ ! -f "$activate" ]
362371
then
363372
echo "ERROR: Environment '$WORKON_HOME/$env_name' does not contain an activate script." >&2
@@ -390,7 +399,7 @@ workon () {
390399
# any settings made by the local postactivate first.
391400
virtualenvwrapper_run_hook "pre_deactivate"
392401
393-
env_postdeactivate_hook="$VIRTUAL_ENV/bin/postdeactivate"
402+
env_postdeactivate_hook="$VIRTUAL_ENV/$script_folder/postdeactivate"
394403
old_env=$(basename "$VIRTUAL_ENV")
395404
396405
# Call the original function.
@@ -589,7 +598,7 @@ cpvirtualenv() {
589598
fi
590599

591600
\cp -r "$source_env" "$target_env"
592-
for script in $( \ls $target_env/bin/* )
601+
for script in $( \ls $target_env/$script_folder/* )
593602
do
594603
newscript="$script-new"
595604
\sed "s|$source_env|$target_env|g" < "$script" > "$newscript"
@@ -598,7 +607,7 @@ cpvirtualenv() {
598607
done
599608

600609
virtualenv "$target_env" --relocatable
601-
\sed "s/VIRTUAL_ENV\(.*\)$env_name/VIRTUAL_ENV\1$new_env/g" < "$source_env/bin/activate" > "$target_env/bin/activate"
610+
\sed "s/VIRTUAL_ENV\(.*\)$env_name/VIRTUAL_ENV\1$new_env/g" < "$source_env/bin/activate" > "$target_env/$script_folder/activate"
602611

603612
(cd "$WORKON_HOME" && (
604613
virtualenvwrapper_run_hook "pre_cpvirtualenv" "$env_name" "$new_env";

virtualenvwrapper/user_scripts.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,31 @@
88

99
import logging
1010
import os
11+
import re
1112
import stat
1213
import subprocess
14+
import sys
1315

1416
import pkg_resources
1517

1618
log = 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+
msys = True
24+
script_folder = 'Scripts'
25+
else:
26+
script_folder = 'bin'
27+
1828

1929
def run_script(script_path, *args):
2030
"""Execute a script in a subshell.
2131
"""
2232
if os.path.exists(script_path):
2333
cmd = [script_path] + list(args)
34+
if msys:
35+
cmd = [os.path.join(os.environ['MSYS_HOME'],'bin','sh.exe')] + cmd
2436
log.debug('running %s', str(cmd))
2537
try:
2638
return_code = subprocess.call(cmd)
@@ -33,7 +45,7 @@ def run_script(script_path, *args):
3345
def run_global(script_name, *args):
3446
"""Run a script from $VIRTUALENVWRAPPER_HOOK_DIR.
3547
"""
36-
script_path = os.path.expandvars(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', script_name))
48+
script_path = get_path('$VIRTUALENVWRAPPER_HOOK_DIR', script_name)
3749
run_script(script_path, *args)
3850
return
3951

@@ -101,7 +113,7 @@ def make_hook(filename, comment):
101113
:param filename: The name of the file to write.
102114
:param comment: The comment to insert into the file.
103115
"""
104-
filename = os.path.expanduser(os.path.expandvars(filename))
116+
filename = get_path(filename)
105117
if not os.path.exists(filename):
106118
log.info('creating %s', filename)
107119
f = open(filename, 'w')
@@ -122,7 +134,7 @@ def make_hook(filename, comment):
122134

123135
def initialize(args):
124136
for filename, comment in GLOBAL_HOOKS:
125-
make_hook(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', filename), comment)
137+
make_hook(get_path('$VIRTUALENVWRAPPER_HOOK_DIR', filename), comment)
126138
return
127139

128140

@@ -138,7 +150,7 @@ def pre_mkvirtualenv(args):
138150
log.debug('pre_mkvirtualenv %s', str(args))
139151
envname=args[0]
140152
for filename, comment in LOCAL_HOOKS:
141-
make_hook(os.path.join('$WORKON_HOME', envname, 'bin', filename), comment)
153+
make_hook(get_path('$WORKON_HOME', envname, script_folder, filename), comment)
142154
run_global('premkvirtualenv', *args)
143155
return
144156

@@ -155,7 +167,7 @@ def pre_cpvirtualenv(args):
155167
log.debug('pre_cpvirtualenv %s', str(args))
156168
envname=args[0]
157169
for filename, comment in LOCAL_HOOKS:
158-
make_hook(os.path.join('$WORKON_HOME', envname, 'bin', filename), comment)
170+
make_hook(get_path('$WORKON_HOME', envname, script_folder, filename), comment)
159171
run_global('precpvirtualenv', *args)
160172
return
161173

@@ -184,7 +196,7 @@ def post_rmvirtualenv(args):
184196
def pre_activate(args):
185197
log.debug('pre_activate')
186198
run_global('preactivate', *args)
187-
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', args[0], 'bin', 'preactivate'))
199+
script_path = get_path('$WORKON_HOME', args[0], script_folder, 'preactivate')
188200
run_script(script_path, *args)
189201
return
190202

@@ -227,6 +239,22 @@ def post_deactivate_source(args):
227239
def get_env_details(args):
228240
log.debug('get_env_details')
229241
run_global('get_env_details', *args)
230-
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', args[0], 'bin', 'get_env_details'))
242+
script_path = get_path('$WORKON_HOME', args[0], script_folder, 'get_env_details')
231243
run_script(script_path, *args)
232244
return
245+
246+
def get_path(*args):
247+
'''
248+
Get a full path from args.
249+
Path separator is determined according to the os and the shell and allow to use msys.
250+
Variables and user are expanded during the process.
251+
'''
252+
path = os.path.expanduser(os.path.expandvars(os.path.join(*args)))
253+
if msys:
254+
# MSYS accept unix or Win32 and sometimes it conduce to mixed style paths
255+
if re.match(r'^/[a-zA-Z](/|^)', path):
256+
# msys path could starts with '/c/'-form drive letter
257+
path = ''.join((path[1],':',path[2:]))
258+
path = path.replace('/', os.sep)
259+
260+
return os.path.abspath(path)

0 commit comments

Comments
 (0)