import os.path, sys from twisted.python import usage class OptionsMixin(usage.Options): optFlags = [ ["quiet", "q", "Operate silently."], ["version", "V", "Display version numbers and exit."], ] class VDriveOptions(OptionsMixin): 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 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 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.""" subCommands = [ ["ls", None, ListOptions, "List a directory"], ["get", None, GetOptions, "Retrieve a file from the virtual drive."], ["put", None, PutOptions, "Upload a file into the virtual drive."], ] 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 vdrive_filename = config['vdrive_filename'] local_filename = config['local_filename'] rc = tahoe_get.get(config['server'], config['vdrive'], 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) 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 dispatch = { "ls": list, "get": get, "put": put, }