]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/runner.py
ee8415d5914ba2fac5ec60672f6114e8513b5b32
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / runner.py
1
2 import sys
3 from cStringIO import StringIO
4
5 import pkg_resources
6 pkg_resources.require('twisted')
7 from twisted.python import usage
8
9 pkg_resources.require('allmydata-tahoe')
10 from allmydata.scripts.common import BaseOptions
11 import debug, create_node, startstop_node, cli, keygen, stats_gatherer
12
13 def GROUP(s):
14     # Usage.parseOptions compares argv[1] against command[0], so it will
15     # effectively ignore any "subcommand" that starts with a newline. We use
16     # these to insert section headers into the --help output.
17     return [("\n" + s, None, None, None)]
18
19
20 class Options(BaseOptions, usage.Options):
21     synopsis = "Usage:  tahoe <command> [command options]"
22     subCommands = ( GROUP("Administration")
23                     +   create_node.subCommands
24                     +   keygen.subCommands
25                     +   stats_gatherer.subCommands
26                     + GROUP("Controlling a node")
27                     +   startstop_node.subCommands
28                     + GROUP("Debugging")
29                     +   debug.subCommands
30                     + GROUP("Using the filesystem")
31                     +   cli.subCommands
32                     )
33
34     def getUsage(self, **kwargs):
35         t = usage.Options.getUsage(self, **kwargs)
36         return t + "\nPlease run 'tahoe <command> --help' for more details on each command.\n"
37
38     def postOptions(self):
39         if not hasattr(self, 'subOptions'):
40             raise usage.UsageError("must specify a command")
41
42 def runner(argv,
43            run_by_human=True,
44            stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
45            install_node_control=True, additional_commands=None):
46
47     config = Options()
48     if install_node_control:
49         config.subCommands.extend(startstop_node.subCommands)
50
51     ac_dispatch = {}
52     if additional_commands:
53         for ac in additional_commands:
54             config.subCommands.extend(ac.subCommands)
55             ac_dispatch.update(ac.dispatch)
56
57     try:
58         config.parseOptions(argv)
59     except usage.error, e:
60         if not run_by_human:
61             raise
62         c = config
63         while hasattr(c, 'subOptions'):
64             c = c.subOptions
65         print str(c)
66         print "%s:  %s" % (sys.argv[0], e)
67         return 1
68
69     command = config.subCommand
70     so = config.subOptions
71
72     if config['quiet']:
73         stdout = StringIO()
74
75     so.stdout = stdout
76     so.stderr = stderr
77     so.stdin = stdin
78
79     rc = 0
80     if command in create_node.dispatch:
81         for basedir in so.basedirs:
82             f = create_node.dispatch[command]
83             rc = f(basedir, so, stdout, stderr) or rc
84     elif command in startstop_node.dispatch:
85         rc = startstop_node.dispatch[command](so, stdout, stderr)
86     elif command in debug.dispatch:
87         rc = debug.dispatch[command](so)
88     elif command in cli.dispatch:
89         rc = cli.dispatch[command](so)
90     elif command in keygen.dispatch:
91         rc = keygen.dispatch[command](so, stdout, stderr)
92     elif command in stats_gatherer.dispatch:
93         rc = stats_gatherer.dispatch[command](so)
94     elif command in ac_dispatch:
95         rc = ac_dispatch[command](so, stdout, stderr)
96     else:
97         raise usage.UsageError()
98
99     return rc
100
101 def run(install_node_control=True):
102     rc = runner(sys.argv[1:])
103     sys.exit(rc)