import os.path
import urllib
import simplejson
+from twisted.python.failure import Failure
from allmydata.scripts.common import get_alias, escape_path, DefaultAliasMarker
from allmydata.scripts.common_http import do_http
from allmydata import uri
return s
return str(s)
-class WriteError(Exception):
- pass
-class ReadError(Exception):
- pass
+class TahoeError(Exception):
+ def __init__(self, msg, resp):
+ self.msg = msg
+ self.status = resp.status
+ self.reason = resp.reason
+ self.body = resp.read()
+
+ def display(self, err):
+ print >>err, "%s: %s %s" % (self.msg, self.status, self.reason)
+ print >>err, self.body
+
class MissingSourceError(Exception):
pass
resp = do_http("GET", url)
if resp.status == 200:
return resp
- raise ReadError("Error during GET: %s %s %s" % (resp.status,
- resp.reason,
- resp.read()))
+ raise TahoeError("Error during GET", resp)
+
def GET_to_string(url):
f = GET_to_file(url)
return f.read()
resp = do_http("PUT", url, data)
if resp.status in (200, 201):
return resp.read()
- raise WriteError("Error during PUT: %s %s %s" % (resp.status, resp.reason,
- resp.read()))
+ raise TahoeError("Error during PUT", resp)
def POST(url, data):
resp = do_http("POST", url, data)
if resp.status in (200, 201):
return resp.read()
- raise WriteError("Error during POST: %s %s %s" % (resp.status, resp.reason,
- resp.read()))
+ raise TahoeError("Error during POST", resp)
def mkdir(targeturl):
url = targeturl + "?t=mkdir"
resp = do_http("POST", url)
if resp.status in (200, 201):
return resp.read().strip()
- raise WriteError("Error during mkdir: %s %s %s" % (resp.status, resp.reason,
- resp.read()))
+ raise TahoeError("Error during mkdir", resp)
def make_tahoe_subdirectory(nodeurl, parent_writecap, name):
url = nodeurl + "/".join(["uri",
resp = do_http("POST", url)
if resp.status in (200, 201):
return resp.read().strip()
- raise WriteError("Error during mkdir: %s %s %s" % (resp.status, resp.reason,
- resp.read()))
+ raise TahoeError("Error during mkdir", resp)
class LocalFileSource:
bestcap = writecap or readcap
url = self.nodeurl + "uri/%s" % urllib.quote(bestcap)
resp = do_http("GET", url + "?t=json")
- assert resp.status == 200
+ if resp.status != 200:
+ raise TahoeError("Error examining source directory", resp)
parsed = simplejson.loads(resp.read())
nodetype, d = parsed
assert nodetype == "dirnode"
bestcap = writecap or readcap
url = self.nodeurl + "uri/%s" % urllib.quote(bestcap)
resp = do_http("GET", url + "?t=json")
- assert resp.status == 200
+ if resp.status != 200:
+ raise TahoeError("Error examining target directory", resp)
parsed = simplejson.loads(resp.read())
nodetype, d = parsed
assert nodetype == "dirnode"
print >>self.stderr, message
self.progressfunc = progress
self.cache = {}
- source_specs = options.sources
- destination_spec = options.destination
+ try:
+ self.try_copy()
+ except TahoeError, te:
+ Failure().printTraceback(self.stderr)
+ print >>self.stderr
+ te.display(self.stderr)
+ return 1
+
+ def try_copy(self):
+ source_specs = self.options.sources
+ destination_spec = self.options.destination
recursive = self.options["recursive"]
target = self.get_target_info(destination_spec)
if resp.status == 404:
# doesn't exist yet
t = TahoeMissingTarget(url)
- else:
+ elif resp.status == 200:
parsed = simplejson.loads(resp.read())
nodetype, d = parsed
if nodetype == "dirnode":
mutable = d.get("mutable", False)
t = TahoeFileTarget(self.nodeurl, mutable,
writecap, readcap, url)
+ else:
+ raise TahoeError("Error examining target '%s'"
+ % destination_spec, resp)
return t
def get_source_info(self, source_spec):
resp = do_http("GET", url + "?t=json")
if resp.status == 404:
raise MissingSourceError(source_spec)
+ elif resp.status != 200:
+ raise TahoeError("Error examining source '%s'" % source_spec,
+ resp)
parsed = simplejson.loads(resp.read())
nodetype, d = parsed
if nodetype == "dirnode":