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