Skip to content

Commit 04f8971

Browse files
committed
clean up script mode changes
Change-Id: Ibaf3590f7929c9f7230fe96358934e6bb49f25d0
1 parent aa4a324 commit 04f8971

File tree

5 files changed

+87
-61
lines changed

5 files changed

+87
-61
lines changed

tests/test.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@ setUp () {
1919
rm -f "$TMPDIR/catch_output"
2020
}
2121

22+
SOURCE_SCRIPTS="initialize postmkvirtualenv predeactivate postdeactivate postactivate "
23+
RUN_SCRIPTS="premkvirtualenv prermvirtualenv postrmvirtualenv preactivate get_env_details"
24+
2225
test_virtualenvwrapper_initialize() {
2326
assertTrue "Initialized" virtualenvwrapper_initialize
24-
for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
27+
for hook in $SOURCE_SCRIPTS
28+
do
29+
assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
30+
assertFalse "Global $WORKON_HOME/$hook is executable" "[ -x $WORKON_HOME/$hook ]"
31+
done
32+
for hook in $RUN_SCRIPTS
2533
do
2634
assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
27-
assertTrue "Global $WORKON_HOME/$hook is not executable" "[ -x $WORKON_HOME/$hook ]"
35+
assertTrue "Global $WORKON_HOME/$hook is executable" "[ -x $WORKON_HOME/$hook ]"
2836
done
2937
echo "echo GLOBAL initialize >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/initialize"
3038
virtualenvwrapper_initialize

tests/test_hook_dir.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ setUp () {
1818
rm -f "$WORKON_HOME/hooks/*"
1919
}
2020

21+
SOURCE_SCRIPTS="initialize postmkvirtualenv predeactivate postdeactivate postactivate "
22+
RUN_SCRIPTS="premkvirtualenv prermvirtualenv postrmvirtualenv preactivate get_env_details"
23+
2124
test_virtualenvwrapper_initialize() {
2225
export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME/hooks"
2326
source "$test_dir/../virtualenvwrapper.sh"
24-
for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
27+
for hook in $SOURCE_SCRIPTS
28+
do
29+
assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/hooks/$hook ]"
30+
assertFalse "Global $WORKON_HOME/$hook is executable" "[ -x $WORKON_HOME/hooks/$hook ]"
31+
done
32+
for hook in $RUN_SCRIPTS
2533
do
26-
assertTrue "Global $hook was not created" "[ -f $WORKON_HOME/hooks/$hook ]"
27-
assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/hooks/$hook ]"
34+
assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/hooks/$hook ]"
35+
assertTrue "Global $WORKON_HOME/$hook is executable" "[ -x $WORKON_HOME/hooks/$hook ]"
2836
done
2937
}
3038

tests/test_mkvirtualenv.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test_create() {
2424
for hook in postactivate predeactivate postdeactivate
2525
do
2626
assertTrue "env1 $hook was not created" "[ -f $WORKON_HOME/env1/bin/$hook ]"
27-
assertTrue "env1 $hook is not executable" "[ -x $WORKON_HOME/env1/bin/$hook ]"
27+
assertFalse "env1 $hook is executable" "[ -x $WORKON_HOME/env1/bin/$hook ]"
2828
done
2929
}
3030

virtualenvwrapper/project.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
import logging
99
import os
1010

11-
from virtualenvwrapper.user_scripts import make_hook, run_global
11+
from virtualenvwrapper.user_scripts import make_hook, run_global, PERMISSIONS
1212

1313
log = logging.getLogger(__name__)
1414

1515
GLOBAL_HOOKS = [
1616
# mkproject
1717
("premkproject",
1818
"This hook is run after a new project is created "
19-
"and before it is activated."),
19+
"and before it is activated.",
20+
PERMISSIONS),
2021
("postmkproject",
21-
"This hook is run after a new project is activated."),
22+
"This hook is run after a new project is activated.",
23+
PERMISSIONS),
2224
]
2325

2426

