]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_put.py
f67ebc5ca636226b316e25874c1066637da138c8
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_put.py
1
2 from cStringIO import StringIO
3 import os.path
4 import urllib
5 from allmydata.scripts.common_http import do_http
6 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
7
8 def put(options):
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     nodeurl = options['node-url']
15     aliases = options.aliases
16     from_file = options.from_file
17     to_file = options.to_file
18     mutable = options['mutable']
19     if options['quiet']:
20         verbosity = 0
21     else:
22         verbosity = 2
23     stdin = options.stdin
24     stdout = options.stdout
25     stderr = options.stderr
26
27     if nodeurl[-1] != "/":
28         nodeurl += "/"
29     if to_file:
30         # several possibilities for the TO_FILE argument.
31         #  <none> : unlinked upload
32         #  foo : TAHOE_ALIAS/foo
33         #  subdir/foo : TAHOE_ALIAS/subdir/foo
34         #  /oops/subdir/foo : DISALLOWED
35         #  ALIAS:foo  : aliases[ALIAS]/foo
36         #  ALIAS:subdir/foo  : aliases[ALIAS]/subdir/foo
37         #  ALIAS:/oops/subdir/foo : DISALLOWED
38         #  DIRCAP:./foo        : DIRCAP/foo
39         #  DIRCAP:./subdir/foo : DIRCAP/subdir/foo
40         #  MUTABLE-FILE-WRITECAP : filecap
41
42         if to_file.startswith("URI:SSK:"):
43             url = nodeurl + "uri/%s" % urllib.quote(to_file)
44         else:
45             rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
46             if path.startswith("/"):
47                 suggestion = to_file.replace("/", "", 1)
48                 print >>stderr, "ERROR: The VDRIVE filename must not start with a slash"
49                 print >>stderr, "Please try again, perhaps with:", suggestion
50                 return 1
51             url = nodeurl + "uri/%s/" % urllib.quote(rootcap)
52             if path:
53                 url += escape_path(path)
54     else:
55         # unlinked upload
56         url = nodeurl + "uri"
57     if mutable:
58         url += "?mutable=true"
59     if from_file:
60         infileobj = open(os.path.expanduser(from_file), "rb")
61     else:
62         # do_http() can't use stdin directly: for one thing, we need a
63         # Content-Length field. So we currently must copy it.
64         if verbosity > 0:
65             print >>stderr, "waiting for file data on stdin.."
66         data = stdin.read()
67         infileobj = StringIO(data)
68
69     resp = do_http("PUT", url, infileobj)
70
71     if resp.status in (200, 201,):
72         print >>stderr, "%s %s" % (resp.status, resp.reason)
73         print >>stdout, resp.read()
74         return 0
75
76     print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
77     print >>stderr, resp.read()
78     return 1