From: Brian Warner Date: Wed, 11 Jul 2007 02:37:37 +0000 (-0700) Subject: cli: implement 'allmydata-tahoe ls' X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/(%5B%5E?a=commitdiff_plain;h=b419b853ea03deae7747063cba0a90f2ad18c69f;p=tahoe-lafs%2Ftahoe-lafs.git cli: implement 'allmydata-tahoe ls' --- diff --git a/src/allmydata/scripts/cli.py b/src/allmydata/scripts/cli.py new file mode 100644 index 00000000..1e82fe77 --- /dev/null +++ b/src/allmydata/scripts/cli.py @@ -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, + } + diff --git a/src/allmydata/scripts/runner.py b/src/allmydata/scripts/runner.py index deb852b8..eacff4fc 100644 --- a/src/allmydata/scripts/runner.py +++ b/src/allmydata/scripts/runner.py @@ -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 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 index 49f6ff9b..00000000 --- a/src/allmydata/scripts/tahoe-ls.py +++ /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 index 00000000..dd3c1773 --- /dev/null +++ b/src/allmydata/scripts/tahoe_ls.py @@ -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()