-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrclone.py
164 lines (137 loc) · 5.3 KB
/
rclone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
A Python wrapper for rclone.
"""
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=W0102,W0703,C0103
import logging
import subprocess
import tempfile
class RClone:
"""
Wrapper class for rclone.
"""
def __init__(self, cfg):
self.cfg = cfg.replace("\\n", "\n")
self.log = logging.getLogger("RClone")
def _execute(self, command_with_args):
"""
Execute the given `command_with_args` using Popen
Args:
- command_with_args (list) : An array with the command to execute,
and its arguments. Each argument is given
as a new element in the list.
"""
self.log.debug("Invoking : %s", " ".join(command_with_args))
try:
with subprocess.Popen(
command_with_args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as proc:
(out, err) = proc.communicate()
#out = proc.stdout.read()
#err = proc.stderr.read()
self.log.debug(out)
if err:
self.log.warning(err.decode("utf-8").replace("\\n", "\n"))
return {
"code": proc.returncode,
"out": out,
"error": err
}
except FileNotFoundError as not_found_e:
self.log.error("Executable not found. %s", not_found_e)
return {
"code": -20,
"error": not_found_e
}
except Exception as generic_e:
self.log.exception("Error running command. Reason: %s", generic_e)
return {
"code": -30,
"error": generic_e
}
def run_cmd(self, command, extra_args=[]):
"""
Execute rclone command
Args:
- command (string): the rclone command to execute.
- extra_args (list): extra arguments to be passed to the rclone command
"""
# save the configuration in a temporary file
with tempfile.NamedTemporaryFile(mode='wt', delete=True) as cfg_file:
# cfg_file is automatically cleaned up by python
self.log.debug("rclone config: ~%s~", self.cfg)
cfg_file.write(self.cfg)
cfg_file.flush()
command_with_args = ["rclone", command, "--config", cfg_file.name]
command_with_args += extra_args
command_result = self._execute(command_with_args)
cfg_file.close()
return command_result
def copy(self, source, dest, flags=[]):
"""
Executes: rclone copy source:path dest:path [flags]
Args:
- source (string): A string "source:path"
- dest (string): A string "dest:path"
- flags (list): Extra flags as per `rclone copy --help` flags.
"""
return self.run_cmd(command="copy", extra_args=[source] + [dest] + flags)
def sync(self, source, dest, flags=[]):
"""
Executes: rclone sync source:path dest:path [flags]
Args:
- source (string): A string "source:path"
- dest (string): A string "dest:path"
- flags (list): Extra flags as per `rclone sync --help` flags.
"""
return self.run_cmd(command="sync", extra_args=[source] + [dest] + flags)
def listremotes(self, flags=[]):
"""
Executes: rclone listremotes [flags]
Args:
- flags (list): Extra flags as per `rclone listremotes --help` flags.
"""
return self.run_cmd(command="listremotes", extra_args=flags)
def ls(self, dest, flags=[]):
"""
Executes: rclone ls remote:path [flags]
Args:
- dest (string): A string "remote:path" representing the location to list.
"""
return self.run_cmd(command="ls", extra_args=[dest] + flags)
def lsjson(self, dest, flags=[]):
"""
Executes: rclone lsjson remote:path [flags]
Args:
- dest (string): A string "remote:path" representing the location to list.
"""
return self.run_cmd(command="lsjson", extra_args=[dest] + flags)
def delete(self, dest, flags=[]):
"""
Executes: rclone delete remote:path
Args:
- dest (string): A string "remote:path" representing the location to delete.
"""
return self.run_cmd(command="delete", extra_args=[dest] + flags)
def with_config(cfg):
"""
Configure a new RClone instance.
"""
inst = RClone(cfg=cfg)
return inst