]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_get.py
Unicode fixes.
[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                                      UnknownAliasError
5 from allmydata.scripts.common_http import do_http, format_http_error
6 from allmydata.util.stringutils import open_unicode
7
8 def get(options):
9     nodeurl = options['node-url']
10     aliases = options.aliases
11     from_file = options.from_file
12     to_file = options.to_file
13     stdout = options.stdout
14     stderr = options.stderr
15
16     if nodeurl[-1] != "/":
17         nodeurl += "/"
18     try:
19         rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
20     except UnknownAliasError, e:
21         e.display(stderr)
22         return 1
23     url = nodeurl + "uri/%s" % urllib.quote(rootcap)
24     if path:
25         url += "/" + escape_path(path)
26
27     resp = do_http("GET", url)
28     if resp.status in (200, 201,):
29         if to_file:
30             outf = open_unicode(to_file, "wb")
31         else:
32             outf = stdout
33         while True:
34             data = resp.read(4096)
35             if not data:
36                 break
37             outf.write(data)
38         if to_file:
39             outf.close()
40         rc = 0
41     else:
42         print >>stderr, format_http_error("Error during GET", resp)
43         rc = 1
44
45     return rc