]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_put.py
CLI: add put --mutable, enhance ls to show mutable vs immutable as rw/r-
[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(nodeurl, aliases, from_file, to_file, mutable,
8         verbosity, stdin, stdout, stderr):
9     """
10     @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
11
12     @return: a Deferred which eventually fires with the exit code
13     """
14     if nodeurl[-1] != "/":
15         nodeurl += "/"
16     if to_file:
17         rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
18         url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
19         if path:
20             url += escape_path(path)
21     else:
22         url = nodeurl + "uri"
23     if mutable:
24         url += "?mutable=true"
25     if from_file:
26         infileobj = open(from_file, "rb")
27     else:
28         # do_http() can't use stdin directly: for one thing, we need a
29         # Content-Length field. So we currently must copy it.
30         if verbosity > 0:
31             print >>stderr, "waiting for file data on stdin.."
32         data = stdin.read()
33         infileobj = StringIO(data)
34
35     resp = do_http("PUT", url, infileobj)
36
37     if resp.status in (200, 201,):
38         print >>stderr, "%s %s" % (resp.status, resp.reason)
39         print >>stdout, resp.read()
40         return 0
41
42     print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
43     print >>stderr, resp.read()
44     return 1