2006-12-05 19:25:23 +00:00
|
|
|
|
2012-06-18 17:43:49 +00:00
|
|
|
import os, sys
|
2016-10-19 05:17:48 +00:00
|
|
|
from six.moves import StringIO
|
2008-11-12 22:25:03 +00:00
|
|
|
|
2006-12-05 19:25:23 +00:00
|
|
|
from twisted.python import usage
|
2016-09-09 21:25:10 +00:00
|
|
|
from twisted.internet import defer, task, threads
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2012-06-18 17:43:49 +00:00
|
|
|
from allmydata.scripts.common import get_default_nodedir
|
2015-10-01 21:40:10 +00:00
|
|
|
from allmydata.scripts import debug, create_node, startstop_node, cli, \
|
|
|
|
stats_gatherer, admin, magic_folder_cli
|
2015-02-04 00:10:21 +00:00
|
|
|
from allmydata.util.encodingutil import quote_output, quote_local_unicode_path, get_io_encoding
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2010-01-27 22:37:58 +00:00
|
|
|
def GROUP(s):
|
|
|
|
# Usage.parseOptions compares argv[1] against command[0], so it will
|
|
|
|
# effectively ignore any "subcommand" that starts with a newline. We use
|
|
|
|
# these to insert section headers into the --help output.
|
2015-05-26 15:11:44 +00:00
|
|
|
return [("\n(%s)" % s, None, None, None)]
|
2010-01-26 04:45:59 +00:00
|
|
|
|
2008-01-09 02:51:18 +00:00
|
|
|
|
2012-06-18 17:43:49 +00:00
|
|
|
_default_nodedir = get_default_nodedir()
|
|
|
|
|
|
|
|
NODEDIR_HELP = ("Specify which Tahoe node directory should be used. The "
|
|
|
|
"directory should either contain a full Tahoe node, or a "
|
|
|
|
"file named node.url that points to some other Tahoe node. "
|
|
|
|
"It should also contain a file named '"
|
|
|
|
+ os.path.join('private', 'aliases') +
|
|
|
|
"' which contains the mapping from alias name to root "
|
|
|
|
"dirnode URI.")
|
|
|
|
if _default_nodedir:
|
2015-02-04 00:10:21 +00:00
|
|
|
NODEDIR_HELP += " [default for most commands: " + quote_local_unicode_path(_default_nodedir) + "]"
|
2012-06-18 17:43:49 +00:00
|
|
|
|
|
|
|
class Options(usage.Options):
|
|
|
|
# unit tests can override these to point at StringIO instances
|
|
|
|
stdin = sys.stdin
|
|
|
|
stdout = sys.stdout
|
|
|
|
stderr = sys.stderr
|
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
synopsis = "\nUsage: tahoe <command> [command options]"
|
2010-01-27 22:37:58 +00:00
|
|
|
subCommands = ( GROUP("Administration")
|
|
|
|
+ create_node.subCommands
|
|
|
|
+ stats_gatherer.subCommands
|
2012-03-12 22:02:58 +00:00
|
|
|
+ admin.subCommands
|
2010-01-27 22:37:58 +00:00
|
|
|
+ GROUP("Controlling a node")
|
|
|
|
+ startstop_node.subCommands
|
|
|
|
+ GROUP("Debugging")
|
|
|
|
+ debug.subCommands
|
2014-12-01 21:48:28 +00:00
|
|
|
+ GROUP("Using the file store")
|
2010-01-27 22:37:58 +00:00
|
|
|
+ cli.subCommands
|
2015-10-01 21:40:10 +00:00
|
|
|
+ magic_folder_cli.subCommands
|
2010-01-27 22:37:58 +00:00
|
|
|
)
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2012-06-18 17:43:49 +00:00
|
|
|
optFlags = [
|
|
|
|
["quiet", "q", "Operate silently."],
|
|
|
|
["version", "V", "Display version numbers."],
|
|
|
|
["version-and-path", None, "Display version numbers and paths to their locations."],
|
|
|
|
]
|
|
|
|
optParameters = [
|
|
|
|
["node-directory", "d", None, NODEDIR_HELP],
|
|
|
|
]
|
|
|
|
|
|
|
|
def opt_version(self):
|
|
|
|
import allmydata
|
|
|
|
print >>self.stdout, allmydata.get_package_versions_string(debug=True)
|
|
|
|
self.no_command_needed = True
|
|
|
|
|
|
|
|
def opt_version_and_path(self):
|
|
|
|
import allmydata
|
|
|
|
print >>self.stdout, allmydata.get_package_versions_string(show_paths=True, debug=True)
|
|
|
|
self.no_command_needed = True
|
|
|
|
|
2015-05-26 18:29:49 +00:00
|
|
|
def __str__(self):
|
|
|
|
return ("\nUsage: tahoe [global-options] <command> [command-options]\n"
|
|
|
|
+ self.getUsage())
|
|
|
|
|
2015-05-26 18:31:06 +00:00
|
|
|
synopsis = "\nUsage: tahoe [global-options]" # used only for subcommands
|
2012-06-18 17:43:49 +00:00
|
|
|
|
2010-01-26 04:45:59 +00:00
|
|
|
def getUsage(self, **kwargs):
|
2010-01-27 22:37:58 +00:00
|
|
|
t = usage.Options.getUsage(self, **kwargs)
|
2015-05-26 18:29:49 +00:00
|
|
|
t = t.replace("Options:", "\nGlobal options:", 1)
|
2010-01-26 04:45:59 +00:00
|
|
|
return t + "\nPlease run 'tahoe <command> --help' for more details on each command.\n"
|
2006-12-05 19:25:23 +00:00
|
|
|
|
|
|
|
def postOptions(self):
|
|
|
|
if not hasattr(self, 'subOptions'):
|
2011-01-21 07:59:13 +00:00
|
|
|
if not hasattr(self, 'no_command_needed'):
|
|
|
|
raise usage.UsageError("must specify a command")
|
|
|
|
sys.exit(0)
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2010-08-03 08:54:16 +00:00
|
|
|
|
|
|
|
create_dispatch = {}
|
2016-04-28 07:05:30 +00:00
|
|
|
for module in (create_node, stats_gatherer):
|
2010-08-03 08:54:16 +00:00
|
|
|
create_dispatch.update(module.dispatch)
|
|
|
|
|
2016-09-09 21:25:10 +00:00
|
|
|
def parse_options(argv, config=None):
|
|
|
|
if not config:
|
|
|
|
config = Options()
|
|
|
|
config.parseOptions(argv) # may raise usage.error
|
|
|
|
return config
|
2010-07-25 08:32:16 +00:00
|
|
|
|
2016-09-09 21:25:10 +00:00
|
|
|
def parse_or_exit_with_explanation(argv, stdout=sys.stdout):
|
2008-02-20 00:05:14 +00:00
|
|
|
config = Options()
|
2006-12-05 19:25:23 +00:00
|
|
|
try:
|
2016-09-09 21:25:10 +00:00
|
|
|
parse_options(argv, config=config)
|
2006-12-05 19:25:23 +00:00
|
|
|
except usage.error, e:
|
2008-08-12 20:37:32 +00:00
|
|
|
c = config
|
|
|
|
while hasattr(c, 'subOptions'):
|
|
|
|
c = c.subOptions
|
2010-07-25 08:32:16 +00:00
|
|
|
print >>stdout, str(c)
|
|
|
|
try:
|
2011-06-29 18:53:56 +00:00
|
|
|
msg = e.args[0].decode(get_io_encoding())
|
2010-07-25 08:32:16 +00:00
|
|
|
except Exception:
|
|
|
|
msg = repr(e)
|
|
|
|
print >>stdout, "%s: %s\n" % (sys.argv[0], quote_output(msg, quotemarks=False))
|
2016-09-09 21:25:10 +00:00
|
|
|
sys.exit(1)
|
|
|
|
return config
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2016-09-09 21:25:10 +00:00
|
|
|
def dispatch(config,
|
|
|
|
stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr):
|
2006-12-05 19:25:23 +00:00
|
|
|
command = config.subCommand
|
|
|
|
so = config.subOptions
|
2007-06-26 23:19:18 +00:00
|
|
|
if config['quiet']:
|
|
|
|
stdout = StringIO()
|
2008-08-01 18:46:24 +00:00
|
|
|
so.stdout = stdout
|
|
|
|
so.stderr = stderr
|
|
|
|
so.stdin = stdin
|
|
|
|
|
2010-08-03 08:54:16 +00:00
|
|
|
if command in create_dispatch:
|
2016-09-08 09:21:04 +00:00
|
|
|
f = create_dispatch[command]
|
2007-07-11 02:05:18 +00:00
|
|
|
elif command in startstop_node.dispatch:
|
2016-09-08 09:21:04 +00:00
|
|
|
f = startstop_node.dispatch[command]
|
2007-07-11 01:41:52 +00:00
|
|
|
elif command in debug.dispatch:
|
2016-09-08 09:21:04 +00:00
|
|
|
f = debug.dispatch[command]
|
2012-03-12 22:02:58 +00:00
|
|
|
elif command in admin.dispatch:
|
2016-09-08 09:21:04 +00:00
|
|
|
f = admin.dispatch[command]
|
2007-07-11 02:37:37 +00:00
|
|
|
elif command in cli.dispatch:
|
2016-09-09 21:25:10 +00:00
|
|
|
# these are blocking, and must be run in a thread
|
|
|
|
f0 = cli.dispatch[command]
|
|
|
|
f = lambda so: threads.deferToThread(f0, so)
|
2015-10-01 21:40:10 +00:00
|
|
|
elif command in magic_folder_cli.dispatch:
|
2016-09-09 21:25:10 +00:00
|
|
|
# same
|
|
|
|
f0 = magic_folder_cli.dispatch[command]
|
|
|
|
f = lambda so: threads.deferToThread(f0, so)
|
2007-08-17 20:23:16 +00:00
|
|
|
else:
|
|
|
|
raise usage.UsageError()
|
2007-07-11 01:41:52 +00:00
|
|
|
|
2016-09-09 21:25:10 +00:00
|
|
|
d = defer.maybeDeferred(f, so)
|
|
|
|
# the calling convention for CLI dispatch functions is that they either:
|
|
|
|
# 1: succeed and return rc=0
|
|
|
|
# 2: print explanation to stderr and return rc!=0
|
|
|
|
# 3: raise an exception that should just be printed normally
|
|
|
|
# 4: return a Deferred that does 1 or 2 or 3
|
|
|
|
def _raise_sys_exit(rc):
|
|
|
|
sys.exit(rc)
|
|
|
|
d.addCallback(_raise_sys_exit)
|
|
|
|
return d
|
2010-07-25 08:32:16 +00:00
|
|
|
|
2016-09-08 07:02:15 +00:00
|
|
|
def run():
|
2016-09-09 21:25:10 +00:00
|
|
|
assert sys.version_info < (3,), ur"Tahoe-LAFS does not run under Python 3. Please use Python 2.7.x."
|
2010-07-25 08:32:16 +00:00
|
|
|
|
2016-09-09 21:25:10 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
from allmydata.windows.fixups import initialize
|
|
|
|
initialize()
|
|
|
|
d = defer.maybeDeferred(parse_or_exit_with_explanation, sys.argv[1:])
|
|
|
|
d.addCallback(dispatch)
|
|
|
|
def _show_exception(f):
|
|
|
|
# when task.react() notices a non-SystemExit exception, it does
|
|
|
|
# log.err() with the failure and then exits with rc=1. We want this
|
|
|
|
# to actually print the exception to stderr, like it would do if we
|
|
|
|
# weren't using react().
|
|
|
|
if f.check(SystemExit):
|
|
|
|
return f # dispatch function handled it
|
|
|
|
f.printTraceback(file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
d.addErrback(_show_exception)
|
|
|
|
task.react(lambda _reactor: d) # doesn't return: calls sys.exit(rc)
|
2016-04-19 16:22:34 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run()
|