mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-09 03:44:23 +00:00
cli: implement 'allmydata-tahoe ls'
This commit is contained in:
parent
c98c7ebfd7
commit
b419b853ea
46
src/allmydata/scripts/cli.py
Normal file
46
src/allmydata/scripts/cli.py
Normal file
@ -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,
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
52
src/allmydata/scripts/tahoe_ls.py
Normal file
52
src/allmydata/scripts/tahoe_ls.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user