2007-10-12 03:31:48 +00:00
|
|
|
|
|
|
|
import re
|
2007-10-12 05:29:23 +00:00
|
|
|
import urllib
|
2007-10-12 03:31:48 +00:00
|
|
|
import simplejson
|
2008-05-20 02:28:50 +00:00
|
|
|
from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
|
2007-10-12 05:29:23 +00:00
|
|
|
from allmydata.scripts.common_http import do_http
|
2007-10-12 03:31:48 +00:00
|
|
|
|
2008-05-20 20:30:31 +00:00
|
|
|
# this script is used for both 'mv' and 'ln'
|
|
|
|
|
|
|
|
def mv(nodeurl, aliases, from_file, to_file, stdout, stderr, mode="move"):
|
2007-10-12 03:31:48 +00:00
|
|
|
if nodeurl[-1] != "/":
|
|
|
|
nodeurl += "/"
|
2008-05-20 02:28:50 +00:00
|
|
|
rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
|
|
|
|
from_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
|
|
|
|
if path:
|
|
|
|
from_url += "/" + escape_path(path)
|
|
|
|
# figure out the source cap
|
|
|
|
data = urllib.urlopen(from_url + "?t=json").read()
|
2007-10-12 03:31:48 +00:00
|
|
|
nodetype, attrs = simplejson.loads(data)
|
2008-05-20 02:28:50 +00:00
|
|
|
cap = attrs.get("rw_uri") or attrs["ro_uri"]
|
|
|
|
# simplejson always returns unicode, but we know that it's really just an
|
|
|
|
# ASCII file-cap.
|
|
|
|
cap = str(cap)
|
|
|
|
|
|
|
|
# now get the target
|
|
|
|
rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
|
|
|
|
to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
|
|
|
|
if path:
|
|
|
|
to_url += "/" + escape_path(path)
|
|
|
|
if path.endswith("/"):
|
|
|
|
# "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
|
|
|
|
pass # TODO
|
|
|
|
to_url += "?t=uri"
|
|
|
|
|
|
|
|
resp = do_http("PUT", to_url, cap)
|
2007-10-12 03:31:48 +00:00
|
|
|
status = resp.status
|
|
|
|
if not re.search(r'^2\d\d$', str(status)):
|
|
|
|
print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
|
|
|
|
print >>stderr, resp.read()
|
2008-05-20 20:30:31 +00:00
|
|
|
if mode == "move":
|
|
|
|
print >>stderr, "NOT removing the original"
|
2008-05-20 19:49:47 +00:00
|
|
|
return
|
2007-10-12 03:31:48 +00:00
|
|
|
|
2008-05-20 20:30:31 +00:00
|
|
|
if mode == "move":
|
|
|
|
# now remove the original
|
|
|
|
resp = do_http("DELETE", from_url)
|
|
|
|
if not re.search(r'^2\d\d$', str(status)):
|
|
|
|
print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
|
|
|
|
print >>stderr, resp.read()
|
2007-10-12 03:31:48 +00:00
|
|
|
|
|
|
|
print >>stdout, "OK"
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|