6
6
"""
7
7
8
8
import inspect
9
+ import itertools
9
10
import logging
10
11
import logging .handlers
11
12
import optparse
14
15
15
16
import pkg_resources
16
17
18
+
17
19
class GroupWriteRotatingFileHandler (logging .handlers .RotatingFileHandler ):
18
20
"""Taken from http://stackoverflow.com/questions/1407474/does-python-logging-handlers-rotatingfilehandler-allow-creation-of-a-group-writa
19
21
"""
@@ -23,6 +25,7 @@ def _open(self):
23
25
os .umask (prevumask )
24
26
return rtv
25
27
28
+
26
29
def main ():
27
30
parser = optparse .OptionParser (
28
31
usage = 'usage: %prog [options] <hook> [<arguments>]' ,
@@ -67,7 +70,7 @@ def main():
67
70
dest = 'names' ,
68
71
default = [],
69
72
)
70
- parser .disable_interspersed_args () # stop when we hit an option without an '-'
73
+ parser .disable_interspersed_args () # stop when we hit an option without an '-'
71
74
options , args = parser .parse_args ()
72
75
73
76
root_logger = logging .getLogger ('' )
@@ -85,10 +88,10 @@ def main():
85
88
86
89
# Send higher-level messages to the console, too
87
90
console = logging .StreamHandler ()
88
- console_level = [ logging .WARNING ,
89
- logging .INFO ,
90
- logging .DEBUG ,
91
- ][options .verbose_level ]
91
+ console_level = [logging .WARNING ,
92
+ logging .INFO ,
93
+ logging .DEBUG ,
94
+ ][options .verbose_level ]
92
95
console .setLevel (console_level )
93
96
formatter = logging .Formatter ('%(name)s %(message)s' )
94
97
console .setFormatter (formatter )
@@ -98,7 +101,11 @@ def main():
98
101
99
102
# Determine which hook we're running
100
103
if not args :
101
- parser .error ('Please specify the hook to run' )
104
+ if options .listing :
105
+ list_hooks ()
106
+ return 0
107
+ else :
108
+ parser .error ('Please specify the hook to run' )
102
109
hook = args [0 ]
103
110
104
111
if options .sourcing and options .script_filename :
@@ -126,6 +133,7 @@ def main():
126
133
127
134
return 0
128
135
136
+
129
137
def run_hooks (hook , options , args , output = None ):
130
138
if output is None :
131
139
output = sys .stdout
@@ -135,7 +143,7 @@ def run_hooks(hook, options, args, output=None):
135
143
continue
136
144
plugin = ep .load ()
137
145
if options .listing :
138
- sys . stdout .write (' %-10s -- %s\n ' % (ep .name , inspect .getdoc (plugin ) or '' ))
146
+ output .write (' %-10s -- %s\n ' % (ep .name , inspect .getdoc (plugin ) or '' ))
139
147
continue
140
148
if options .sourcing :
141
149
# Show the shell commands so they can
@@ -149,5 +157,29 @@ def run_hooks(hook, options, args, output=None):
149
157
# Just run the plugin ourselves
150
158
plugin (args [1 :])
151
159
160
+
161
+ def list_hooks (output = None ):
162
+ if output is None :
163
+ output = sys .stdout
164
+ for hook in itertools .chain (
165
+ ('_' .join (h )
166
+ for h in itertools .product (['pre' , 'post' ],
167
+ ['mkvirtualenv' ,
168
+ 'rmvirtualenv' ,
169
+ 'activate' ,
170
+ 'deactivate' ,
171
+ 'cpvirtualenv' ,
172
+ ])
173
+ ),
174
+ ['initialize' ,
175
+ 'get_env_details' ,
176
+ 'project.pre_mkproject' ,
177
+ 'project.post_mkproject' ,
178
+ 'project.template' ,
179
+ ]
180
+ ):
181
+ output .write (hook + '\n ' )
182
+
183
+
152
184
if __name__ == '__main__' :
153
185
main ()
0 commit comments