2007-08-16 19:15:38 +00:00
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
from cStringIO import StringIO
|
2007-10-12 05:29:23 +00:00
|
|
|
import urllib
|
|
|
|
from allmydata.scripts.common_http import do_http
|
2008-05-20 02:28:50 +00:00
|
|
|
from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2008-05-20 02:28:50 +00:00
|
|
|
def put(nodeurl, aliases, from_file, to_file, verbosity,
|
|
|
|
stdin, stdout, stderr):
|
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
|
|
|
|
"""
|
2007-10-12 05:29:23 +00:00
|
|
|
if nodeurl[-1] != "/":
|
|
|
|
nodeurl += "/"
|
2008-05-20 02:28:50 +00:00
|
|
|
if to_file:
|
|
|
|
rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
|
|
|
|
url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
|
|
|
|
if path:
|
|
|
|
url += escape_path(path)
|
|
|
|
else:
|
|
|
|
url = nodeurl + "uri"
|
|
|
|
if from_file:
|
|
|
|
infileobj = open(from_file, "rb")
|
|
|
|
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,):
|
2008-05-20 02:28:50 +00:00
|
|
|
print >>stderr, "%s %s" % (resp.status, resp.reason)
|
|
|
|
print >>stdout, resp.read()
|
2007-10-12 05:29:23 +00:00
|
|
|
return 0
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-10-12 05:29:23 +00:00
|
|
|
print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
|
|
|
|
print >>stderr, resp.read()
|
2007-08-16 23:30:39 +00:00
|
|
|
return 1
|