2007-07-11 01:41:52 +00:00
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
import os, sys, urllib
|
2007-07-11 01:41:52 +00:00
|
|
|
from twisted.python import usage
|
|
|
|
|
|
|
|
|
2007-08-16 19:50:19 +00:00
|
|
|
class BaseOptions:
|
2008-08-01 18:46:24 +00:00
|
|
|
# unit tests can override these to point at StringIO instances
|
|
|
|
stdin = sys.stdin
|
|
|
|
stdout = sys.stdout
|
|
|
|
stderr = sys.stderr
|
|
|
|
|
2007-08-16 19:50:19 +00:00
|
|
|
optFlags = [
|
|
|
|
["quiet", "q", "Operate silently."],
|
|
|
|
["version", "V", "Display version numbers and exit."],
|
2009-01-23 22:21:50 +00:00
|
|
|
["version-and-path", None, "Display version numbers and paths to their locations and exit."],
|
2007-08-16 19:50:19 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def opt_version(self):
|
2007-12-13 02:37:37 +00:00
|
|
|
import allmydata
|
|
|
|
print allmydata.get_package_versions_string()
|
2007-08-16 19:50:19 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2009-01-16 19:47:51 +00:00
|
|
|
def opt_version_and_path(self):
|
|
|
|
import allmydata
|
|
|
|
print allmydata.get_package_versions_string(show_paths=True)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2007-08-16 19:50:19 +00:00
|
|
|
|
2007-07-11 01:41:52 +00:00
|
|
|
class BasedirMixin:
|
|
|
|
optFlags = [
|
|
|
|
["multiple", "m", "allow multiple basedirs to be specified at once"],
|
|
|
|
]
|
|
|
|
|
|
|
|
def postOptions(self):
|
|
|
|
if not self.basedirs:
|
|
|
|
raise usage.UsageError("<basedir> parameter is required")
|
|
|
|
if self['basedir']:
|
|
|
|
del self['basedir']
|
2009-01-21 19:47:20 +00:00
|
|
|
self['basedirs'] = [os.path.abspath(os.path.expanduser(b)) for b in self.basedirs]
|
2007-07-11 01:41:52 +00:00
|
|
|
|
|
|
|
def parseArgs(self, *args):
|
2009-01-21 19:47:20 +00:00
|
|
|
from allmydata.util.assertutil import precondition
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs = []
|
|
|
|
if self['basedir']:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(isinstance(self['basedir'], (str, unicode)), self['basedir'])
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs.append(self['basedir'])
|
|
|
|
if self['multiple']:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(not [x for x in args if not isinstance(x, (str, unicode))], args)
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs.extend(args)
|
|
|
|
else:
|
|
|
|
if len(args) == 0 and not self.basedirs:
|
2008-01-11 02:32:18 +00:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
from allmydata.windows import registry
|
2009-01-21 19:47:20 +00:00
|
|
|
rbdp = registry.get_base_dir_path()
|
|
|
|
if rbdp:
|
|
|
|
precondition(isinstance(registry.get_base_dir_path(), (str, unicode)), registry.get_base_dir_path())
|
|
|
|
self.basedirs.append(rbdp)
|
2008-01-11 02:32:18 +00:00
|
|
|
else:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(isinstance(os.path.expanduser("~/.tahoe"), (str, unicode)), os.path.expanduser("~/.tahoe"))
|
2008-01-11 02:32:18 +00:00
|
|
|
self.basedirs.append(os.path.expanduser("~/.tahoe"))
|
2007-07-11 01:41:52 +00:00
|
|
|
if len(args) > 0:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(isinstance(args[0], (str, unicode)), args[0])
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs.append(args[0])
|
|
|
|
if len(args) > 1:
|
|
|
|
raise usage.UsageError("I wasn't expecting so many arguments")
|
|
|
|
|
|
|
|
class NoDefaultBasedirMixin(BasedirMixin):
|
|
|
|
def parseArgs(self, *args):
|
2009-01-21 19:47:20 +00:00
|
|
|
from allmydata.util.assertutil import precondition
|
2007-10-11 08:54:23 +00:00
|
|
|
# create-client won't default to --basedir=~/.tahoe
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs = []
|
|
|
|
if self['basedir']:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(isinstance(self['basedir'], (str, unicode)), self['basedir'])
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs.append(self['basedir'])
|
|
|
|
if self['multiple']:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(not [x for x in args if not isinstance(x, (str, unicode))], args)
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs.extend(args)
|
|
|
|
else:
|
|
|
|
if len(args) > 0:
|
2009-01-21 19:47:20 +00:00
|
|
|
precondition(isinstance(args[0], (str, unicode)), args[0])
|
2007-07-11 01:41:52 +00:00
|
|
|
self.basedirs.append(args[0])
|
|
|
|
if len(args) > 1:
|
|
|
|
raise usage.UsageError("I wasn't expecting so many arguments")
|
|
|
|
if not self.basedirs:
|
|
|
|
raise usage.UsageError("--basedir must be provided")
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
DEFAULT_ALIAS = "tahoe"
|
2007-10-12 05:29:23 +00:00
|
|
|
|
2008-05-20 21:36:04 +00:00
|
|
|
|
|
|
|
def get_aliases(nodedir):
|
|
|
|
from allmydata import uri
|
|
|
|
aliases = {}
|
|
|
|
aliasfile = os.path.join(nodedir, "private", "aliases")
|
|
|
|
rootfile = os.path.join(nodedir, "private", "root_dir.cap")
|
|
|
|
try:
|
|
|
|
f = open(rootfile, "r")
|
|
|
|
rootcap = f.read().strip()
|
|
|
|
if rootcap:
|
|
|
|
aliases["tahoe"] = uri.from_string_dirnode(rootcap).to_string()
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
f = open(aliasfile, "r")
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.strip()
|
2008-08-13 02:50:50 +00:00
|
|
|
if line.startswith("#") or not line:
|
2008-05-20 21:36:04 +00:00
|
|
|
continue
|
|
|
|
name, cap = line.split(":", 1)
|
|
|
|
# normalize it: remove http: prefix, urldecode
|
|
|
|
cap = cap.strip()
|
|
|
|
aliases[name] = uri.from_string_dirnode(cap).to_string()
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
return aliases
|
|
|
|
|
2008-05-20 23:56:03 +00:00
|
|
|
class DefaultAliasMarker:
|
|
|
|
pass
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
def get_alias(aliases, path, default):
|
2008-05-20 23:56:03 +00:00
|
|
|
# transform "work:path/filename" into (aliases["work"], "path/filename").
|
|
|
|
# If default=None, then an empty alias is indicated by returning
|
|
|
|
# DefaultAliasMarker. We special-case "URI:" to make it easy to access
|
|
|
|
# specific files/directories by their read-cap.
|
2008-08-02 02:27:29 +00:00
|
|
|
path = path.strip()
|
2008-05-20 02:28:50 +00:00
|
|
|
if path.startswith("URI:"):
|
|
|
|
# The only way to get a sub-path is to use URI:blah:./foo, and we
|
|
|
|
# strip out the :./ sequence.
|
|
|
|
sep = path.find(":./")
|
|
|
|
if sep != -1:
|
|
|
|
return path[:sep], path[sep+3:]
|
|
|
|
return path, ""
|
|
|
|
colon = path.find(":")
|
|
|
|
if colon == -1:
|
|
|
|
# no alias
|
2008-05-20 23:56:03 +00:00
|
|
|
if default == None:
|
|
|
|
return DefaultAliasMarker, path
|
2008-05-20 02:28:50 +00:00
|
|
|
return aliases[default], path
|
|
|
|
alias = path[:colon]
|
|
|
|
if "/" in alias:
|
|
|
|
# no alias, but there's a colon in a dirname/filename, like
|
|
|
|
# "foo/bar:7"
|
2008-05-20 23:56:03 +00:00
|
|
|
if default == None:
|
|
|
|
return DefaultAliasMarker, path
|
2008-05-20 02:28:50 +00:00
|
|
|
return aliases[default], path
|
|
|
|
return aliases[alias], path[colon+1:]
|
|
|
|
|
|
|
|
def escape_path(path):
|
|
|
|
segments = path.split("/")
|
2008-12-24 16:53:17 +00:00
|
|
|
return "/".join([urllib.quote(s) for s in segments])
|