]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_get.py
abcd927cce59c56caa76dfc8189549bf3d384cb6
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_get.py
1
2 import urllib
3 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
4 from allmydata.scripts.common_http import do_http
5
6 def get(options):
7     nodeurl = options['node-url']
8     aliases = options.aliases
9     from_file = options.from_file
10     to_file = options.to_file
11     stdout = options.stdout
12     stderr = options.stderr
13
14     if nodeurl[-1] != "/":
15         nodeurl += "/"
16     rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
17     url = nodeurl + "uri/%s" % urllib.quote(rootcap)
18     if path:
19         url += "/" + escape_path(path)
20
21     resp = do_http("GET", url)
22     if resp.status in (200, 201,):
23         if to_file:
24             outf = open(to_file, "wb")
25         else:
26             outf = stdout
27         while True:
28             data = resp.read(4096)
29             if not data:
30                 break
31             outf.write(data)
32         if to_file:
33             outf.close()
34         rc = 0
35     else:
36         print >>stderr, "Error, got %s %s" % (resp.status, resp.reason)
37         print >>stderr, resp.read()
38         rc = 1
39
40     return rc