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