5 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
6 from allmydata.scripts.common_http import do_http
8 # this script is used for both 'mv' and 'ln'
10 def mv(nodeurl, aliases, from_file, to_file, stdout, stderr, mode="move"):
11 if nodeurl[-1] != "/":
13 rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
14 from_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
16 from_url += "/" + escape_path(path)
17 # figure out the source cap
18 data = urllib.urlopen(from_url + "?t=json").read()
19 nodetype, attrs = simplejson.loads(data)
20 cap = attrs.get("rw_uri") or attrs["ro_uri"]
21 # simplejson always returns unicode, but we know that it's really just an
26 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
27 to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
29 to_url += "/" + escape_path(path)
30 if path.endswith("/"):
31 # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
35 resp = do_http("PUT", to_url, cap)
37 if not re.search(r'^2\d\d$', str(status)):
38 print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
39 print >>stderr, resp.read()
41 print >>stderr, "NOT removing the original"
45 # now remove the original
46 resp = do_http("DELETE", from_url)
47 if not re.search(r'^2\d\d$', str(status)):
48 print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
49 print >>stderr, resp.read()