]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_put.py
3dc13773fa75a8a62c97e9c4bce68451330940a5
[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     mutable_type = False
22
23     if mutable:
24         mutable_type = options['mutable-type']
25     if options['quiet']:
26         verbosity = 0
27     else:
28         verbosity = 2
29     stdin = options.stdin
30     stdout = options.stdout
31     stderr = options.stderr
32
33     if nodeurl[-1] != "/":
34         nodeurl += "/"
35     if to_file:
36         # several possibilities for the TO_FILE argument.
37         #  <none> : unlinked upload
38         #  foo : TAHOE_ALIAS/foo
39         #  subdir/foo : TAHOE_ALIAS/subdir/foo
40         #  /oops/subdir/foo : DISALLOWED
41         #  ALIAS:foo  : aliases[ALIAS]/foo
42         #  ALIAS:subdir/foo  : aliases[ALIAS]/subdir/foo
43
44         #  ALIAS:/oops/subdir/foo : DISALLOWED
45         #  DIRCAP:./foo        : DIRCAP/foo
46         #  DIRCAP:./subdir/foo : DIRCAP/subdir/foo
47         #  MUTABLE-FILE-WRITECAP : filecap
48
49         # FIXME: don't hardcode cap format.
50         if to_file.startswith("URI:MDMF:") or to_file.startswith("URI:SSK:"):
51             url = nodeurl + "uri/%s" % urllib.quote(to_file)
52         else:
53             try:
54                 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
55             except UnknownAliasError, e:
56                 e.display(stderr)
57                 return 1
58             if path.startswith("/"):
59                 suggestion = to_file.replace(u"/", u"", 1)
60                 print >>stderr, "Error: The remote filename must not start with a slash"
61                 print >>stderr, "Please try again, perhaps with %s" % quote_output(suggestion)
62                 return 1
63             url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
64             if path:
65                 url += escape_path(path)
66     else:
67         # unlinked upload
68         url = nodeurl + "uri"
69     if mutable:
70         url += "?mutable=true"
71     if mutable_type:
72         assert mutable
73         url += "&mutable-type=%s" % mutable_type
74
75     if from_file:
76         infileobj = open(os.path.expanduser(from_file), "rb")
77     else:
78         # do_http() can't use stdin directly: for one thing, we need a
79         # Content-Length field. So we currently must copy it.
80         if verbosity > 0:
81             print >>stderr, "waiting for file data on stdin.."
82         data = stdin.read()
83         infileobj = StringIO(data)
84
85     resp = do_http("PUT", url, infileobj)
86
87     if resp.status in (200, 201,):
88         print >>stderr, format_http_success(resp)
89         print >>stdout, quote_output(resp.read(), quotemarks=False)
90         return 0
91
92     print >>stderr, format_http_error("Error", resp)
93     return 1