4 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
6 from allmydata.scripts.common_http import do_http
9 nodeurl = options['node-url']
10 aliases = options.aliases
12 stdout = options.stdout
13 stderr = options.stderr
15 if not nodeurl.endswith("/"):
17 if where.endswith("/"):
20 rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS)
21 except UnknownAliasError, e:
22 print >>stderr, "error: %s" % e.args[0]
24 url = nodeurl + "uri/%s" % urllib.quote(rootcap)
26 # move where.endswith check here?
27 url += "/" + escape_path(path)
28 assert not url.endswith("/")
30 resp = do_http("GET", url)
31 if resp.status == 404:
32 print >>stderr, "No such file or directory"
34 if resp.status != 200:
35 print >>stderr, "Error during GET: %s %s %s" % (resp.status,
50 parsed = simplejson.loads(data)
52 le.args = tuple(le.args + (data,))
56 if nodetype == "dirnode":
57 children = d['children']
58 elif nodetype == "filenode":
59 childname = path.split("/")[-1]
60 children = {childname: (nodetype, d)}
61 if "metadata" not in d:
63 childnames = sorted(children.keys())
66 # we build up a series of rows, then we loop through them to compute a
67 # maxwidth so we can format them tightly. Size, filename, and URI are the
68 # variable-width ones.
71 for name in childnames:
73 child = children[name]
76 # See webapi.txt for a discussion of the meanings of unix local
77 # filesystem mtime and ctime, Tahoe mtime and ctime, and Tahoe
78 # linkmotime and linkcrtime.
79 ctime = child[1].get("metadata", {}).get('tahoe', {}).get("linkcrtime")
81 ctime = child[1]["metadata"].get("ctime")
83 mtime = child[1].get("metadata", {}).get('tahoe', {}).get("linkmotime")
85 mtime = child[1]["metadata"].get("mtime")
86 rw_uri = child[1].get("rw_uri")
87 ro_uri = child[1].get("ro_uri")
89 # match for formatting that GNU 'ls' does
90 if (now - ctime) > 6*30*24*60*60:
95 ctime_s = time.strftime(fmt, time.localtime(ctime))
98 if childtype == "dirnode":
102 elif childtype == "filenode":
104 size = str(child[1]['size'])
119 if childtype == "dirnode":
122 uri = rw_uri or ro_uri
126 line.append(t0+t1+t2+t3)
129 if not options["classify"]:
131 line.append(name + classify)
134 if options["readonly-uri"]:
135 line.append(ro_uri or "-")
142 for i,cell in enumerate(row):
143 while len(max_widths) <= i:
145 while len(left_justifys) <= i:
146 left_justifys.append(False)
147 max_widths[i] = max(max_widths[i], len(cell))
148 if cell.startswith("URI"):
149 left_justifys[i] = True
150 if len(left_justifys) == 1:
151 left_justifys[0] = True
153 for i in range(len(max_widths)):
157 piece += str(max_widths[i])
159 fmt_pieces.append(piece)
160 fmt = " ".join(fmt_pieces)
162 print >>stdout, (fmt % tuple(row)).rstrip()
166 # error cases that need improvement:
167 # list-one-file: tahoe ls my:docs/Makefile