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