2007-07-11 02:37:37 +00:00
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
import urllib, time
|
2007-07-11 02:37:37 +00:00
|
|
|
import simplejson
|
2008-05-20 02:28:50 +00:00
|
|
|
from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
|
2007-07-11 02:37:37 +00:00
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
def list(nodeurl, aliases, where, config, stdout, stderr):
|
|
|
|
if not nodeurl.endswith("/"):
|
2007-08-17 19:54:47 +00:00
|
|
|
nodeurl += "/"
|
2008-05-20 02:28:50 +00:00
|
|
|
if where.endswith("/"):
|
|
|
|
where = where[:-1]
|
|
|
|
rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS)
|
|
|
|
url = nodeurl + "uri/%s" % urllib.quote(rootcap)
|
|
|
|
if path:
|
|
|
|
# move where.endswith check here?
|
|
|
|
url += "/" + escape_path(path)
|
|
|
|
assert not url.endswith("/")
|
2007-07-11 02:37:37 +00:00
|
|
|
url += "?t=json"
|
|
|
|
data = urllib.urlopen(url).read()
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
if config['json']:
|
|
|
|
print >>stdout, data
|
|
|
|
return
|
|
|
|
|
2007-07-11 02:37:37 +00:00
|
|
|
parsed = simplejson.loads(data)
|
|
|
|
nodetype, d = parsed
|
2008-05-20 02:28:50 +00:00
|
|
|
children = {}
|
2007-07-11 02:37:37 +00:00
|
|
|
if nodetype == "dirnode":
|
2008-05-20 02:28:50 +00:00
|
|
|
children = d['children']
|
2007-10-21 19:33:06 +00:00
|
|
|
elif nodetype == "filenode":
|
2008-05-20 02:28:50 +00:00
|
|
|
childname = path.split("/")[-1]
|
|
|
|
children = {childname: d}
|
|
|
|
childnames = sorted(children.keys())
|
|
|
|
now = time.time()
|
|
|
|
for name in childnames:
|
|
|
|
child = children[name]
|
|
|
|
childtype = child[0]
|
|
|
|
ctime = child[1]["metadata"].get("ctime")
|
|
|
|
mtime = child[1]["metadata"].get("mtime")
|
2008-05-20 19:36:55 +00:00
|
|
|
rw_uri = child[1].get("rw_uri")
|
|
|
|
ro_uri = child[1].get("ro_uri")
|
2008-05-20 02:28:50 +00:00
|
|
|
if ctime:
|
|
|
|
# match for formatting that GNU 'ls' does
|
|
|
|
if (now - ctime) > 6*30*24*60*60:
|
|
|
|
# old files
|
|
|
|
fmt = "%b %d %Y"
|
|
|
|
else:
|
|
|
|
fmt = "%b %d %H:%M"
|
|
|
|
ctime_s = time.strftime(fmt, time.localtime(ctime))
|
|
|
|
else:
|
|
|
|
ctime_s = "-"
|
|
|
|
if childtype == "dirnode":
|
2008-05-20 19:36:55 +00:00
|
|
|
t0 = "d"
|
2008-05-20 02:28:50 +00:00
|
|
|
size = "-"
|
|
|
|
classify = "/"
|
|
|
|
elif childtype == "filenode":
|
2008-05-20 19:36:55 +00:00
|
|
|
t0 = "-"
|
2008-05-20 02:28:50 +00:00
|
|
|
size = child[1]['size']
|
|
|
|
classify = ""
|
2008-05-20 19:36:55 +00:00
|
|
|
else:
|
|
|
|
t0 = "?"
|
|
|
|
size = "?"
|
|
|
|
classify = "?"
|
|
|
|
t1 = "-"
|
|
|
|
if ro_uri:
|
|
|
|
t1 = "r"
|
|
|
|
t2 = "-"
|
|
|
|
if rw_uri:
|
|
|
|
t2 = "w"
|
|
|
|
t3 = "-"
|
|
|
|
if childtype == "dirnode":
|
|
|
|
t3 = "x"
|
2008-05-20 02:28:50 +00:00
|
|
|
|
2008-05-20 19:36:55 +00:00
|
|
|
uri = rw_uri or ro_uri
|
2008-05-20 02:28:50 +00:00
|
|
|
|
|
|
|
line = []
|
|
|
|
if config["long"]:
|
2008-05-20 19:36:55 +00:00
|
|
|
line.append("%s %10s %12s" % (t0+t1+t2+t3, size, ctime_s))
|
2008-05-20 02:28:50 +00:00
|
|
|
if config["uri"]:
|
|
|
|
line.append(uri)
|
|
|
|
line.append(name)
|
|
|
|
if config["classify"]:
|
|
|
|
line[-1] += classify
|
|
|
|
|
|
|
|
print >>stdout, " ".join(line)
|