I apologize if this turns out to be a repost -- gmail subscription oddities 
today.  S

Hey!

        I was having trouble seeing where things came from in a complex 
fabfile/fabkit setup and wrote the following list command to display commands 
by module.   

        Output looks like this (excuse any mail-induced funkiness, please):

Available commands:

fabfile:
   install_pido_python_support
   base_system_setup                Only Needed Once, But Needed! Get all the 
pre-requisite...
   test                             Run all unit tests and doctests.
fabkit.cpanel:
   restart_mail_servers             Gives the mail server a whack by restarting 
all of: ima...
fabkit.fab_cfg_install:
   install_from_config              Install specified packages as per config 
file.  Default...
fabkit.python:
   py_install_basic_tools           Installs distribute, pip, and virtualenv.
fabkit.ubuntu:
   install_working_toolset          RUN FIRST! Installs all compilers, VCS 
tools, Python bu...
   install_PIL                      Install PIL without Ubuntu incompatible 
extensions
fabkit.utils:
   remote_mkdir_p                   Safely create a directory, including 
intermediate dirs ...
   run_multiple                     This chains a bunch of commands with '&&' 
so, for examp...
   local_mkdir_p
   unarchive
   url_to_legal_pathname            Convert a URL to a legal pathname by 
replacing cruft wi...
   remove_archive_extensions        Strip off all of the compression extensions 
until there...
   sudocmd                          A general execute function for running 
specified comman...
   download_and_unarchive_package
urlparse:
   urlparse                         Parse a URL into 6 components:

        The order the functions come out within each module is controlled by 
the order they arrive in the main commands list so it seems somewhat arbitrary 
but useful nonetheless.

        If there's any interest, I'll submit a patch.

        What should the command line switch be?  --list-by-module would work 
for me (with no short command unless we're using argparse, then -lm).

        You can just copy the code below into main.py, and it will replace the 
default list_commands with this function.

Thanks,

S

def list_commands_by_module():
   """
   Print all found commands/tasks, BY MODULE then exit. Invoked with -l/--list.
   """
   print("Available commands:\n")
   # Want separator between name, description to be straight col
   cmd_keys = commands.keys()  # we need this more than once

   max_len = reduce(lambda a, b: max(a, len(b)), cmd_keys, 0)
   sep = '  '
   trail = '...'
   modules = {}
   for name in cmd_keys:
       func = commands[name]
       module = func.__module__
       if module in modules:
           modules[module].append(func)
       else:
           modules[module] = [func]
   module_names = sorted(modules.keys())
   for module_name in module_names:
       print module_name + ':'
       # TBD: ? this prints in the order they're found in the commands
       #       list, not sure how to order it better...
       for func in modules[module_name]:
           output = None
           name = func.func_name
           if func.__doc__:
               lines = filter(None, func.__doc__.splitlines())
               first_line = lines[0].strip()
               # Truncate it if it's longer than N chars
               size = 75 - (max_len + len(sep) + len(trail))
               if len(first_line) > size:
                   first_line = first_line[:size] + trail
               #   + "\n (%s)"%(func.__module__,)
               output = name.ljust(max_len) + sep + ' ' + first_line
           # Or nothing (so just the name)
           else:
               output = name
           print(indent(output))

   sys.exit(0)

list_commands = list_commands_by_module





_______________________________________________
Fab-user mailing list
Fab-user@nongnu.org
http://lists.nongnu.org/mailman/listinfo/fab-user

Reply via email to