]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_ls.py
e64e9d2049028df67c8ab3d4d0964f8a4004ce2f
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_ls.py
1
2 import urllib, time
3 import simplejson
4 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
5                                      UnknownAliasError
6 from allmydata.scripts.common_http import do_http, format_http_error
7 from allmydata.util.encodingutil import unicode_to_output, quote_output, is_printable_ascii, to_str
8
9 def list(options):
10     nodeurl = options['node-url']
11     aliases = options.aliases
12     where = options.where
13     stdout = options.stdout
14     stderr = options.stderr
15
16     if not nodeurl.endswith("/"):
17         nodeurl += "/"
18     if where.endswith("/"):
19         where = where[:-1]
20     try:
21         rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS)
22     except UnknownAliasError, e:
23         e.display(stderr)
24         return 1
25     url = nodeurl + "uri/%s" % urllib.quote(rootcap)
26     if path:
27         # move where.endswith check here?
28         url += "/" + escape_path(path)
29     assert not url.endswith("/")
30     url += "?t=json"
31     resp = do_http("GET", url)
32     if resp.status == 404:
33         print >>stderr, "No such file or directory"
34         return 2
35     if resp.status != 200:
36         print >>stderr, format_http_error("Error during GET", resp)
37         if resp.status == 0:
38             return 3
39         else:
40             return resp.status
41
42     data = resp.read()
43
44     if options['json']:
45         # The webapi server should always output printable ASCII.
46         if is_printable_ascii(data):
47             print >>stdout, data
48             return 0
49         else:
50             print >>stderr, "The JSON response contained unprintable characters:\n%s" % quote_output(data)
51             return 1
52
53     try:
54         parsed = simplejson.loads(data)
55     except Exception, e:
56         print >>stderr, "error: %s" % quote_output(e.args[0], quotemarks=False)
57         print >>stderr, "Could not parse JSON response:\n%s" % quote_output(data)
58         return 1
59
60     nodetype, d = parsed
61     children = {}
62     if nodetype == "dirnode":
63         children = d['children']
64     else:
65         # paths returned from get_alias are always valid UTF-8
66         childname = path.split("/")[-1].decode('utf-8')
67         children = {childname: (nodetype, d)}
68         if "metadata" not in d:
69             d["metadata"] = {}
70     childnames = sorted(children.keys())
71     now = time.time()
72
73     # we build up a series of rows, then we loop through them to compute a
74     # maxwidth so we can format them tightly. Size, filename, and URI are the
75     # variable-width ones.
76     rows = []
77     has_unknowns = False
78
79     for name in childnames:
80         child = children[name]
81         name = unicode(name)
82         childtype = child[0]
83
84         # See webapi.txt for a discussion of the meanings of unix local
85         # filesystem mtime and ctime, Tahoe mtime and ctime, and Tahoe
86         # linkmotime and linkcrtime.
87         ctime = child[1].get("metadata", {}).get('tahoe', {}).get("linkcrtime")
88         if not ctime:
89             ctime = child[1]["metadata"].get("ctime")
90
91         mtime = child[1].get("metadata", {}).get('tahoe', {}).get("linkmotime")
92         if not mtime:
93             mtime = child[1]["metadata"].get("mtime")
94         rw_uri = to_str(child[1].get("rw_uri"))
95         ro_uri = to_str(child[1].get("ro_uri"))
96         if ctime:
97             # match for formatting that GNU 'ls' does
98             if (now - ctime) > 6*30*24*60*60:
99                 # old files
100                 fmt = "%b %d  %Y"
101             else:
102                 fmt = "%b %d %H:%M"
103             ctime_s = time.strftime(fmt, time.localtime(ctime))
104         else:
105             ctime_s = "-"
106         if childtype == "dirnode":
107             t0 = "d"
108             size = "-"
109             classify = "/"
110         elif childtype == "filenode":
111             t0 = "-"
112             size = str(child[1].get("size", "?"))
113             classify = ""
114             if rw_uri:
115                 classify = "*"
116         else:
117             has_unknowns = True
118             t0 = "?"
119             size = "?"
120             classify = "?"
121         t1 = "-"
122         if ro_uri:
123             t1 = "r"
124         t2 = "-"
125         if rw_uri:
126             t2 = "w"
127         t3 = "-"
128         if childtype == "dirnode":
129             t3 = "x"
130
131         uri = rw_uri or ro_uri
132
133         line = []
134         if options["long"]:
135             line.append(t0+t1+t2+t3)
136             line.append(size)
137             line.append(ctime_s)
138         if not options["classify"]:
139             classify = ""
140
141         encoding_error = False
142         try:
143             line.append(unicode_to_output(name) + classify)
144         except UnicodeEncodeError:
145             encoding_error = True
146             line.append(quote_output(name) + classify)
147
148         if options["uri"]:
149             line.append(uri)
150         if options["readonly-uri"]:
151             line.append(quote_output(ro_uri or "-", quotemarks=False))
152
153         rows.append((encoding_error, line))
154
155     max_widths = []
156     left_justifys = []
157     for (encoding_error, row) in rows:
158         for i,cell in enumerate(row):
159             while len(max_widths) <= i:
160                 max_widths.append(0)
161             while len(left_justifys) <= i:
162                 left_justifys.append(False)
163             max_widths[i] = max(max_widths[i], len(cell))
164             if cell.startswith("URI"):
165                 left_justifys[i] = True
166     if len(left_justifys) == 1:
167         left_justifys[0] = True
168     fmt_pieces = []
169     for i in range(len(max_widths)):
170         piece = "%"
171         if left_justifys[i]:
172             piece += "-"
173         piece += str(max_widths[i])
174         piece += "s"
175         fmt_pieces.append(piece)
176     fmt = " ".join(fmt_pieces)
177
178     rc = 0
179     for (encoding_error, row) in rows:
180         if encoding_error:
181             print >>stderr, (fmt % tuple(row)).rstrip()
182             rc = 1
183         else:
184             print >>stdout, (fmt % tuple(row)).rstrip()
185
186     if rc == 1:
187         print >>stderr, "\nThis listing included files whose names could not be converted to the terminal" \
188                         "\noutput encoding. Their names are shown using backslash escapes and in quotes."
189     if has_unknowns:
190         print >>stderr, "\nThis listing included unknown objects. Using a webapi server that supports" \
191                         "\na later version of Tahoe may help."
192
193     return rc