]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_put.py
remove the slash-to-bang conversion from CLI tools and webapi.txt
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_put.py
1 #!/usr/bin/env python
2
3 import urllib
4 from allmydata.scripts.common_http import do_http
5
6 def put(nodeurl, root_uri, local_fname, vdrive_fname, verbosity,
7         stdout, stderr):
8     """
9     @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
10
11     @return: a Deferred which eventually fires with the exit code
12     """
13     if nodeurl[-1] != "/":
14         nodeurl += "/"
15     url = nodeurl + "uri/%s/" % urllib.quote(root_uri)
16     if vdrive_fname:
17         url += urllib.quote(vdrive_fname)
18
19     infileobj = open(local_fname, "rb")
20     resp = do_http("PUT", url, infileobj)
21
22     if resp.status in (200, 201,):
23         print >>stdout, "%s %s" % (resp.status, resp.reason)
24         return 0
25
26     print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
27     print >>stderr, resp.read()
28     return 1
29
30 def main():
31     import optparse, re
32     parser = optparse.OptionParser()
33     parser.add_option("-u", "--node-url", dest="nodeurl")
34     parser.add_option("-r", "--root-uri", dest="rooturi")
35
36     (options, args) = parser.parse_args()
37
38     NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
39     if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
40         raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
41
42     if not options.rooturi:
43         raise ValueError("must provide --root-uri")
44
45     local_file = args[0]
46     vdrive_fname = None
47     if len(args) > 1:
48         vdrive_fname = args[1]
49
50     return put(options.nodeurl, options.rooturi, vdrive_fname, local_file)
51
52 if __name__ == '__main__':
53     main()