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

103 lines
3.3 KiB
Python
Raw Normal View History

2007-07-11 02:37:37 +00:00
2007-07-11 17:26:19 +00:00
import os.path, sys
2007-07-11 02:37:37 +00:00
from twisted.python import usage
class OptionsMixin(usage.Options):
optFlags = [
["quiet", "q", "Operate silently."],
["version", "V", "Display version numbers and exit."],
]
class VDriveOptions(OptionsMixin):
2007-07-11 02:37:37 +00:00
optParameters = [
["vdrive", "d", "global",
"which virtual drive to use: 'global' or 'private'"],
["server", "s", "http://tahoebs1.allmydata.com:8011/",
"which vdrive server to use, a URL like http://example.com/"],
]
class ListOptions(VDriveOptions):
def parseArgs(self, vdrive_filename=""):
self['vdrive_filename'] = vdrive_filename
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['vdrive_filename'] = vdrive_filename
self['local_filename'] = local_filename
def getSynopsis(self):
return "%s put LOCAL_FILEVDRI VE_FILE" % (os.path.basename(sys.argv[0]),)
longdesc = """Put a file into the virtual drive (copying the file's
contents from the local filesystem). If LOCAL_FILE is omitted or '-', the
contents of the file will be read from stdin."""
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."],
2007-07-11 02:37:37 +00:00
]
def list(config, stdout, stderr):
from allmydata.scripts import tahoe_ls
rc = tahoe_ls.list(config['server'],
config['vdrive'],
config['vdrive_filename'])
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-07-11 02:37:37 +00:00
rc = tahoe_get.get(config['server'],
config['vdrive'],
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['server'],
config['vdrive'],
vdrive_filename,
local_filename,
verbosity)
return rc
2007-07-11 02:37:37 +00:00
dispatch = {
"ls": list,
"get": get,
"put": put,
2007-07-11 02:37:37 +00:00
}