Skip to content

rclone

RCloneHandler

Bases: AbstractProvider

example preset submission x = {'enabled': True, 'alternative_sites': [], 'root': '/projects', 'rclone_executable_path': {'windows': 'rclone.exe', 'linux': 'rclone ', 'darwin': 'rclone '}, 'config_type': 'config_web', 'config_file': {'remote_name': '', 'rclone_config_path': { 'windows': '', 'linux': '', 'darwin': ''}}, 'config_web': {'type': 's3', 'config_params': [{'key': 'provider', 'value': 'Other'}, {'key': 'access_key_id', 'value': 'changemeuser'}, {'key': 'secret_access_key', 'value': 'changemepass'}, {'key': 'endpoint', 'value': 'bucket.storage.com'}, {'key': 'region', 'value': 'bucket-region'}, {'key': 'acl', 'value': 'private'}, {'key': 'disable_ssl', 'value': 'true'}]}, 'additional_args': []}

Source code in client/ayon_sitesync/providers/rclone.py
 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
class RCloneHandler(AbstractProvider):
    """
    example preset submission
    x = {'enabled': True, 'alternative_sites': [],
     'root': '/projects',
     'rclone_executable_path': {'windows': 'rclone.exe', 'linux': 'rclone ',
                                'darwin': 'rclone '},
     'config_type': 'config_web',
     'config_file': {'remote_name': '',
                      'rclone_config_path': {
                          'windows': '',
                          'linux': '',
                          'darwin': ''}},
     'config_web': {'type': 's3',
                    'config_params': [{'key': 'provider', 'value': 'Other'},
                                      {'key': 'access_key_id',
                                       'value': 'changemeuser'},
                                      {'key': 'secret_access_key',
                                       'value': 'changemepass'},
                                      {'key': 'endpoint',
                                       'value': 'bucket.storage.com'},
                                      {'key': 'region', 'value': 'bucket-region'},
                                      {'key': 'acl', 'value': 'private'},
                                      {'key': 'disable_ssl', 'value': 'true'}]},
     'additional_args': []}
    """

    CODE = "rclone"
    LABEL = "RClone"

    def __init__(self, project_name, site_name, tree=None, presets=None):
        super().__init__(project_name, site_name, tree, presets)
        self.project_name = project_name
        self.presets = presets or {}
        self.log.debug(f"Using rclone presets: {self.presets}")
        rclone_path = self.presets.get("rclone_executable_path", {}).get(
            platform.system().lower(), "rclone"
        )
        self.rclone_path = expand_env_vars(rclone_path)
        self.remote_name = ""
        self.config_path = ""
        self.web_config_params = []
        self.extra_args = self.presets.get("additional_args", [])

        if self.presets["config_type"] == "config_file":
            config_path = (
                self.presets["config_file"]["rclone_config_path"]
                .get(platform.system().lower(), "")
            )
            self.config_path = expand_env_vars(config_path)
            self.log.debug("Config path: %s", self.config_path)
            self.remote_name = self.presets["config_file"]["remote_name"]
            self.log.debug("Remote name: %s", self.remote_name)
            if not self.remote_name:
                self.log.error("Remote name not specified webui")
                raise ValueError("Remote name not specified in webui")
            if not os.path.exists(self.config_path):
                self.log.error(f"Config file not found at {self.config_path}")
                raise FileNotFoundError(
                    f"Config file not found at {self.config_path}"
                )

        if self.presets["config_type"] == "config_web":
            self.web_config_params = self.presets["config_web"]["config_params"]
            self.log.debug("Web config params: %s", self.web_config_params)
            if not self.web_config_params:
                self.log.error("Web config params not specified")
                raise ValueError("Web config params not specified")
            self.web_config_type = self.presets["config_web"]["type"]
            if not self.web_config_type:
                self.log.error("Web config type not specified")
                raise ValueError("Web config type not specified")
            self.remote_name = "WEBREMOTE"

        self._tree = tree

    def is_active(self) -> bool:
        """Check if rclone is available and config exists."""
        try:
            self._run_rclone(["version"])
            self.log.debug("rclone is healthy")
            return True
        except Exception as e:
            self.log.exception(f"rclone is not healthy: {e}")
            return False

    def upload_file(
        self,
        source_path: str,
        target_path: str,
        addon: SiteSyncAddon,
        project_name: str,
        file: dict,
        repre_status: dict,
        site_name: str,
        overwrite: bool = False,
    ) -> str:
        """High-speed upload using rclone copyto."""
        if not os.path.exists(source_path):
            raise FileNotFoundError(
                f"Source file {source_path} doesn't exist."
            )

        if self._path_exists(target_path) and not overwrite:
            raise FileExistsError(
                f"File already exists, use 'overwrite' argument: {target_path}"
            )

        remote_path = self._get_remote_path(target_path)

        args = [
            "copyto",
            source_path,
            remote_path,
            "--fast-list",
            "--contimeout",
            "60s",
        ]
        if not overwrite:
            args.append("--ignore-existing")

        self._run_rclone(args)

        # SiteSync status update
        if addon:
            self.log.debug(
                f"Successfully uploaded {source_path} to {remote_path}"
            )
            self.log.debug(f"Updating SiteSync status for {file['id']}")
            addon.update_db(
                project_name=project_name,
                new_file_id=None,
                file=file,
                repre_status=repre_status,
                site_name=site_name,
                side="remote",
                progress=1.0,
            )

        return target_path

    def download_file(
        self,
        source_path: str,
        local_path: str,
        addon: SiteSyncAddon,
        project_name: str,
        file: dict,
        repre_status: dict,
        site_name: str,
        overwrite: bool = False,
    ) -> str:
        """High-speed download using rclone copyto."""
        if not self._path_exists(source_path):
            raise FileNotFoundError(
                f"Source file {source_path} doesn't exist."
            )

        if os.path.exists(local_path) and not overwrite:
            raise FileExistsError(
                "File already exists, use 'overwrite' argument"
            )

        source_path = self._get_remote_path(source_path)
        args = ["copyto", source_path, local_path]
        if not overwrite:
            args.append("--ignore-existing")

        self._run_rclone(args)

        if addon:
            self.log.debug(
                f"Successfully downloaded {source_path} to {local_path}"
            )
            self.log.debug(f"Updating SiteSync status for {file['id']}")
            addon.update_db(
                project_name=project_name,
                new_file_id=None,
                file=file,
                repre_status=repre_status,
                site_name=site_name,
                side="local",
                progress=1.0,
            )
        return os.path.basename(source_path)

    def delete_file(self, path: str) -> None:
        """Delete a file from the remote."""
        remote_path = self._get_remote_path(path)
        args = ["deletefile", f"{remote_path}"]
        try:
            self._run_rclone(args)
            self.log.debug(
                f"Successfully deleted {path} on {self.remote_name}"
            )
        except RuntimeError as e:
            self.log.error(f"Failed to delete {path}: {e}")
            raise FileNotFoundError(
                f"Failed to delete {path} on {self.remote_name}"
            )

    def list_folder(self, folder_path: str) -> list[str]:
        """List all files in a folder non-recursively."""
        remote_folder_path = self._get_remote_path(folder_path)
        args = ["lsjson", remote_folder_path]
        raw_json = self._run_rclone(args)
        if not raw_json:
            return []
        items = json.loads(raw_json)
        data = []
        for item in items:
            name = item["Name"]
            if not name.startswith("/"):
                name = "/" + name
            data.append(name)
        self.log.debug(f"Fetched list of files in {folder_path}: {data}")
        return data

    def create_folder(self, folder_path: str) -> str:
        """Create a directory on the remote."""
        if self._path_exists(folder_path):
            return folder_path
        remote_folder_path = self._get_remote_path(folder_path)
        args = ["mkdir", remote_folder_path]
        self._run_rclone(args)
        self.log.debug(
            f"Successfully created {folder_path} on {self.remote_name}"
        )
        return folder_path

    def get_tree(self):
        """Not needed here."""
        pass

    def get_roots_config(self, anatomy=None) -> dict:
        """Returns root values for path resolving."""
        return {"root": {"work": self.presets.get("root", "/")}}

    # Helper methods
    def _manage_web_config(self):
        """Manage web config parameters."""
        env = os.environ.copy()
        if self.web_config_params:
            # the config file wins over the web params
            self.log.debug("Updating web config params")
            env[f"RCLONE_CONFIG_{self.remote_name.upper()}_TYPE"] = (
                self.web_config_type
            )
            for param in self.web_config_params:
                key = param.get("key").lower()
                value = param.get("value")
                if key in ("password", "pass"):
                    env_key = f"RCLONE_CONFIG_{self.remote_name.upper()}_PASS"
                    env[env_key] = self._obscure_pass(value)
                else:
                    env_key = f"RCLONE_CONFIG_{self.remote_name.upper()}_{key.upper()}"
                    env[env_key] = value
            null_device = (
                "NUL"
                if platform.system().lower() == "windows"
                else "/dev/null"
            )
            env["RCLONE_CONFIG"] = null_device
        return env

    def _run_rclone(self, args: list[str]) -> str:
        """Internal helper to execute rclone commands with extra args."""
        cmd = [self.rclone_path]
        if self.config_path:
            cmd.extend(["--config", self.config_path])

        if self.extra_args:
            cmd.extend(self.extra_args)
        cmd.extend(args)

        env = self._manage_web_config()

        self.log.debug("Running rclone: %s", " ".join(cmd))

        kwargs = {}
        if platform.system().lower() == "windows":
            kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW

        p = subprocess.run(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=env,
            text=True,
            check=False,
            **kwargs,
        )

        if p.returncode != 0:
            # Rclone exit code 3 means directory not found
            if p.returncode == 3 and "lsjson" in args:
                return ""

            raise RuntimeError(
                f"rclone failed with exit code {p.returncode}\n"
                f"stdout:\n{p.stdout}\n"
                f"stderr:\n{p.stderr}"
            )

        out = p.stdout
        return out

    def _path_exists(self, path: str) -> bool:
        """Checks if path exists on remote."""
        remote_path = self._get_remote_path(path)
        args = ["lsjson", remote_path]
        try:
            output = self._run_rclone(args)
            if not output:
                return False

            parsed = self._parse_rclone_json(output)
            if not parsed:
                self.log.error(
                    f"Path check failed for {remote_path}: empty output"
                )
                return False

            items = json.loads(parsed)
            # If rclone returns a list with items, the path (or its contents) exists.
            # When pointing directly to a file, rclone returns [ { "Path": "filename", ... } ]
            self.log.debug(f"{remote_path} exists")
            return len(items) > 0
        except Exception as e:
            self.log.error(f"Path check failed for {remote_path}: {e}")
            return False

    def _get_remote_path(self, path: str) -> str:
        """Helper to format the path for rclone with the remote name."""
        path = f"{self.remote_name}:{path}"
        self.log.debug(f"Remote Path: {path}")
        return path

    def _obscure_pass(self, password: str) -> str:
        # Rclone expects passwords in env vars to be obscured
        # You can call 'rclone obscure' via subprocess to get this string
        cmd = [self.rclone_path, "obscure", password]
        try:
            output = subprocess.check_output(
                cmd,
                stderr=subprocess.STDOUT,
                creationflags=subprocess.CREATE_NO_WINDOW,
            )
            return output.decode().strip()
        except FileNotFoundError as e:
            self.log.error(
                f"Failed to run rclone for obscuring password: "
                f"executable not found at '{self.rclone_path}'"
            )
            raise RuntimeError(
                "rclone executable not found for password obscuring"
            ) from e
        except subprocess.CalledProcessError as e:
            output = (
                e.output.decode("utf-8", errors="replace") if e.output else ""
            )
            self.log.error(
                "rclone 'obscure' command failed with exit code %s and output: %s",
                e.returncode,
                output,
            )
            raise RuntimeError("Failed to obscure password with rclone") from e

    @staticmethod
    def _parse_rclone_json(output: str | bytes) -> str | None:
        """Parses JSON from rclone output, stripping leading/trailing non-JSON text."""
        if not output:
            return None

        if isinstance(output, (bytes, bytearray)):
            output = output.decode("utf-8", errors="replace")

        # Find the first occurrence of '[' or '{' to skip headers/notices
        start_index = -1
        for i, char in enumerate(output):
            if char in ("[", "{"):
                start_index = i
                break

        if start_index == -1:
            raise ValueError(f"No JSON object found in output: {output}")

        # Find the last occurrence of ']' or '}'
        end_index = -1
        for i, char in enumerate(reversed(output)):
            if char in ("]", "}"):
                end_index = len(output) - i
                break

        return output[start_index:end_index]

