source: trunk/src/allmydata/scripts/tahoe_get.py

Last change on this file was 53084f7, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-27T23:49:07Z

remove more Python2 compatibility

  • Property mode set to 100644
File size: 1.4 KB
Line 
1"""
2Ported to Python 3.
3"""
4
5from urllib.parse import quote as url_quote
6from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
7                                     UnknownAliasError
8from allmydata.scripts.common_http import do_http, format_http_error
9
10def get(options):
11    nodeurl = options['node-url']
12    aliases = options.aliases
13    from_file = options.from_file
14    to_file = options.to_file
15    stdout = options.stdout
16    stderr = options.stderr
17
18    if nodeurl[-1] != "/":
19        nodeurl += "/"
20    try:
21        rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
22    except UnknownAliasError as e:
23        e.display(stderr)
24        return 1
25    url = nodeurl + "uri/%s" % url_quote(rootcap)
26    if path:
27        url += "/" + escape_path(path)
28
29    resp = do_http("GET", url)
30    if resp.status in (200, 201,):
31        if to_file:
32            outf = open(to_file, "wb")
33        else:
34            outf = stdout
35            # Make sure we can write bytes; on Python 3 stdout is Unicode by
36            # default.
37            if getattr(outf, "encoding", None) is not None:
38                outf = outf.buffer
39        while True:
40            data = resp.read(4096)
41            if not data:
42                break
43            outf.write(data)
44        if to_file:
45            outf.close()
46        rc = 0
47    else:
48        print(format_http_error("Error during GET", resp), file=stderr)
49        rc = 1
50
51    return rc
Note: See TracBrowser for help on using the repository browser.