2007-08-16 19:15:38 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
import os
|
2008-05-20 02:28:50 +00:00
|
|
|
from cStringIO import StringIO
|
2007-10-12 05:29:23 +00:00
|
|
|
import urllib
|
2010-06-07 01:02:15 +00:00
|
|
|
from allmydata.scripts.common_http import do_http, format_http_success, format_http_error
|
2010-02-11 02:43:18 +00:00
|
|
|
from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
|
|
|
|
UnknownAliasError
|
2010-07-12 00:30:15 +00:00
|
|
|
from allmydata.util.encodingutil import quote_output
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2008-08-01 18:46:24 +00:00
|
|
|
def put(options):
|
2007-08-16 19:15:38 +00:00
|
|
|
"""
|
|
|
|
@param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
|
|
|
|
|
|
|
|
@return: a Deferred which eventually fires with the exit code
|
|
|
|
"""
|
2008-08-01 18:46:24 +00:00
|
|
|
nodeurl = options['node-url']
|
|
|
|
aliases = options.aliases
|
|
|
|
from_file = options.from_file
|
|
|
|
to_file = options.to_file
|
|
|
|
mutable = options['mutable']
|
2011-09-05 02:09:22 +00:00
|
|
|
mutable_type = options['mutable-type']
|
2008-08-01 18:46:24 +00:00
|
|
|
if options['quiet']:
|
|
|
|
verbosity = 0
|
|
|
|
else:
|
|
|
|
verbosity = 2
|
|
|
|
stdin = options.stdin
|
|
|
|
stdout = options.stdout
|
|
|
|
stderr = options.stderr
|
|
|
|
|
2007-10-12 05:29:23 +00:00
|
|
|
if nodeurl[-1] != "/":
|
|
|
|
nodeurl += "/"
|
2008-05-20 02:28:50 +00:00
|
|
|
if to_file:
|
2008-08-02 02:27:29 +00:00
|
|
|
# several possibilities for the TO_FILE argument.
|
|
|
|
# <none> : unlinked upload
|
|
|
|
# foo : TAHOE_ALIAS/foo
|
|
|
|
# subdir/foo : TAHOE_ALIAS/subdir/foo
|
2009-12-27 20:10:43 +00:00
|
|
|
# /oops/subdir/foo : DISALLOWED
|
2008-08-02 02:27:29 +00:00
|
|
|
# ALIAS:foo : aliases[ALIAS]/foo
|
|
|
|
# ALIAS:subdir/foo : aliases[ALIAS]/subdir/foo
|
2010-02-11 02:43:18 +00:00
|
|
|
|
2009-12-27 20:10:43 +00:00
|
|
|
# ALIAS:/oops/subdir/foo : DISALLOWED
|
2008-08-02 02:27:29 +00:00
|
|
|
# DIRCAP:./foo : DIRCAP/foo
|
|
|
|
# DIRCAP:./subdir/foo : DIRCAP/subdir/foo
|
|
|
|
# MUTABLE-FILE-WRITECAP : filecap
|
|
|
|
|
2011-08-02 02:16:13 +00:00
|
|
|
# FIXME: don't hardcode cap format.
|
|
|
|
if to_file.startswith("URI:MDMF:") or to_file.startswith("URI:SSK:"):
|
2008-08-02 02:27:29 +00:00
|
|
|
url = nodeurl + "uri/%s" % urllib.quote(to_file)
|
|
|
|
else:
|
2010-02-11 02:43:18 +00:00
|
|
|
try:
|
|
|
|
rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
|
|
|
|
except UnknownAliasError, e:
|
2010-06-07 01:02:15 +00:00
|
|
|
e.display(stderr)
|
2010-02-11 02:43:18 +00:00
|
|
|
return 1
|
2009-12-27 20:10:43 +00:00
|
|
|
if path.startswith("/"):
|
2010-06-07 01:02:15 +00:00
|
|
|
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)
|
2009-12-27 20:10:43 +00:00
|
|
|
return 1
|
2008-08-02 02:27:29 +00:00
|
|
|
url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
|
|
|
|
if path:
|
|
|
|
url += escape_path(path)
|
2008-05-20 02:28:50 +00:00
|
|
|
else:
|
2008-08-02 02:27:29 +00:00
|
|
|
# unlinked upload
|
2008-05-20 02:28:50 +00:00
|
|
|
url = nodeurl + "uri"
|
2008-05-20 19:36:55 +00:00
|
|
|
if mutable:
|
|
|
|
url += "?mutable=true"
|
2011-08-02 02:16:13 +00:00
|
|
|
if mutable_type:
|
|
|
|
assert mutable
|
|
|
|
url += "&mutable-type=%s" % mutable_type
|
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
if from_file:
|
2010-07-18 01:34:35 +00:00
|
|
|
infileobj = open(os.path.expanduser(from_file), "rb")
|
2008-05-20 02:28:50 +00:00
|
|
|
else:
|
|
|
|
# do_http() can't use stdin directly: for one thing, we need a
|
|
|
|
# Content-Length field. So we currently must copy it.
|
|
|
|
if verbosity > 0:
|
|
|
|
print >>stderr, "waiting for file data on stdin.."
|
|
|
|
data = stdin.read()
|
|
|
|
infileobj = StringIO(data)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-10-12 05:29:23 +00:00
|
|
|
resp = do_http("PUT", url, infileobj)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-10-12 05:29:23 +00:00
|
|
|
if resp.status in (200, 201,):
|
2010-06-07 01:02:15 +00:00
|
|
|
print >>stderr, format_http_success(resp)
|
|
|
|
print >>stdout, quote_output(resp.read(), quotemarks=False)
|
2007-10-12 05:29:23 +00:00
|
|
|
return 0
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
print >>stderr, format_http_error("Error", resp)
|
2007-08-16 23:30:39 +00:00
|
|
|
return 1
|