2527
def initialize(args):
2628
"""Set up user hooks
2729
"""
28-
for filename, comment in GLOBAL_HOOKS:
30+
for filename, comment, permissions in GLOBAL_HOOKS:
2931
make_hook(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', filename),
30-
comment)
32+
comment, permissions)
3133
return
3234

3335

virtualenvwrapper/user_scripts.py

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,68 @@ def run_global(script_name, *args):
5151
return
5252

5353

54-
PERMISSIONS = (stat.S_IRWXU # read/write/execute, user
55-
| stat.S_IRWXG # read/write/execute, group
56-
| stat.S_IROTH # read, others
57-
| stat.S_IXOTH) # execute, others
58-
PERMISSIONS_SOURCED = PERMISSIONS \
59-
& ~ (# remove executable bits for
60-
stat.S_IXUSR # ... user
61-
| stat.S_IXGRP # ... group
62-
| stat.S_IXOTH) # ... others
54+
PERMISSIONS = (
55+
stat.S_IRWXU # read/write/execute, user
56+
| stat.S_IRGRP # read, group
57+
| stat.S_IXGRP # execute, group
58+
| stat.S_IROTH # read, others
59+
| stat.S_IXOTH # execute, others
60+
)
61+
PERMISSIONS_SOURCED = PERMISSIONS & ~(
62+
# remove executable bits for
63+
stat.S_IXUSR # ... user
64+
| stat.S_IXGRP # ... group
65+
| stat.S_IXOTH # ... others
66+
)
6367

6468

6569
GLOBAL_HOOKS = [
6670
# initialize
6771
("initialize",
6872
"This hook is sourced during the startup phase "
69-
"when loading virtualenvwrapper.sh."),
73+
"when loading virtualenvwrapper.sh.",
74+
PERMISSIONS_SOURCED),
7075

7176
# mkvirtualenv
7277
("premkvirtualenv",
7378
"This hook is run after a new virtualenv is created "
74-
"and before it is activated."),
75-
# argument: name of new environment
79+
"and before it is activated.\n"
80+
"# argument: name of new environment",
81+
PERMISSIONS),
7682
("postmkvirtualenv",
77-
"This hook is sourced after a new virtualenv is activated."),
83+
"This hook is sourced after a new virtualenv is activated.",
84+
PERMISSIONS_SOURCED),
7885

7986
# cpvirtualenv:
8087
# precpvirtualenv <old> <new> (run),
8188
# postcpvirtualenv (sourced)
8289

8390
# rmvirtualenv
8491
("prermvirtualenv",
85-
"This hook is run before a virtualenv is deleted."),
86-
# argument: full path to environment directory
92+
"This hook is run before a virtualenv is deleted.\n"
93+
"# argument: full path to environment directory",
94+
PERMISSIONS),
8795
("postrmvirtualenv",
88-
"This hook is run after a virtualenv is deleted."),
89-
# argument: full path to environment directory
96+
"This hook is run after a virtualenv is deleted.\n"
97+
"# argument: full path to environment directory",
98+
PERMISSIONS),
9099

91100
# deactivate
92101
("predeactivate",
93-
"This hook is sourced before every virtualenv is deactivated."),
102+
"This hook is sourced before every virtualenv is deactivated.",
103+
PERMISSIONS_SOURCED),
94104
("postdeactivate",
95-
"This hook is sourced after every virtualenv is deactivated."),
105+
"This hook is sourced after every virtualenv is deactivated.",
106+
PERMISSIONS_SOURCED),
96107

97108
# activate
98109
("preactivate",
99-
"This hook is run before every virtualenv is activated."),
100-
# argument: environment name
110+
"This hook is run before every virtualenv is activated.\n"
111+
"# argument: environment name",
112+
PERMISSIONS),
101113
("postactivate",
102-
"This hook is sourced after every virtualenv is activated."),
114+
"This hook is sourced after every virtualenv is activated.",
115+
PERMISSIONS_SOURCED),
103116

