3 from cStringIO import StringIO
5 from allmydata.scripts.common_http import do_http, format_http_success, format_http_error
6 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
8 from allmydata.util.encodingutil import quote_output
12 @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
14 @return: a Deferred which eventually fires with the exit code
16 nodeurl = options['node-url']
17 aliases = options.aliases
18 from_file = options.from_file
19 to_file = options.to_file
20 mutable = options['mutable']
21 mutable_type = options['mutable-type']
27 stdout = options.stdout
28 stderr = options.stderr
30 if nodeurl[-1] != "/":
33 # several possibilities for the TO_FILE argument.
34 # <none> : unlinked upload
35 # foo : TAHOE_ALIAS/foo
36 # subdir/foo : TAHOE_ALIAS/subdir/foo
37 # /oops/subdir/foo : DISALLOWED
38 # ALIAS:foo : aliases[ALIAS]/foo
39 # ALIAS:subdir/foo : aliases[ALIAS]/subdir/foo
41 # ALIAS:/oops/subdir/foo : DISALLOWED
42 # DIRCAP:./foo : DIRCAP/foo
43 # DIRCAP:./subdir/foo : DIRCAP/subdir/foo
44 # MUTABLE-FILE-WRITECAP : filecap
46 # FIXME: don't hardcode cap format.
47 if to_file.startswith("URI:MDMF:") or to_file.startswith("URI:SSK:"):
48 url = nodeurl + "uri/%s" % urllib.quote(to_file)
51 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
52 except UnknownAliasError, e:
55 if path.startswith("/"):
56 suggestion = to_file.replace(u"/", u"", 1)
57 print >>stderr, "Error: The remote filename must not start with a slash"
58 print >>stderr, "Please try again, perhaps with %s" % quote_output(suggestion)
60 url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
62 url += escape_path(path)
72 file_format = mutable_type.upper()
74 url += "?format=%s" % file_format
77 infileobj = open(os.path.expanduser(from_file), "rb")
79 # do_http() can't use stdin directly: for one thing, we need a
80 # Content-Length field. So we currently must copy it.
82 print >>stderr, "waiting for file data on stdin.."
84 infileobj = StringIO(data)
86 resp = do_http("PUT", url, infileobj)
88 if resp.status in (200, 201,):
89 print >>stderr, format_http_success(resp)
90 print >>stdout, quote_output(resp.read(), quotemarks=False)
93 print >>stderr, format_http_error("Error", resp)