]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/scripts/tahoe_put.py
add --format= to 'tahoe put'/'mkdir', remove --mutable-type. Closes #1561
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_put.py
index f67ebc5ca636226b316e25874c1066637da138c8..a85539efec87e0d96f4a839705a4261212848ac8 100644 (file)
@@ -1,9 +1,11 @@
 
+import os
 from cStringIO import StringIO
-import os.path
 import urllib
-from allmydata.scripts.common_http import do_http
-from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
+from allmydata.scripts.common_http import do_http, format_http_success, format_http_error
+from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
+                                     UnknownAliasError
+from allmydata.util.encodingutil import quote_output
 
 def put(options):
     """
@@ -16,6 +18,7 @@ def put(options):
     from_file = options.from_file
     to_file = options.to_file
     mutable = options['mutable']
+    format = options['format']
     if options['quiet']:
         verbosity = 0
     else:
@@ -34,19 +37,25 @@ def put(options):
         #  /oops/subdir/foo : DISALLOWED
         #  ALIAS:foo  : aliases[ALIAS]/foo
         #  ALIAS:subdir/foo  : aliases[ALIAS]/subdir/foo
+
         #  ALIAS:/oops/subdir/foo : DISALLOWED
         #  DIRCAP:./foo        : DIRCAP/foo
         #  DIRCAP:./subdir/foo : DIRCAP/subdir/foo
         #  MUTABLE-FILE-WRITECAP : filecap
 
-        if to_file.startswith("URI:SSK:"):
+        # FIXME: don't hardcode cap format.
+        if to_file.startswith("URI:MDMF:") or to_file.startswith("URI:SSK:"):
             url = nodeurl + "uri/%s" % urllib.quote(to_file)
         else:
-            rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
+            try:
+                rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
+            except UnknownAliasError, e:
+                e.display(stderr)
+                return 1
             if path.startswith("/"):
-                suggestion = to_file.replace("/", "", 1)
-                print >>stderr, "ERROR: The VDRIVE filename must not start with a slash"
-                print >>stderr, "Please try again, perhaps with:", suggestion
+                suggestion = to_file.replace(u"/", u"", 1)
+                print >>stderr, "Error: The remote filename must not start with a slash"
+                print >>stderr, "Please try again, perhaps with %s" % quote_output(suggestion)
                 return 1
             url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
             if path:
@@ -54,8 +63,15 @@ def put(options):
     else:
         # unlinked upload
         url = nodeurl + "uri"
+
+    queryargs = []
     if mutable:
-        url += "?mutable=true"
+        queryargs.append("mutable=true")
+    if format:
+        queryargs.append("format=%s" % format)
+    if queryargs:
+        url += "?" + "&".join(queryargs)
+
     if from_file:
         infileobj = open(os.path.expanduser(from_file), "rb")
     else:
@@ -69,10 +85,9 @@ def put(options):
     resp = do_http("PUT", url, infileobj)
 
     if resp.status in (200, 201,):
-        print >>stderr, "%s %s" % (resp.status, resp.reason)
-        print >>stdout, resp.read()
+        print >>stderr, format_http_success(resp)
+        print >>stdout, quote_output(resp.read(), quotemarks=False)
         return 0
 
-    print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
-    print >>stderr, resp.read()
+    print >>stderr, format_http_error("Error", resp)
     return 1