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-01-08 17:41:27 +00:00
|
|
|
["dir-cap", "r", "root",
|
2008-01-04 00:14:19 +00:00
|
|
|
"Which dirnode URI should be used as a root directory. The "
|
2008-01-04 00:02:05 +00:00
|
|
|
"string 'root' is special, and means we should use the "
|
|
|
|
"directory found in the 'root_dir.cap' file in the 'private' "
|
|
|
|
"subdirectory of the --node-directory ."],
|
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-01-04 00:35:35 +00:00
|
|
|
rootdircap = None
|
2008-01-08 17:41:27 +00:00
|
|
|
if self['dir-cap'] == 'root':
|
2008-01-04 00:35:35 +00:00
|
|
|
uri_file = os.path.join(self['node-directory'], 'private', "root_dir.cap")
|
|
|
|
try:
|
|
|
|
rootdircap = open(uri_file, "r").read().strip()
|
|
|
|
except EnvironmentError, le:
|
|
|
|
raise usage.UsageError("\n"
|
2008-01-08 17:41:27 +00:00
|
|
|
"If --dir-cap is absent or is 'root', then the node directory's 'private'\n"
|
2008-04-14 18:43:05 +00:00
|
|
|
"subdirectory is required to contain a file named 'root_dir.cap' which is\n"
|
|
|
|
"required to contain a dir cap, but when we tried to open that file we got:\n"
|
2008-01-04 00:35:35 +00:00
|
|
|
"'%s'." % (le,))
|
|
|
|
else:
|
2008-01-08 17:41:27 +00:00
|
|
|
rootdircap = self['dir-cap']
|
2008-01-04 00:02:05 +00:00
|
|
|
from allmydata import uri
|
2008-01-04 00:35:35 +00:00
|
|
|
try:
|
|
|
|
parsed = uri.NewDirectoryURI.init_from_human_encoding(rootdircap)
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
parsed = uri.ReadonlyNewDirectoryURI.init_from_human_encoding(rootdircap)
|
|
|
|
except:
|
2008-01-08 17:41:27 +00:00
|
|
|
if self['dir-cap'] == 'root':
|
2008-01-04 00:35:35 +00:00
|
|
|
raise usage.UsageError("\n"
|
2008-01-08 17:41:27 +00:00
|
|
|
"If --dir-cap is absent or is 'root', then the node directory's 'private'\n"
|
2008-01-04 00:35:35 +00:00
|
|
|
"subdirectory's 'root_dir.cap' is required to contain a dir cap, but we found\n"
|
|
|
|
"'%s'." % (rootdircap,))
|
|
|
|
else:
|
2008-04-14 18:43:05 +00:00
|
|
|
raise usage.UsageError("--dir-cap is required to be a dir cap (or \"root\"), but we got '%s'." % (self['dir-cap'],))
|
2007-10-11 07:30:36 +00:00
|
|
|
|
2008-01-08 17:41:27 +00:00
|
|
|
self['dir-cap'] = parsed.to_string()
|
2007-10-11 07:30:36 +00:00
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
class ListOptions(VDriveOptions):
|
2007-08-17 20:23:16 +00:00
|
|
|
def parseArgs(self, vdrive_pathname=""):
|
|
|
|
self['vdrive_pathname'] = vdrive_pathname
|
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):
|
|
|
|
def parseArgs(self, vdrive_filename, local_filename="-"):
|
|
|
|
self['vdrive_filename'] = vdrive_filename
|
|
|
|
self['local_filename'] = local_filename
|
|
|
|
|
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):
|
|
|
|
def parseArgs(self, local_filename, vdrive_filename):
|
|
|
|
self['local_filename'] = local_filename
|
2007-08-17 20:23:16 +00:00
|
|
|
self['vdrive_filename'] = vdrive_filename
|
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):
|
|
|
|
def parseArgs(self, vdrive_pathname):
|
|
|
|
self['vdrive_pathname'] = vdrive_pathname
|
|
|
|
|
|
|
|
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):
|
|
|
|
self['from'] = frompath
|
|
|
|
self['to'] = topath
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
["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
|
|
|
]
|
|
|
|
|
|
|
|
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-01-08 17:41:27 +00:00
|
|
|
config['dir-cap'],
|
2007-10-12 02:20:41 +00:00
|
|
|
config['vdrive_pathname'],
|
|
|
|
stdout, stderr)
|
2007-07-11 02:37:37 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def get(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_get
|
2007-07-11 17:26:19 +00:00
|
|
|
vdrive_filename = config['vdrive_filename']
|
|
|
|
local_filename = config['local_filename']
|
2007-08-16 23:53:27 +00:00
|
|
|
rc = tahoe_get.get(config['node-url'],
|
2008-01-08 17:41:27 +00:00
|
|
|
config['dir-cap'],
|
2007-07-11 17:26:19 +00:00
|
|
|
vdrive_filename,
|
2007-10-12 02:20:41 +00:00
|
|
|
local_filename,
|
|
|
|
stdout, stderr)
|
2007-07-11 17:26:19 +00:00
|
|
|
if rc == 0:
|
|
|
|
if local_filename is None or local_filename == "-":
|
|
|
|
# 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" % \
|
|
|
|
(vdrive_filename, local_filename)
|
2007-07-11 02:37:37 +00:00
|
|
|
return rc
|
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
def put(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_put
|
|
|
|
vdrive_filename = config['vdrive_filename']
|
|
|
|
local_filename = config['local_filename']
|
|
|
|
if config['quiet']:
|
|
|
|
verbosity = 0
|
|
|
|
else:
|
|
|
|
verbosity = 2
|
2007-08-16 23:53:27 +00:00
|
|
|
rc = tahoe_put.put(config['node-url'],
|
2008-01-08 17:41:27 +00:00
|
|
|
config['dir-cap'],
|
2007-08-16 19:15:38 +00:00
|
|
|
local_filename,
|
2007-08-17 20:23:16 +00:00
|
|
|
vdrive_filename,
|
2007-10-12 02:20:41 +00:00
|
|
|
verbosity,
|
|
|
|
stdout, stderr)
|
2007-08-17 20:23:16 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def rm(config, stdout, stderr):
|
|
|
|
from allmydata.scripts import tahoe_rm
|
|
|
|
vdrive_pathname = config['vdrive_pathname']
|
|
|
|
if config['quiet']:
|
|
|
|
verbosity = 0
|
|
|
|
else:
|
|
|
|
verbosity = 2
|
|
|
|
rc = tahoe_rm.rm(config['node-url'],
|
2008-01-08 17:41:27 +00:00
|
|
|
config['dir-cap'],
|
2007-10-11 07:30:36 +00:00
|
|
|
vdrive_pathname,
|
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
|
|
|
|
frompath = config['from']
|
|
|
|
topath = config['to']
|
|
|
|
rc = tahoe_mv.mv(config['node-url'],
|
2008-01-08 17:41:27 +00:00
|
|
|
config['dir-cap'],
|
2007-10-12 03:31:48 +00:00
|
|
|
frompath,
|
|
|
|
topath,
|
|
|
|
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 = {
|
|
|
|
"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
|
|
|
}
|
|
|
|
|