]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
cli: implement 'get'
authorBrian Warner <warner@allmydata.com>
Wed, 11 Jul 2007 17:26:19 +0000 (10:26 -0700)
committerBrian Warner <warner@allmydata.com>
Wed, 11 Jul 2007 17:26:19 +0000 (10:26 -0700)
src/allmydata/scripts/cli.py
src/allmydata/scripts/tahoe-get.py [deleted file]
src/allmydata/scripts/tahoe_get.py [new file with mode: 0644]

index 1e82fe77138f482239972ff711738f16485d063f..d9a079d9acb56753e44be6a789c9aca1ef76cf6b 100644 (file)
@@ -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 (file)
index fa92d63..0000000
+++ /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 (file)
index 0000000..fd65553
--- /dev/null
@@ -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()