source file: /home/buildslave/tahoe/edgy/build/src/allmydata/scripts/tahoe_ls.py
file stats: 114 lines, 101 executed: 88.6% covered
   1. 
   2. import urllib, time
   3. import simplejson
   4. from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
   5. from allmydata.scripts.common_http import do_http
   6. 
   7. def list(options):
   8.     nodeurl = options['node-url']
   9.     aliases = options.aliases
  10.     where = options.where
  11.     stdout = options.stdout
  12.     stderr = options.stderr
  13. 
  14.     if not nodeurl.endswith("/"):
  15.         nodeurl += "/"
  16.     if where.endswith("/"):
  17.         where = where[:-1]
  18.     rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS)
  19.     url = nodeurl + "uri/%s" % urllib.quote(rootcap)
  20.     if path:
  21.         # move where.endswith check here?
  22.         url += "/" + escape_path(path)
  23.     assert not url.endswith("/")
  24.     url += "?t=json"
  25.     resp = do_http("GET", url)
  26.     if resp.status == 404:
  27.         print >>stderr, "No such file or directory"
  28.         return 2
  29.     if resp.status != 200:
  30.         print >>stderr, "Error during GET: %s %s %s" % (resp.status,
  31.                                                         resp.reason,
  32.                                                         resp.read())
  33.     data = resp.read()
  34. 
  35.     if options['json']:
  36.         print >>stdout, data
  37.         return
  38. 
  39.     parsed = simplejson.loads(data)
  40.     nodetype, d = parsed
  41.     children = {}
  42.     if nodetype == "dirnode":
  43.         children = d['children']
  44.     elif nodetype == "filenode":
  45.         childname = path.split("/")[-1]
  46.         children = {childname: d}
  47.     childnames = sorted(children.keys())
  48.     now = time.time()
  49. 
  50.     # we build up a series of rows, then we loop through them to compute a
  51.     # maxwidth so we can format them tightly. Size, filename, and URI are the
  52.     # variable-width ones.
  53.     rows = []
  54. 
  55.     for name in childnames:
  56.         child = children[name]
  57.         childtype = child[0]
  58.         ctime = child[1]["metadata"].get("ctime")
  59.         mtime = child[1]["metadata"].get("mtime")
  60.         rw_uri = child[1].get("rw_uri")
  61.         ro_uri = child[1].get("ro_uri")
  62.         if ctime:
  63.             # match for formatting that GNU 'ls' does
  64.             if (now - ctime) > 6*30*24*60*60:
  65.                 # old files
  66.                 fmt = "%b %d  %Y"
  67.             else:
  68.                 fmt = "%b %d %H:%M"
  69.             ctime_s = time.strftime(fmt, time.localtime(ctime))
  70.         else:
  71.             ctime_s = "-"
  72.         if childtype == "dirnode":
  73.             t0 = "d"
  74.             size = "-"
  75.             classify = "/"
  76.         elif childtype == "filenode":
  77.             t0 = "-"
  78.             size = str(child[1]['size'])
  79.             classify = ""
  80.             if rw_uri:
  81.                 classify = "*"
  82.         else:
  83.             t0 = "?"
  84.             size = "?"
  85.             classify = "?"
  86.         t1 = "-"
  87.         if ro_uri:
  88.             t1 = "r"
  89.         t2 = "-"
  90.         if rw_uri:
  91.             t2 = "w"
  92.         t3 = "-"
  93.         if childtype == "dirnode":
  94.             t3 = "x"
  95. 
  96.         uri = rw_uri or ro_uri
  97. 
  98.         line = []
  99.         if options["long"]:
 100.             line.append(t0+t1+t2+t3)
 101.             line.append(size)
 102.             line.append(ctime_s)
 103.         if not options["classify"]:
 104.             classify = ""
 105.         line.append(name + classify)
 106.         if options["uri"]:
 107.             line.append(uri)
 108.         if options["readonly-uri"]:
 109.             line.append(ro_uri or "-")
 110. 
 111.         rows.append(line)
 112. 
 113.     max_widths = []
 114.     left_justifys = []
 115.     for row in rows:
 116.         for i,cell in enumerate(row):
 117.             while len(max_widths) <= i:
 118.                 max_widths.append(0)
 119.             while len(left_justifys) <= i:
 120.                 left_justifys.append(False)
 121.             max_widths[i] = max(max_widths[i], len(cell))
 122.             if cell.startswith("URI"):
 123.                 left_justifys[i] = True
 124.     if len(left_justifys) == 1:
 125.         left_justifys[0] = True
 126.     fmt_pieces = []
 127.     for i in range(len(max_widths)):
 128.         piece = "%"
 129.         if left_justifys[i]:
 130.             piece += "-"
 131.         piece += str(max_widths[i])
 132.         piece += "s"
 133.         fmt_pieces.append(piece)
 134.     fmt = " ".join(fmt_pieces)
 135.     for row in rows:
 136.         print >>stdout, (fmt % tuple(row)).rstrip()