2007-07-11 02:37:37 +00:00
|
|
|
|
2007-08-17 19:54:47 +00:00
|
|
|
import os.path, re, sys
|
2007-07-11 02:37:37 +00:00
|
|
|
from twisted.python import usage
|
2007-08-16 19:50:19 +00:00
|
|
|
from allmydata.scripts.common import BaseOptions
|
2007-07-11 02:37:37 +00:00
|
|
|
|
2007-08-17 19:54:47 +00:00
|
|
|
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
|
|
|
|
|
2007-08-16 19:50:19 +00:00
|
|
|
class VDriveOptions(BaseOptions, usage.Options):
|
2007-07-11 02:37:37 +00:00
|
|
|
optParameters = [
|
2007-10-11 07:30:36 +00:00
|
|
|
["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. "
|
2008-01-04 00:02:05 +00:00
|
|
|
"It should also contain a file named root_dir.cap which contains "
|
2007-12-03 21:52:42 +00:00
|
|
|
"the root dirnode URI that should be used."
|
2007-10-11 07:30:36 +00:00
|
|
|
],
|
2007-08-16 23:53:27 +00:00
|
|
|
["node-url", "u", None,
|
2007-10-11 07:30:36 +00:00
|
|
|
"URL of the tahoe node to use, a URL like \"http://127.0.0.1:8123\". "
|
|
|
|
"This overrides the URL found in the --node-directory ."],
|
2008-05-20 02:28:50 +00:00
|
|
|
["dir-cap", "r", None,
|
|
|
|
"Which dirnode URI should be used as the 'tahoe' alias."]
|
2007-07-11 02:37:37 +00:00
|
|
|
]
|
|
|
|
|
2007-08-17 19:54:47 +00:00
|
|
|
def postOptions(self):
|
2007-10-11 07:30:36 +00:00
|
|
|
# compute a node-url from the existing options, put in self['node-url']
|
|
|
|
if self['node-directory']:
|
2008-01-11 02:32:18 +00:00
|
|
|
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'] = os.path.expanduser(self['node-directory'])
|
2007-10-11 07:30:36 +00:00
|
|
|
if self['node-url']:
|
|
|
|
if (not isinstance(self['node-url'], basestring)
|
|
|
|
or not NODEURL_RE.match(self['node-url'])):
|
|
|
|
msg = ("--node-url is required to be a string and look like "
|
|
|
|
"\"http://HOSTNAMEORADDR:PORT\", not: %r" %
|
|
|
|
(self['node-url'],))
|
|
|
|
raise usage.UsageError(msg)
|
|
|
|
else:
|
|
|
|
node_url_file = os.path.join(self['node-directory'], "node.url")
|
|
|
|
self['node-url'] = open(node_url_file, "r").read().strip()
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
aliases = self.get_aliases(self['node-directory'])
|
|
|
|
if self['dir-cap']:
|
|
|
|
aliases["tahoe"] = self['dir-cap']
|
|
|
|
self.aliases = aliases # maps alias name to dircap
|
|
|
|
|
|
|
|
|
|
|
|
def get_aliases(self, nodedir):
|
2008-01-04 00:02:05 +00:00
|
|
|
from allmydata import uri
|
2008-05-20 02:28:50 +00:00
|
|
|
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
|
2008-01-04 00:35:35 +00:00
|
|
|
try:
|
2008-05-20 02:28:50 +00:00
|
|
|
f = open(aliasfile, "r")
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("#"):
|
|
|
|
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
|
|
|
|
|
|
|
|
class MakeDirectoryOptions(VDriveOptions):
|
|
|
|
def parseArgs(self, where=""):
|
|
|
|
self.where = where
|
|
|
|
longdesc = """Create a new directory, either unlinked or as a subdirectory."""
|
|
|
|
|
|
|
|
class AddAliasOptions(VDriveOptions):
|
|
|
|
def parseArgs(self, alias, cap):
|
|
|
|
self.alias = alias
|
|
|
|
self.cap = cap
|
2007-10-11 07:30:36 +00:00
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
class ListOptions(VDriveOptions):
|
2008-05-20 02:28:50 +00:00
|
|
|
optFlags = [
|
|
|
|
("long", "l", "Use long format: show file sizes, and timestamps"),
|
|
|
|
("uri", "u", "Show file URIs"),
|
|
|
|
("classify", "F", "Append '/' to directory names, and '*' to mutable"),
|
|
|
|
("json", None, "Show the raw JSON output"),
|
|
|
|
]
|
|
|
|
def parseArgs(self, where=""):
|
|
|
|
self.where = where
|
2007-07-11 02:37:37 +00:00
|
|
|
|
2007-08-16 19:50:19 +00:00
|
|
|
longdesc = """List the contents of some portion of the virtual drive."""
|
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
class GetOptions(VDriveOptions):
|
2008-05-20 02:28:50 +00:00
|
|
|
def parseArgs(self, arg1, arg2=None):
|
|
|
|
# tahoe get FOO |less # write to stdout
|
|
|
|
# tahoe get tahoe:FOO |less # same
|
|
|
|
# tahoe get FOO bar # write to local file
|
|
|
|
# tahoe get tahoe:FOO bar # same
|
|
|
|
|
|
|
|
self.from_file = arg1
|
|
|
|
self.to_file = arg2
|
|
|
|
if self.to_file == "-":
|
|
|
|
self.to_file = None
|
2007-07-11 02:37:37 +00:00
|
|
|
|
2007-07-11 17:26:19 +00:00
|
|
|
def getSynopsis(self):
|
|
|
|
return "%s get VDRIVE_FILE LOCAL_FILE" % (os.path.basename(sys.argv[0]),)
|
|
|
|
|
|
|
|
longdesc = """Retrieve a file from the virtual drive and write it to the
|
2007-08-16 19:15:38 +00:00
|
|
|
local filesystem. If LOCAL_FILE is omitted or '-', the contents of the file
|
2007-07-11 17:26:19 +00:00
|
|
|
will be written to stdout."""
|
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
class PutOptions(VDriveOptions):
|
2008-05-20 02:28:50 +00:00
|
|
|
def parseArgs(self, arg1=None, arg2=None):
|
|
|
|
# cat FILE > tahoe put # create unlinked file from stdin
|
|
|
|
# cat FILE > tahoe put FOO # create tahoe:FOO from stdin
|
|
|
|
# cat FILE > tahoe put tahoe:FOO # same
|
|
|
|
# tahoe put bar FOO # copy local 'bar' to tahoe:FOO
|
|
|
|
# tahoe put bar tahoe:FOO # same
|
|
|
|
|
|
|
|
if arg1 is not None and arg2 is not None:
|
|
|
|
self.from_file = arg1
|
|
|
|
self.to_file = arg2
|
|
|
|
elif arg1 is not None and arg2 is None:
|
|
|
|
self.from_file = None
|
|
|
|
self.to_file = arg1
|
|
|
|
else:
|
|
|
|
self.from_file = arg1
|
|
|
|
self.to_file = arg2
|
|
|
|
if self.from_file == "-":
|
|
|
|
self.from_file = None
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
def getSynopsis(self):
|
2007-08-17 00:47:24 +00:00
|
|
|
return "%s put LOCAL_FILE VDRIVE_FILE" % (os.path.basename(sys.argv[0]),)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
longdesc = """Put a file into the virtual drive (copying the file's
|
2007-08-17 21:59:49 +00:00
|
|
|
contents from the local filesystem). LOCAL_FILE is required to be a
|
|
|
|
local file (it can't be stdin)."""
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-08-17 20:23:16 +00:00
|
|
|
class RmOptions(VDriveOptions):
|
2008-05-20 02:28:50 +00:00
|
|
|
def parseArgs(self, where):
|
|
|
|
self.where = where
|
2007-08-17 20:23:16 +00:00
|
|
|
|
|
|
|
def getSynopsis(self):
|
|
|
|
return "%s rm VE_FILE" % (os.path.basename(sys.argv[0]),)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-10-12 03:31:48 +00:00
|
|
|
class MvOptions(VDriveOptions):
|
|
|
|
def parseArgs(self, frompath, topath):
|
2008-05-20 02:28:50 +00:00
|
|
|
self.from_file = frompath
|
|
|
|
self.to_file = topath
|
2007-10-12 03:31:48 +00:00
|
|
|
|
|
|
|
def getSynopsis(self):
|
|
|
|
return "%s mv FROM TO" % (os.path.basename(sys.argv[0]),)
|
|
|
|
|
2008-01-05 01:34:10 +00:00
|
|
|
class WebopenOptions(VDriveOptions):
|
|
|
|
def parseArgs(self, vdrive_pathname=""):
|
|
|
|
self['vdrive_pathname'] = vdrive_pathname
|
|
|
|
|
|
|
|
longdesc = """Opens a webbrowser to the contents of some portion of the virtual drive."""
|
2007-07-11 02:37:37 +00:00
|
|
|
|
2008-01-10 02:19:52 +00:00
|
|
|
class ReplOptions(usage.Options):
|
|
|
|
pass
|
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
subCommands = [
|
2008-05-20 02:28:50 +00:00
|
|
|
["mkdir", None, MakeDirectoryOptions, "Create a new directory"],
|
|
|
|
["add-alias", None, AddAliasOptions, "Add a new alias cap"],
|
2007-07-11 02:37:37 +00:00
|
|
|
["ls", None, ListOptions, "List a directory"],
|
2007-07-11 17:26:19 +00:00
|
|
|
["get", None, GetOptions, "Retrieve a file from the virtual drive."],
|
2007-08-16 19:15:38 +00:00
|
|
|
["put", None, PutOptions, "Upload a file into the virtual drive."],
|
2007-08-17 20:23:16 +00:00
|
|
|
["rm", None, RmOptions, "Unlink a file or directory in the virtual drive."],
|
2007-10-12 03:31:48 +00:00
|
|
|
["mv", None, MvOptions, "Move a file within the virtual drive."],
|
2008-01-05 01:34:10 +00:00
|
|
|
["webopen", None, WebopenOptions, "Open a webbrowser to the root_dir"],
|
2008-01-10 02:19:52 +00:00
|
|
|
["repl", None, ReplOptions, "Open a python interpreter"],
|
2007-07-11 02:37:37 +00:00
|
|
|
]
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
def mkdir(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_mkdir
|
|
|
|
rc = tahoe_mkdir.mkdir(config['node-url'],
|
|
|
|
config.aliases,
|
|
|
|
config.where,
|
|
|
|
stdout, stderr)
|
|
|
|
return rc
|
|
|
|
|
|
|
|
def add_alias(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_add_alias
|
|
|
|
rc = tahoe_add_alias.add_alias(config['node-directory'],
|
|
|
|
config.alias,
|
|
|
|
config.cap,
|
|
|
|
stdout, stderr)
|
|
|
|
return rc
|
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
def list(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_ls
|
2007-08-16 23:53:27 +00:00
|
|
|
rc = tahoe_ls.list(config['node-url'],
|
2008-05-20 02:28:50 +00:00
|
|
|
config.aliases,
|
|
|
|
config.where,
|
|
|
|
config,
|
2007-10-12 02:20:41 +00:00
|
|
|
stdout, stderr)
|
2007-07-11 02:37:37 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def get(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_get
|
2007-08-16 23:53:27 +00:00
|
|
|
rc = tahoe_get.get(config['node-url'],
|
2008-05-20 02:28:50 +00:00
|
|
|
config.aliases,
|
|
|
|
config.from_file,
|
|
|
|
config.to_file,
|
2007-10-12 02:20:41 +00:00
|
|
|
stdout, stderr)
|
2007-07-11 17:26:19 +00:00
|
|
|
if rc == 0:
|
2008-05-20 02:28:50 +00:00
|
|
|
if config.to_file is None:
|
2007-07-11 17:26:19 +00:00
|
|
|
# be quiet, since the file being written to stdout should be
|
|
|
|
# proof enough that it worked, unless the user is unlucky
|
|
|
|
# enough to have picked an empty file
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print >>stderr, "%s retrieved and written to %s" % \
|
2008-05-20 02:28:50 +00:00
|
|
|
(config.from_file, config.to_file)
|
2007-07-11 02:37:37 +00:00
|
|
|
return rc
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
def put(config, stdout, stderr, stdin=sys.stdin):
|
2007-08-16 19:15:38 +00:00
|
|
|
from allmydata.scripts import tahoe_put
|
|
|
|
if config['quiet']:
|
|
|
|
verbosity = 0
|
|
|
|
else:
|
|
|
|
verbosity = 2
|
2007-08-16 23:53:27 +00:00
|
|
|
rc = tahoe_put.put(config['node-url'],
|
2008-05-20 02:28:50 +00:00
|
|
|
config.aliases,
|
|
|
|
config.from_file,
|
|
|
|
config.to_file,
|
2007-10-12 02:20:41 +00:00
|
|
|
verbosity,
|
2008-05-20 02:28:50 +00:00
|
|
|
stdin, stdout, stderr)
|
2007-08-17 20:23:16 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def rm(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_rm
|
|
|
|
if config['quiet']:
|
|
|
|
verbosity = 0
|
|
|
|
else:
|
|
|
|
verbosity = 2
|
|
|
|
rc = tahoe_rm.rm(config['node-url'],
|
2008-05-20 02:28:50 +00:00
|
|
|
config.aliases,
|
|
|
|
config.where,
|
2007-10-12 02:20:41 +00:00
|
|
|
verbosity,
|
|
|
|
stdout, stderr)
|
2007-08-16 19:15:38 +00:00
|
|
|
return rc
|
|
|
|
|
2007-10-12 03:31:48 +00:00
|
|
|
def mv(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_mv
|
|
|
|
rc = tahoe_mv.mv(config['node-url'],
|
2008-05-20 02:28:50 +00:00
|
|
|
config.aliases,
|
|
|
|
config.from_file,
|
|
|
|
config.to_file,
|
2007-10-12 03:31:48 +00:00
|
|
|
stdout, stderr)
|
|
|
|
return rc
|
|
|
|
|
2008-01-05 01:34:10 +00:00
|
|
|
def webopen(config, stdout, stderr):
|
|
|
|
import urllib, webbrowser
|
|
|
|
nodeurl = config['node-url']
|
|
|
|
if nodeurl[-1] != "/":
|
|
|
|
nodeurl += "/"
|
2008-01-08 17:41:27 +00:00
|
|
|
url = nodeurl + "uri/%s/" % urllib.quote(config['dir-cap'])
|
2008-01-05 01:34:10 +00:00
|
|
|
if config['vdrive_pathname']:
|
|
|
|
url += urllib.quote(config['vdrive_pathname'])
|
|
|
|
webbrowser.open(url)
|
|
|
|
return 0
|
|
|
|
|
2008-01-10 02:19:52 +00:00
|
|
|
def repl(config, stdout, stderr):
|
|
|
|
import code
|
|
|
|
return code.interact()
|
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
dispatch = {
|
2008-05-20 02:28:50 +00:00
|
|
|
"mkdir": mkdir,
|
|
|
|
"add-alias": add_alias,
|
2007-07-11 02:37:37 +00:00
|
|
|
"ls": list,
|
|
|
|
"get": get,
|
2007-08-16 19:15:38 +00:00
|
|
|
"put": put,
|
2007-08-17 20:23:16 +00:00
|
|
|
"rm": rm,
|
2007-10-12 03:31:48 +00:00
|
|
|
"mv": mv,
|
2008-01-05 01:34:10 +00:00
|
|
|
"webopen": webopen,
|
2008-01-10 02:19:52 +00:00
|
|
|
"repl": repl,
|
2007-07-11 02:37:37 +00:00
|
|
|
}
|
|
|
|
|