create_folder(folder_path)

Create a directory on the remote.

Source code in client/ayon_sitesync/providers/rclone.py
240
241
242
243
244
245
246
247
248
249
250
def create_folder(self, folder_path: str) -> str:
    """Create a directory on the remote."""
    if self._path_exists(folder_path):
        return folder_path
    remote_folder_path = self._get_remote_path(folder_path)
    args = ["mkdir", remote_folder_path]
    self._run_rclone(args)
    self.log.debug(
        f"Successfully created {folder_path} on {self.remote_name}"
    )
    return folder_path

delete_file(path)

Delete a file from the remote.

Source code in client/ayon_sitesync/providers/rclone.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def delete_file(self, path: str) -> None:
    """Delete a file from the remote."""
    remote_path = self._get_remote_path(path)
    args = ["deletefile", f"{remote_path}"]
    try:
        self._run_rclone(args)
        self.log.debug(
            f"Successfully deleted {path} on {self.remote_name}"
        )
    except RuntimeError as e:
        self.log.error(f"Failed to delete {path}: {e}")
        raise FileNotFoundError(
            f"Failed to delete {path} on {self.remote_name}"
        )

download_file(source_path, local_path, addon, project_name, file, repre_status, site_name, overwrite=False)

High-speed download using rclone copyto.

