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

Last change on this file was 58d6f9f, checked in by Itamar Turner-Trauring <itamar@…>, at 2021-06-03T13:28:10Z

Port to Python 3.

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