From: Brian Warner Date: Wed, 11 Jul 2007 17:26:19 +0000 (-0700) Subject: cli: implement 'get' X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/frontends?a=commitdiff_plain;h=9238c61224c13afcb62826336709d232a4e192e5;p=tahoe-lafs%2Ftahoe-lafs.git cli: implement 'get' --- diff --git a/src/allmydata/scripts/cli.py b/src/allmydata/scripts/cli.py index 1e82fe77..d9a079d9 100644 --- a/src/allmydata/scripts/cli.py +++ b/src/allmydata/scripts/cli.py @@ -1,4 +1,5 @@ +import os.path, sys from twisted.python import usage class VDriveOptions(usage.Options): @@ -19,10 +20,17 @@ class GetOptions(VDriveOptions): 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 disk. If LOCAL_FILE is omitted or '-', the contents of the file + will be written to stdout.""" + subCommands = [ ["ls", None, ListOptions, "List a directory"], - ["get", None, GetOptions, "Retrieve a file from the virtual drive"], + ["get", None, GetOptions, "Retrieve a file from the virtual drive."], ] def list(config, stdout, stderr): @@ -34,9 +42,21 @@ def list(config, stdout, stderr): 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'], - config['vdrive_filename']) + 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 dispatch = { diff --git a/src/allmydata/scripts/tahoe-get.py b/src/allmydata/scripts/tahoe-get.py deleted file mode 100644 index fa92d63e..00000000 --- a/src/allmydata/scripts/tahoe-get.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python - -import optparse, sys, urllib - -def GET(url, outf): - f = urllib.urlopen(url) - outf.write(f.read()) - -parser = optparse.OptionParser() -parser.add_option("-d", "--vdrive", dest="vdrive", default="global") -parser.add_option("-s", "--server", dest="server", default="http://tahoebs1.allmydata.com:8011") - -(options, args) = parser.parse_args() - -vfname = args[0] -if len(args) == 1 or args[1] == "-": - targfname = None -else: - targfname = args[1] - -base = options.server -base += "/vdrive/" -base += options.vdrive -base += "/" - -url = base + vfname - -if targfname is None: - GET(url, sys.stdout) -else: - GET(url, open(targfname, "wb")) diff --git a/src/allmydata/scripts/tahoe_get.py b/src/allmydata/scripts/tahoe_get.py new file mode 100644 index 00000000..fd655533 --- /dev/null +++ b/src/allmydata/scripts/tahoe_get.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import sys, urllib + +def get(server, vdrive, vdrive_file, local_file): + + if server[-1] != "/": + server += "/" + url = server + "vdrive/" + vdrive + "/" + if vdrive_file: + url += vdrive_file + + if local_file is None or local_file == "-": + outf = sys.stdout + else: + outf = open(local_file, "wb") + inf = urllib.urlopen(url) + while True: + data = inf.read(4096) + if not data: + break + outf.write(data) + outf.close() + + return 0 + + +def main(): + import optparse + parser = optparse.OptionParser() + parser.add_option("-d", "--vdrive", dest="vdrive", default="global") + parser.add_option("-s", "--server", dest="server", default="http://tahoebs1.allmydata.com:8011") + + (options, args) = parser.parse_args() + + vdrive_file = args[0] + local_file = None + if len(args) > 1: + local_file = args[1] + + get(options.server, options.vdrive, vdrive_file, local_file) + +if __name__ == '__main__': + main()