tahoe-lafs/src/allmydata/scripts/tahoe_ls.py

54 lines
1.6 KiB
Python
Raw Normal View History

2007-07-11 02:37:37 +00:00
#! /usr/bin/python
import urllib
import simplejson
2007-10-12 02:20:41 +00:00
def list(nodeurl, root_uri, vdrive_pathname, stdout, stderr):
if nodeurl[-1] != "/":
nodeurl += "/"
2007-10-11 08:34:44 +00:00
url = nodeurl + "uri/%s/" % urllib.quote(root_uri.replace("/","!"))
if vdrive_pathname:
url += urllib.quote(vdrive_pathname)
2007-07-11 02:37:37 +00:00
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":
2007-10-12 02:20:41 +00:00
print >>stdout, "%10s %s/" % ("", name)
2007-07-11 02:37:37 +00:00
else:
assert childtype == "filenode"
size = child[1]['size']
2007-10-12 02:20:41 +00:00
print >>stdout, "%10s %s" % (size, name)
2007-07-11 02:37:37 +00:00
def main():
import optparse, re
2007-07-11 02:37:37 +00:00
parser = optparse.OptionParser()
parser.add_option("-u", "--node-url", dest="nodeurl")
parser.add_option("-r", "--root-uri", dest="rooturi")
2007-07-11 02:37:37 +00:00
(options, args) = parser.parse_args()
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
if not options.rooturi:
raise ValueError("must provide --root-uri")
vdrive_pathname = ""
2007-07-11 02:37:37 +00:00
if args:
vdrive_pathname = args[0]
2007-07-11 02:37:37 +00:00
list(options.nodeurl, options.rooturi, vdrive_pathname)
2007-07-11 02:37:37 +00:00
if __name__ == '__main__':
main()