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