66"""
77
88import inspect
9+ import itertools
910import logging
1011import logging .handlers
1112import optparse
1415
1516import pkg_resources
1617
18+
1719class GroupWriteRotatingFileHandler (logging .handlers .RotatingFileHandler ):
1820 """Taken from http://stackoverflow.com/questions/1407474/does-python-logging-handlers-rotatingfilehandler-allow-creation-of-a-group-writa
1921 """
@@ -23,6 +25,7 @@ def _open(self):
2325 os .umask (prevumask )
2426 return rtv
2527
28+
2629def main ():
2730 parser = optparse .OptionParser (
2831 usage = 'usage: %prog [options] <hook> [<arguments>]' ,
@@ -67,7 +70,7 @@ def main():
6770 dest = 'names' ,
6871 default = [],
6972 )
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 '-'
7174 options , args = parser .parse_args ()
7275
7376 root_logger = logging .getLogger ('' )
@@ -85,10 +88,10 @@ def main():
8588
8689 # Send higher-level messages to the console, too
8790 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 ]
9295 console .setLevel (console_level )
9396 formatter = logging .Formatter ('%(name)s %(message)s' )
9497 console .setFormatter (formatter )
@@ -98,7 +101,11 @@ def main():
98101
99102 # Determine which hook we're running
100103 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' )
102109 hook = args [0 ]
103110
104111 if options .sourcing and options .script_filename :
@@ -126,6 +133,7 @@ def main():
126133
127134 return 0
128135
136+
129137def run_hooks (hook , options , args , output = None ):
130138 if output is None :
131139 output = sys .stdout
@@ -135,7 +143,7 @@ def run_hooks(hook, options, args, output=None):
135143 continue
136144 plugin = ep .load ()
137145 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 '' ))
139147 continue
140148 if options .sourcing :
141149 # Show the shell commands so they can
@@ -149,5 +157,29 @@ def run_hooks(hook, options, args, output=None):
149157 # Just run the plugin ourselves
150158 plugin (args [1 :])
151159
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+
152184if __name__ == '__main__' :
153185 main ()
0 commit comments