source file: /home/buildslave/tahoe/edgy/build/src/allmydata/scripts/tahoe_mv.py
file stats: 44 lines, 35 executed: 79.5% covered
   1. 
   2. import re
   3. import urllib
   4. import simplejson
   5. from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
   6. from allmydata.scripts.common_http import do_http
   7. 
   8. # this script is used for both 'mv' and 'ln'
   9. 
  10. def mv(options, mode="move"):
  11.     nodeurl = options['node-url']
  12.     aliases = options.aliases
  13.     from_file = options.from_file
  14.     to_file = options.to_file
  15.     stdout = options.stdout
  16.     stderr = options.stderr
  17. 
  18.     if nodeurl[-1] != "/":
  19.         nodeurl += "/"
  20.     rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
  21.     from_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
  22.     if path:
  23.         from_url += "/" + escape_path(path)
  24.     # figure out the source cap
  25.     data = urllib.urlopen(from_url + "?t=json").read()
  26.     nodetype, attrs = simplejson.loads(data)
  27.     cap = attrs.get("rw_uri") or attrs["ro_uri"]
  28.     # simplejson always returns unicode, but we know that it's really just an
  29.     # ASCII file-cap.
  30.     cap = str(cap)
  31. 
  32.     # now get the target
  33.     rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
  34.     to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
  35.     if path:
  36.         to_url += "/" + escape_path(path)
  37.     if path.endswith("/"):
  38.         # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
  39.         pass # TODO
  40.     to_url += "?t=uri"
  41. 
  42.     resp = do_http("PUT", to_url, cap)
  43.     status = resp.status
  44.     if not re.search(r'^2\d\d$', str(status)):
  45.         print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
  46.         print >>stderr, resp.read()
  47.         if mode == "move":
  48.             print >>stderr, "NOT removing the original"
  49.         return
  50. 
  51.     if mode == "move":
  52.         # now remove the original
  53.         resp = do_http("DELETE", from_url)
  54.         if not re.search(r'^2\d\d$', str(status)):
  55.             print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
  56.             print >>stderr, resp.read()
  57. 
  58.     print >>stdout, "OK"
  59.     return
  60. 
  61. 
  62.