]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_mv.py
41bb5fd95471ecb505794ea9b85fd4c8dfa987f6
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_mv.py
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(nodeurl, aliases, from_file, to_file, stdout, stderr, mode="move"):
11     if nodeurl[-1] != "/":
12         nodeurl += "/"
13     rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
14     from_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
15     if path:
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
22     # ASCII file-cap.
23     cap = str(cap)
24
25     # now get the target
26     rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
27     to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
28     if path:
29         to_url += "/" + escape_path(path)
30     if path.endswith("/"):
31         # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
32         pass # TODO
33     to_url += "?t=uri"
34
35     resp = do_http("PUT", to_url, cap)
36     status = resp.status
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()
40         if mode == "move":
41             print >>stderr, "NOT removing the original"
42         return
43
44     if mode == "move":
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()
50
51     print >>stdout, "OK"
52     return
53
54
55