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

diff --git a/src/allmydata/scripts/cli.py b/src/allmydata/scripts/cli.py
new file mode 100644 (file)
index 0000000..1e82fe7
--- /dev/null
@@ -0,0 +1,46 @@
+
+from twisted.python import usage
+
+class VDriveOptions(usage.Options):
+    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
+
+
+subCommands = [
+    ["ls", None, ListOptions, "List a directory"],
+    ["get", None, GetOptions, "Retrieve a file from 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
+    rc = tahoe_get.get(config['server'],
+                       config['vdrive'],
+                       config['vdrive_filename'])
+    return rc
+
+dispatch = {
+    "ls": list,
+    "get": get,
+    }
+
index deb852b844482dfa800a7fc2c4be8dc92e3749b1..eacff4fcd50181e91734921cd5c26d0ee251f464 100644 (file)
@@ -3,7 +3,7 @@ import sys
 from cStringIO import StringIO
 from twisted.python import usage
 
-from allmydata.scripts import debug, create_node, startstop_node
+from allmydata.scripts import debug, create_node, startstop_node, cli
 
 class Options(usage.Options):
     synopsis = "Usage:  allmydata <command> [command options]"
@@ -16,6 +16,7 @@ class Options(usage.Options):
     subCommands += create_node.subCommands
     subCommands += startstop_node.subCommands
     subCommands += debug.subCommands
+    subCommands += cli.subCommands
 
     def postOptions(self):
         if not hasattr(self, 'subOptions'):
@@ -49,6 +50,8 @@ def runner(argv, run_by_human=True, stdout=sys.stdout, stderr=sys.stderr):
         rc = startstop_node.dispatch[command](so, stdout, stderr)
     elif command in debug.dispatch:
         rc = debug.dispatch[command](so, stdout, stderr)
+    elif command in cli.dispatch:
+        rc = cli.dispatch[command](so, stdout, stderr)
 
     return rc
 
diff --git a/src/allmydata/scripts/tahoe-ls.py b/src/allmydata/scripts/tahoe-ls.py
deleted file mode 100644 (file)
index 49f6ff9..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-#! /usr/bin/python
-
-import optparse, urllib
-import simplejson
-
-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()
-
-
-url = options.server + "/vdrive/" + options.vdrive
-if args:
-    url += "/" + args[0]
-url += "?t=json"
-data = urllib.urlopen(url).read()
-
-parsed = simplejson.loads(data)
-nodetype, d = parsed
-if nodetype == "dirnode":
-    childnames = sorted(d['children'].keys())
-    for name in childnames:
-        child = d['children'][name]
-        childtype = child[0]
-        if childtype == "dirnode":
-            print "%10s %s/" % ("", name)
-        else:
-            assert childtype == "filenode"
-            size = child[1]['size']
-            print "%10s %s" % (size, name)
-
-
diff --git a/src/allmydata/scripts/tahoe_ls.py b/src/allmydata/scripts/tahoe_ls.py
new file mode 100644 (file)
index 0000000..dd3c177
--- /dev/null
@@ -0,0 +1,52 @@
+#! /usr/bin/python
+
+import urllib
+import simplejson
+
+def GET(url, outf):
+    f = urllib.urlopen(url)
+    outf.write(f.read())
+
+def list(server, vdrive, vdrive_file):
+
+    if server[-1] != "/":
+        server += "/"
+    url = server + "vdrive/" + vdrive + "/"
+    if vdrive_file:
+        url += vdrive_file
+    url += "?t=json"
+    print "URL:", url
+    data = urllib.urlopen(url).read()
+
+    parsed = simplejson.loads(data)
+    nodetype, d = parsed
+    if nodetype == "dirnode":
+        childnames = sorted(d['children'].keys())
+        for name in childnames:
+            child = d['children'][name]
+            childtype = child[0]
+            if childtype == "dirnode":
+                print "%10s %s/" % ("", name)
+            else:
+                assert childtype == "filenode"
+                size = child[1]['size']
+                print "%10s %s" % (size, name)
+
+
+
+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 = ""
+    if args:
+        vdrive_file = args[0]
+
+    list(options.server, options.vdrive, vdrive_file)
+
+if __name__ == '__main__':
+    main()