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

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 2.5 KB
Line 
1"""
2Ported to Python 3.
3"""
4
5import re
6from urllib.parse import quote as url_quote
7import json
8from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
9                                     UnknownAliasError
10from allmydata.scripts.common_http import do_http, format_http_error
11from allmydata.util.encodingutil import to_bytes
12
13# this script is used for both 'mv' and 'ln'
14
15def mv(options, mode="move"):
16    nodeurl = options['node-url']
17    aliases = options.aliases
18    from_file = options.from_file
19    to_file = options.to_file
20    stdout = options.stdout
21    stderr = options.stderr
22
23    if nodeurl[-1] != "/":
24        nodeurl += "/"
25    try:
26        rootcap, from_path = get_alias(aliases, from_file, DEFAULT_ALIAS)
27    except UnknownAliasError as e:
28        e.display(stderr)
29        return 1
30    from_path = str(from_path, "utf-8")
31    from_url = nodeurl + "uri/%s" % url_quote(rootcap)
32    if from_path:
33        from_url += "/" + escape_path(from_path)
34    # figure out the source cap
35    resp = do_http("GET", from_url + "?t=json")
36    if not re.search(r'^2\d\d$', str(resp.status)):
37        print(format_http_error("Error", resp), file=stderr)
38        return 1
39    data = resp.read()
40    nodetype, attrs = json.loads(data)
41    cap = to_bytes(attrs.get("rw_uri") or attrs["ro_uri"])
42
43    # now get the target
44    try:
45        rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
46    except UnknownAliasError as e:
47        e.display(stderr)
48        return 1
49    to_url = nodeurl + "uri/%s" % url_quote(rootcap)
50    path = str(path, "utf-8")
51    if path:
52        to_url += "/" + escape_path(path)
53
54    if to_url.endswith("/"):
55        # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
56        to_url += escape_path(from_path[from_path.rfind("/")+1:])
57
58    to_url += "?t=uri&replace=only-files"
59
60    resp = do_http("PUT", to_url, cap)
61    status = resp.status
62    if not re.search(r'^2\d\d$', str(status)):
63        if status == 409:
64            print("Error: You can't overwrite a directory with a file", file=stderr)
65        else:
66            print(format_http_error("Error", resp), file=stderr)
67            if mode == "move":
68                print("NOT removing the original", file=stderr)
69        return 1
70
71    if mode == "move":
72        # now remove the original
73        resp = do_http("DELETE", from_url)
74        if not re.search(r'^2\d\d$', str(resp.status)):
75            print(format_http_error("Error deleting original after move", resp), file=stderr)
76            return 2
77
78    print("OK", file=stdout)
79    return 0
Note: See TracBrowser for help on using the repository browser.