import os.path, re, sys, fnmatch
from twisted.python import usage
-from allmydata.scripts.common import BaseOptions, get_aliases
-from allmydata.util.encodingutil import argv_to_unicode, argv_to_abspath
+from allmydata.scripts.common import BaseOptions, get_aliases, get_default_nodedir, DEFAULT_ALIAS
+from allmydata.util.encodingutil import argv_to_unicode, argv_to_abspath, quote_output
NODEURL_RE=re.compile("http(s?)://([^:]*)(:([1-9][0-9]*))?")
-class VDriveOptions(BaseOptions, usage.Options):
+_default_nodedir = get_default_nodedir()
+
+class VDriveOptions(BaseOptions):
optParameters = [
- ["node-directory", "d", "~/.tahoe",
- "Look here to find out which Tahoe node should be used for all "
- "operations. The directory should either contain a full Tahoe node, "
- "or a file named node.url which points to some other Tahoe node. "
- "It should also contain a file named private/aliases which contains "
- "the mapping from alias name to root dirnode URI."
- ],
+ ["node-directory", "d", None,
+ "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 private/aliases which contains the mapping from alias name "
+ "to root dirnode URI." + (
+ _default_nodedir and (" [default for most commands: " + quote_output(_default_nodedir) + "]") or "")],
["node-url", "u", None,
"URL of the tahoe node to use, a URL like \"http://127.0.0.1:3456\". "
"This overrides the URL found in the --node-directory ."],
]
def postOptions(self):
- # compute a node-url from the existing options, put in self['node-url']
if self['node-directory']:
- if sys.platform == 'win32' and self['node-directory'] == '~/.tahoe':
- from allmydata.windows import registry
- self['node-directory'] = registry.get_base_dir_path()
- else:
- self['node-directory'] = argv_to_abspath(self['node-directory'])
+ self['node-directory'] = argv_to_abspath(self['node-directory'])
+ else:
+ self['node-directory'] = _default_nodedir
+
+ # compute a node-url from the existing options, put in self['node-url']
if self['node-url']:
if (not isinstance(self['node-url'], basestring)
or not NODEURL_RE.match(self['node-url'])):
aliases = get_aliases(self['node-directory'])
if self['dir-cap']:
- aliases["tahoe"] = self['dir-cap']
+ aliases[DEFAULT_ALIAS] = self['dir-cap']
self.aliases = aliases # maps alias name to dircap
from allmydata.util.encodingutil import unicode_to_url, quote_output, argv_to_abspath
from allmydata.util.fileutil import abspath_expanduser_unicode
-class BaseOptions:
+
+_default_nodedir = None
+if sys.platform == 'win32':
+ from allmydata.windows import registry
+ path = registry.get_base_dir_path()
+ if path:
+ precondition(isinstance(path, unicode), path)
+ _default_nodedir = abspath_expanduser_unicode(path)
+
+if _default_nodedir is None:
+ path = abspath_expanduser_unicode(u"~/.tahoe")
+ precondition(isinstance(path, unicode), path)
+ _default_nodedir = path
+
+def get_default_nodedir():
+ return _default_nodedir
+
+
+class BaseOptions(usage.Options):
# unit tests can override these to point at StringIO instances
stdin = sys.stdin
stdout = sys.stdout
["quiet", "q", "Operate silently."],
["version", "V", "Display version numbers and exit."],
["version-and-path", None, "Display version numbers and paths to their locations and exit."],
- ]
+ ]
+ optParameters = [
+ ["node-directory", "d", None, "Specify which Tahoe node directory should be used." + (
+ _default_nodedir and (" [default for most commands: " + quote_output(_default_nodedir) + "]") or "")],
+ ]
def opt_version(self):
import allmydata
class BasedirMixin:
- optFlags = [
- ["multiple", "m", "allow multiple basedirs to be specified at once"],
- ]
+ default_nodedir = _default_nodedir
+ allow_multiple = True
- def postOptions(self):
- if not self.basedirs:
- raise usage.UsageError("<basedir> parameter is required")
- if self['basedir']:
- del self['basedir']
- self['basedirs'] = self.basedirs
+ optParameters = [
+ ["basedir", "C", None, "Same as --node-directory."],
+ ]
+ optFlags = [
+ ["multiple", "m", "Specify multiple node directories at once"],
+ ]
def parseArgs(self, *args):
- self.basedirs = []
- if self['basedir']:
- precondition(isinstance(self['basedir'], str), self['basedir'])
- self.basedirs.append(argv_to_abspath(self['basedir']))
- if self['multiple']:
- self.basedirs.extend(map(argv_to_abspath, args))
+ if self['node-directory'] and self['basedir']:
+ raise usage.UsageError("The --node-directory (or -d) and --basedir (or -C) "
+ "options cannot both be used.")
+
+ if self['node-directory'] or self['basedir']:
+ self.basedirs = [argv_to_abspath(self['node-directory'] or self['basedir'])]
else:
- if len(args) == 0 and not self.basedirs:
- if sys.platform == 'win32':
- from allmydata.windows import registry
- rbdp = registry.get_base_dir_path()
- if rbdp:
- precondition(isinstance(registry.get_base_dir_path(), unicode), registry.get_base_dir_path())
- self.basedirs.append(rbdp)
- else:
- self.basedirs.append(abspath_expanduser_unicode(u"~/.tahoe"))
- if len(args) > 0:
- self.basedirs.append(argv_to_abspath(args[0]))
- if len(args) > 1:
- raise usage.UsageError("I wasn't expecting so many arguments")
+ self.basedirs = []
-class NoDefaultBasedirMixin(BasedirMixin):
- def parseArgs(self, *args):
- # create-client won't default to --basedir=~/.tahoe
- self.basedirs = []
- if self['basedir']:
- self.basedirs.append(argv_to_abspath(self['basedir']))
- if self['multiple']:
+ if self.allow_multiple and self['multiple']:
self.basedirs.extend(map(argv_to_abspath, args))
else:
if len(args) > 0:
self.basedirs.append(argv_to_abspath(args[0]))
if len(args) > 1:
- raise usage.UsageError("I wasn't expecting so many arguments")
+ raise usage.UsageError("I wasn't expecting so many arguments." +
+ (self.allow_multiple and
+ " Use the --multiple option to specify more than one node directory." or ""))
+
+ if len(args) == 0 and self.default_nodedir and not self.basedirs:
+ self.basedirs.append(self.default_nodedir)
+ elif len(args) > 0:
+ self.basedirs.append(argv_to_abspath(args[0]))
+
+ def postOptions(self):
if not self.basedirs:
- raise usage.UsageError("--basedir must be provided")
+ raise usage.UsageError("A base directory for the node must be provided.")
+ del self['basedir']
+ self['basedirs'] = self.basedirs
+
DEFAULT_ALIAS = u"tahoe"
f = open(rootfile, "r")
rootcap = f.read().strip()
if rootcap:
- aliases[u"tahoe"] = uri.from_string_dirnode(rootcap).to_string()
+ aliases[DEFAULT_ALIAS] = uri.from_string_dirnode(rootcap).to_string()
except EnvironmentError:
pass
try:
import os, sys
-from twisted.python import usage
-from allmydata.scripts.common import BasedirMixin, NoDefaultBasedirMixin
+from allmydata.scripts.common import BasedirMixin, BaseOptions
from allmydata.util.assertutil import precondition
from allmydata.util.encodingutil import listdir_unicode, argv_to_unicode, quote_output
import allmydata
-class CreateClientOptions(BasedirMixin, usage.Options):
+class CreateClientOptions(BasedirMixin, BaseOptions):
optParameters = [
("basedir", "C", None, "which directory to create the node in"),
# we provide 'create-node'-time options for the most common
("no-storage", None, "do not offer storage service to other nodes"),
]
-class CreateIntroducerOptions(NoDefaultBasedirMixin, usage.Options):
+class CreateIntroducerOptions(BasedirMixin, BaseOptions):
+ default_nodedir = None
+
optParameters = [
["basedir", "C", None, "which directory to create the introducer in"],
]
import os, sys
-from twisted.python import usage
-#from allmydata.scripts.common import BasedirMixin, NoDefaultBasedirMixin
-from allmydata.util.encodingutil import listdir_unicode, argv_to_abspath, quote_output
+from allmydata.scripts.common import BasedirMixin, BaseOptions
+from allmydata.util.encodingutil import listdir_unicode, quote_output
+
+class CreateKeyGeneratorOptions(BasedirMixin, BaseOptions):
+ default_nodedir = None
+ allow_multiple = False
-class CreateKeyGeneratorOptions(usage.Options):
optParameters = [
["basedir", "C", None, "which directory to create the key-generator in"],
- ]
+ ]
keygen_tac = """
# -*- python -*-
"""
def create_key_generator(config, out=sys.stdout, err=sys.stderr):
- if not config['basedir']:
- print >>err, "a basedir was not provided, please use --basedir or -C"
- return -1
- basedir = argv_to_abspath(config['basedir'])
+ basedir = config['basedirs'][0]
if os.path.exists(basedir):
if listdir_unicode(basedir):
print >>err, "The base directory %s is not empty." % quote_output(basedir)
import os, sys, signal, time
-from twisted.python import usage
-from allmydata.scripts.common import BasedirMixin
+from allmydata.scripts.common import BasedirMixin, BaseOptions
from allmydata.util import fileutil, find_exe
-class StartOptions(BasedirMixin, usage.Options):
+class StartOptions(BasedirMixin, BaseOptions):
optParameters = [
["basedir", "C", None, "which directory to start the node in"],
]
["syslog", None, "tell the node to log to syslog, not a file"],
]
-class StopOptions(BasedirMixin, usage.Options):
+class StopOptions(BasedirMixin, BaseOptions):
optParameters = [
["basedir", "C", None, "which directory to stop the node in"],
]
-class RestartOptions(BasedirMixin, usage.Options):
+class RestartOptions(BasedirMixin, BaseOptions):
optParameters = [
["basedir", "C", None, "which directory to restart the node in"],
]
["syslog", None, "tell the node to log to syslog, not a file"],
]
-class RunOptions(usage.Options):
+class RunOptions(BasedirMixin, BaseOptions):
+ default_nodedir = u"."
+
optParameters = [
["basedir", "C", None, "which directory to run the node in, CWD by default"],
]
# make sure it rejects a missing basedir specification
argv = ["create-key-generator"]
- rc, out, err = self.run_tahoe(argv)
- self.failIfEqual(rc, 0, str((out, err, rc)))
- self.failUnlessEqual(out, "")
- self.failUnless("a basedir was not provided" in err)
+ self.failUnlessRaises(usage.UsageError,
+ runner.runner, argv,
+ run_by_human=False)
def test_stats_gatherer(self):
basedir = self.workdir("test_stats_gatherer")