]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_put.py
CLI: simplify argument-passing, use options= for everthing, including stdout
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_put.py
1
2 from cStringIO import StringIO
3 import urllib
4 from allmydata.scripts.common_http import do_http
5 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
6
7 def put(options):
8     """
9     @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
10
11     @return: a Deferred which eventually fires with the exit code
12     """
13     nodeurl = options['node-url']
14     aliases = options.aliases
15     from_file = options.from_file
16     to_file = options.to_file
17     mutable = options['mutable']
18     if options['quiet']:
19         verbosity = 0
20     else:
21         verbosity = 2
22     stdin = options.stdin
23     stdout = options.stdout
24     stderr = options.stderr
25
26     if nodeurl[-1] != "/":
27         nodeurl += "/"
28     if to_file:
29         rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
30         url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
31         if path:
32             url += escape_path(path)
33     else:
34         url = nodeurl + "uri"
35     if mutable:
36         url += "?mutable=true"
37     if from_file:
38         infileobj = open(from_file, "rb")
39     else:
40         # do_http() can't use stdin directly: for one thing, we need a
41         # Content-Length field. So we currently must copy it.
42         if verbosity > 0:
43             print >>stderr, "waiting for file data on stdin.."
44         data = stdin.read()
45         infileobj = StringIO(data)
46
47     resp = do_http("PUT", url, infileobj)
48
49     if resp.status in (200, 201,):
50         print >>stderr, "%s %s" % (resp.status, resp.reason)
51         print >>stdout, resp.read()
52         return 0
53
54     print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
55     print >>stderr, resp.read()
56     return 1