source file: /home/buildslave/tahoe/edgy/build/src/allmydata/scripts/runner.py
file stats: 61 lines, 45 executed: 73.8% covered
   1. 
   2. import sys
   3. from cStringIO import StringIO
   4. from twisted.python import usage
   5. 
   6. from allmydata.scripts.common import BaseOptions
   7. import debug, create_node, startstop_node, cli, keygen
   8. 
   9. _general_commands = ( create_node.subCommands
  10.                     + keygen.subCommands
  11.                     + debug.subCommands
  12.                     + cli.subCommands
  13.                     )
  14. 
  15. class Options(BaseOptions, usage.Options):
  16.     synopsis = "Usage:  tahoe <command> [command options]"
  17. 
  18.     subCommands = []
  19.     subCommands += _general_commands
  20.     subCommands += startstop_node.subCommands
  21. 
  22.     def postOptions(self):
  23.         if not hasattr(self, 'subOptions'):
  24.             raise usage.UsageError("must specify a command")
  25. 
  26. def runner(argv,
  27.            run_by_human=True,
  28.            stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
  29.            install_node_control=True, additional_commands=None):
  30. 
  31.     config = Options()
  32.     if install_node_control:
  33.         config.subCommands.extend(startstop_node.subCommands)
  34. 
  35.     ac_dispatch = {}
  36.     if additional_commands:
  37.         for ac in additional_commands:
  38.             config.subCommands.extend(ac.subCommands)
  39.             ac_dispatch.update(ac.dispatch)
  40. 
  41.     try:
  42.         config.parseOptions(argv)
  43.     except usage.error, e:
  44.         if not run_by_human:
  45.             raise
  46.         c = config
  47.         while hasattr(c, 'subOptions'):
  48.             c = c.subOptions
  49.         print str(c)
  50.         print "%s:  %s" % (sys.argv[0], e)
  51.         return 1
  52. 
  53.     command = config.subCommand
  54.     so = config.subOptions
  55. 
  56.     if config['quiet']:
  57.         stdout = StringIO()
  58. 
  59.     so.stdout = stdout
  60.     so.stderr = stderr
  61.     so.stdin = stdin
  62. 
  63.     rc = 0
  64.     if command in create_node.dispatch:
  65.         for basedir in so.basedirs:
  66.             f = create_node.dispatch[command]
  67.             rc = f(basedir, so, stdout, stderr) or rc
  68.     elif command in startstop_node.dispatch:
  69.         rc = startstop_node.dispatch[command](so, stdout, stderr)
  70.     elif command in debug.dispatch:
  71.         rc = debug.dispatch[command](so)
  72.     elif command in cli.dispatch:
  73.         rc = cli.dispatch[command](so)
  74.     elif command in keygen.dispatch:
  75.         rc = keygen.dispatch[command](so, stdout, stderr)
  76.     elif command in ac_dispatch:
  77.         rc = ac_dispatch[command](so, stdout, stderr)
  78.     else:
  79.         raise usage.UsageError()
  80. 
  81.     return rc
  82. 
  83. def run(install_node_control=True):
  84.     rc = runner(sys.argv[1:])
  85.     sys.exit(rc)