]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_put.py
eb578becbbd8ea2ca868c6bc531d9b9587a54cfd
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_put.py
1
2 import os
3 from cStringIO import StringIO
4 import urllib
5 from allmydata.scripts.common_http import do_http, format_http_success, format_http_error
6 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
7                                      UnknownAliasError
8 from allmydata.util.encodingutil import quote_output
9
10 def put(options):
11     """
12     @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
13
14     @return: a Deferred which eventually fires with the exit code
15     """
16     nodeurl = options['node-url']
17     aliases = options.aliases
18     from_file = options.from_file
19     to_file = options.to_file
20     mutable = options['mutable']
21     if options['quiet']:
22         verbosity = 0
23     else:
24         verbosity = 2
25     stdin = options.stdin
26     stdout = options.stdout
27     stderr = options.stderr
28
29     if nodeurl[-1] != "/":
30         nodeurl += "/"
31     if to_file:
32         # several possibilities for the TO_FILE argument.
33         #  <none> : unlinked upload
34         #  foo : TAHOE_ALIAS/foo
35         #  subdir/foo : TAHOE_ALIAS/subdir/foo
36         #  /oops/subdir/foo : DISALLOWED
37         #  ALIAS:foo  : aliases[ALIAS]/foo
38         #  ALIAS:subdir/foo  : aliases[ALIAS]/subdir/foo
39
40         #  ALIAS:/oops/subdir/foo : DISALLOWED
41         #  DIRCAP:./foo        : DIRCAP/foo
42         #  DIRCAP:./subdir/foo : DIRCAP/subdir/foo
43         #  MUTABLE-FILE-WRITECAP : filecap
44
45         # FIXME: this shouldn't rely on a particular prefix.
46         if to_file.startswith("URI:SSK:"):
47             url = nodeurl + "uri/%s" % urllib.quote(to_file)
48         else:
49             try:
50                 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
51             except UnknownAliasError, e:
52                 e.display(stderr)
53                 return 1
54             if path.startswith("/"):
55                 suggestion = to_file.replace(u"/", u"", 1)
56                 print >>stderr, "Error: The remote filename must not start with a slash"
57                 print >>stderr, "Please try again, perhaps with %s" % quote_output(suggestion)
58                 return 1
59             url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
60             if path:
61                 url += escape_path(path)
62     else:
63         # unlinked upload
64         url = nodeurl + "uri"
65     if mutable:
66         url += "?mutable=true"
67     if from_file:
68         infileobj = open(os.path.expanduser(from_file), "rb")
69     else:
70         # do_http() can't use stdin directly: for one thing, we need a
71         # Content-Length field. So we currently must copy it.
72         if verbosity > 0:
73             print >>stderr, "waiting for file data on stdin.."
74         data = stdin.read()
75         infileobj = StringIO(data)
76
77     resp = do_http("PUT", url, infileobj)
78
79     if resp.status in (200, 201,):
80         print >>stderr, format_http_success(resp)
81         print >>stdout, quote_output(resp.read(), quotemarks=False)
82         return 0
83
84     print >>stderr, format_http_error("Error", resp)
85     return 1