104117
# mkproject:
105118
# premkproject <new project name> (run),
@@ -108,42 +121,39 @@ def run_global(script_name, *args):
108121
# get_env_details
109122
("get_env_details",
110123
"This hook is run when the list of virtualenvs is printed "
111-
"so each name can include details."),
112-
# argument: environment name
124+
"so each name can include details.\n"
125+
"# argument: environment name",
126+
PERMISSIONS),
113127
]
114128

115129

116130
LOCAL_HOOKS = [
117131
# deactivate
118132
("predeactivate",
119-
"This hook is sourced before this virtualenv is deactivated."),
133+
"This hook is sourced before this virtualenv is deactivated.",
134+
PERMISSIONS_SOURCED),
120135
("postdeactivate",
121-
"This hook is sourced after this virtualenv is deactivated."),
136+
"This hook is sourced after this virtualenv is deactivated.",
137+
PERMISSIONS_SOURCED),
122138

123139
# activate
124140
("preactivate",
125-
"This hook is run before this virtualenv is activated."),
141+
"This hook is run before this virtualenv is activated.",
142+
PERMISSIONS),
126143
("postactivate",
127-
"This hook is sourced after this virtualenv is activated."),
144+
"This hook is sourced after this virtualenv is activated.",
145+
PERMISSIONS_SOURCED),
128146

129147
# get_env_details
130148
("get_env_details",
131149
"This hook is run when the list of virtualenvs is printed "
132-
"in 'long' mode so each name can include details."),
133-
# argument: environment name
150+
"in 'long' mode so each name can include details.\n"
151+
"# argument: environment name",
152+
PERMISSIONS),
134153
]
135154

136155

137-
SOURCED = ('initialize',
138-
'postactivate',
139-
'predeactivate',
140-
'postdeactivate',
141-
'postmkproject',
142-
'postmkvirtualenv',
143-
)
144-
145-
146-
def make_hook(filename, comment):
156+
def make_hook(filename, comment, permissions):
147157
"""Create a hook script.
148158
149159
:param filename: The name of the file to write.
@@ -162,19 +172,17 @@ def make_hook(filename, comment):
162172
})
163173
finally:
164174
f.close()
165-
os.chmod(filename,
166-
os.path.basename(filename) in SOURCED
167-
and PERMISSIONS_SOURCED
168-
or PERMISSIONS)
175+
os.chmod(filename, permissions)
169176
return
170177

171178

172179
# HOOKS
173180

174181

175182
def initialize(args):
176-
for filename, comment in GLOBAL_HOOKS:
177-
make_hook(get_path('$VIRTUALENVWRAPPER_HOOK_DIR', filename), comment)
183+
for filename, comment, permissions in GLOBAL_HOOKS:
184+
make_hook(get_path('$VIRTUALENVWRAPPER_HOOK_DIR', filename),
185+
comment, permissions)
178186
return
179187

180188

@@ -191,9 +199,9 @@ def initialize_source(args):
191199
def pre_mkvirtualenv(args):
192200
log.debug('pre_mkvirtualenv %s', str(args))
193201
envname = args[0]
194-
for filename, comment in LOCAL_HOOKS:
202+
for filename, comment, permissions in LOCAL_HOOKS:
195203
make_hook(get_path('$WORKON_HOME', envname, script_folder, filename),
196-
comment)
204+
comment, permissions)
197205
run_global('premkvirtualenv', *args)
198206
return
199207

@@ -211,9 +219,9 @@ def post_mkvirtualenv_source(args):
211219
def pre_cpvirtualenv(args):
212220
log.debug('pre_cpvirtualenv %s', str(args))
213221
envname = args[0]
214-
for filename, comment in LOCAL_HOOKS:
222+
for filename, comment, permissions in LOCAL_HOOKS:
215223
make_hook(get_path('$WORKON_HOME', envname, script_folder, filename),
216-
comment)
224+
comment, permissions)
217225
run_global('precpvirtualenv', *args)
218226
return
219227

0 commit comments

Comments
 (0)