tahoe-lafs/src/allmydata/scripts/cli.py

120 lines
4.0 KiB
Python
Raw Normal View History

2007-07-11 02:37:37 +00:00
import os.path, re, sys
2007-07-11 02:37:37 +00:00
from twisted.python import usage
from allmydata.scripts.common import BaseOptions
2007-07-11 02:37:37 +00:00
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
class VDriveOptions(BaseOptions, usage.Options):
2007-07-11 02:37:37 +00:00
optParameters = [
["node-url", "u", None,
"URL of the tahoe node to use, a URL like \"http://127.0.0.1:8123\""],
2007-07-11 02:37:37 +00:00
]
def postOptions(self):
if not isinstance(self['node-url'], basestring) or not NODEURL_RE.match(self['node-url']):
raise usage.UsageError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (self['node-url'],))
2007-07-11 02:37:37 +00:00
class ListOptions(VDriveOptions):
def parseArgs(self, vdrive_pathname=""):
self['vdrive_pathname'] = vdrive_pathname
2007-07-11 02:37:37 +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
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."""
class PutOptions(VDriveOptions):
def parseArgs(self, local_filename, vdrive_filename):
self['local_filename'] = local_filename
self['vdrive_filename'] = vdrive_filename
def getSynopsis(self):
2007-08-17 00:47:24 +00:00
return "%s put LOCAL_FILE VDRIVE_FILE" % (os.path.basename(sys.argv[0]),)
longdesc = """Put a file into the virtual drive (copying the file's
contents from the local filesystem). LOCAL_FILE is required to be a
local file (it can't be stdin)."""
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-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."],
["put", None, PutOptions, "Upload a file into the virtual drive."],
["rm", None, RmOptions, "Unlink a file or directory in the virtual drive."],
2007-07-11 02:37:37 +00:00
]
def list(config, stdout, stderr):
from allmydata.scripts import tahoe_ls
rc = tahoe_ls.list(config['node-url'],
config['vdrive_pathname'])
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']
rc = tahoe_get.get(config['node-url'],
2007-07-11 17:26:19 +00:00
vdrive_filename,
local_filename)
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
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
rc = tahoe_put.put(config['node-url'],
local_filename,
vdrive_filename,
verbosity)
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'],
vdrive_pathname,
verbosity)
return rc
2007-07-11 02:37:37 +00:00
dispatch = {
"ls": list,
"get": get,
"put": put,
"rm": rm,
2007-07-11 02:37:37 +00:00
}