2007-08-16 19:15:38 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2007-10-12 05:29:23 +00:00
|
|
|
import urllib
|
|
|
|
from allmydata.scripts.common_http import do_http
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-10-12 02:20:41 +00:00
|
|
|
def put(nodeurl, root_uri, local_fname, vdrive_fname, verbosity,
|
|
|
|
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 += "/"
|
|
|
|
url = nodeurl + "uri/%s/" % urllib.quote(root_uri.replace("/","!"))
|
2007-08-16 19:15:38 +00:00
|
|
|
if vdrive_fname:
|
2007-10-27 01:30:44 +00:00
|
|
|
url += urllib.quote(vdrive_fname)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-08-17 21:59:49 +00:00
|
|
|
infileobj = open(local_fname, "rb")
|
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,):
|
|
|
|
print >>stdout, "%s %s" % (resp.status, resp.reason)
|
|
|
|
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
|
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
def main():
|
2007-08-17 19:54:47 +00:00
|
|
|
import optparse, re
|
2007-08-16 19:15:38 +00:00
|
|
|
parser = optparse.OptionParser()
|
2007-08-17 19:54:47 +00:00
|
|
|
parser.add_option("-u", "--node-url", dest="nodeurl")
|
2007-10-11 07:30:36 +00:00
|
|
|
parser.add_option("-r", "--root-uri", dest="rooturi")
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2007-08-17 19:54:47 +00:00
|
|
|
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
|
|
|
|
if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
|
|
|
|
raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
|
2007-10-11 07:30:36 +00:00
|
|
|
|
|
|
|
if not options.rooturi:
|
|
|
|
raise ValueError("must provide --root-uri")
|
2007-11-01 22:28:58 +00:00
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
local_file = args[0]
|
2007-08-17 20:23:16 +00:00
|
|
|
vdrive_fname = None
|
2007-08-16 19:15:38 +00:00
|
|
|
if len(args) > 1:
|
2007-08-17 20:23:16 +00:00
|
|
|
vdrive_fname = args[1]
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-10-11 07:30:36 +00:00
|
|
|
return put(options.nodeurl, options.rooturi, vdrive_fname, local_file)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|