Source code in client/ayon_sitesync/providers/rclone.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def download_file(
    self,
    source_path: str,
    local_path: str,
    addon: SiteSyncAddon,
    project_name: str,
    file: dict,
    repre_status: dict,
    site_name: str,
    overwrite: bool = False,
) -> str:
    """High-speed download using rclone copyto."""
    if not self._path_exists(source_path):
        raise FileNotFoundError(
            f"Source file {source_path} doesn't exist."
        )

    if os.path.exists(local_path) and not overwrite:
        raise FileExistsError(
            "File already exists, use 'overwrite' argument"
        )

    source_path = self._get_remote_path(source_path)
    args = ["copyto", source_path, local_path]
    if not overwrite:
        args.append("--ignore-existing")

    self._run_rclone(args)

    if addon:
        self.log.debug(
            f"Successfully downloaded {source_path} to {local_path}"
        )
        self.log.debug(f"Updating SiteSync status for {file['id']}")
        addon.update_db(
            project_name=project_name,
            new_file_id=None,
            file=file,
            repre_status=repre_status,
            site_name=site_name,
            side="local",
            progress=1.0,
        )
    return os.path.basename(source_path)

get_roots_config(anatomy=None)

Returns root values for path resolving.

Source code in client/ayon_sitesync/providers/rclone.py
256
257
258
def get_roots_config(self, anatomy=None) -> dict:
    """Returns root values for path resolving."""
    return {"root": {"work": self.presets.get("root", "/")}}

get_tree()

Not needed here.

Source code in client/ayon_sitesync/providers/rclone.py
252
253
254
def get_tree(self):
    """Not needed here."""
    pass

is_active()

Check if rclone is available and config exists.

Source code in client/ayon_sitesync/providers/rclone.py
 98
 99
100
101
102
103
104
105
106
def is_active(self) -> bool:
    """Check if rclone is available and config exists."""
    try:
        self._run_rclone(["version"])
        self.log.debug("rclone is healthy")
        return True
    except Exception as e:
        self.log.exception(f"rclone is not healthy: {e}")
        return False

list_folder(folder_path)

List all files in a folder non-recursively.

Source code in client/ayon_sitesync/providers/rclone.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
def list_folder(self, folder_path: str) -> list[str]:
    """List all files in a folder non-recursively."""
    remote_folder_path = self._get_remote_path(folder_path)
    args = ["lsjson", remote_folder_path]
    raw_json = self._run_rclone(args)
    if not raw_json:
        return []
    items = json.loads(raw_json)
    data = []
    for item in items:
        name = item["Name"]
        if not name.startswith("/"):
            name = "/" + name
        data.append(name)
    self.log.debug(f"Fetched list of files in {folder_path}: {data}")
    return data

upload_file(source_path, target_path, addon, project_name, file, repre_status, site_name, overwrite=False)

High-speed upload using rclone copyto.

Source code in client/ayon_sitesync/providers/rclone.py
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
def upload_file(
    self,
    source_path: str,
    target_path: str,
    addon: SiteSyncAddon,
    project_name: str,
    file: dict,
    repre_status: dict,
    site_name: str,
    overwrite: bool = False,
) -> str:
    """High-speed upload using rclone copyto."""
    if not os.path.exists(source_path):
        raise FileNotFoundError(
            f"Source file {source_path} doesn't exist."
        )

    if self._path_exists(target_path) and not overwrite:
        raise FileExistsError(
            f"File already exists, use 'overwrite' argument: {target_path}"
        )

    remote_path = self._get_remote_path(target_path)

    args = [
        "copyto",
        source_path,
        remote_path,
        "--fast-list",
        "--contimeout",
        "60s",
    ]
    if not overwrite:
        args.append("--ignore-existing")

    self._run_rclone(args)

    # SiteSync status update
    if addon:
        self.log.debug(
            f"Successfully uploaded {source_path} to {remote_path}"
        )
        self.log.debug(f"Updating SiteSync status for {file['id']}")
        addon.update_db(
            project_name=project_name,
            new_file_id=None,
            file=file,
            repre_status=repre_status,
            site_name=site_name,
            side="remote",
            progress=1.0,
        )

    return target_path