From aa00db1896f2fa2eb64b71591fb27c23cff484a2 Mon Sep 17 00:00:00 2001 From: dst27 <72784348+dst27@users.noreply.github.com> Date: Sat, 19 Jun 2021 19:11:28 +0530 Subject: [PATCH 1/2] Fix compatibility with Windows Replaced the usage of tempfile's NamedTemporaryFile() method with the mkstemp() method for the generation of the temp rclone config. The new method requires manual removal of the file but allows the wrapper to work on Windows. --- rclone.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/rclone.py b/rclone.py index 3798036..2d758ae 100644 --- a/rclone.py +++ b/rclone.py @@ -23,6 +23,7 @@ import logging import subprocess import tempfile +import os class RClone: @@ -85,17 +86,17 @@ def run_cmd(self, command, extra_args=[]): - 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 + cfg_fd, cfg_name = tempfile.mkstemp() + with open(cfg_name, 'w') as conf_file: + conf_file.write(self.cfg) + conf_file.flush() + + command_with_args = ["rclone", command, "--config", cfg_name] + command_with_args += extra_args + command_result = self._execute(command_with_args) + os.close(cfg_fd) + os.remove(cfg_name) + return command_result def copy(self, source, dest, flags=[]): """ From e44f1e90ce423e8a1491d371783518e8663c1766 Mon Sep 17 00:00:00 2001 From: dst27 <72784348+dst27@users.noreply.github.com> Date: Tue, 22 Jun 2021 18:45:56 +0530 Subject: [PATCH 2/2] Close config file before invoking rclone In general, rclone may attempt to update a config file and fail if it cannot do so. With this change, the file descriptor returned by mkstemp() is closed before invoking rclone. --- rclone.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rclone.py b/rclone.py index 2d758ae..3efad15 100644 --- a/rclone.py +++ b/rclone.py @@ -87,14 +87,12 @@ def run_cmd(self, command, extra_args=[]): """ # save the configuration in a temporary file cfg_fd, cfg_name = tempfile.mkstemp() - with open(cfg_name, 'w') as conf_file: + with open(cfg_fd, 'w') as conf_file: conf_file.write(self.cfg) - conf_file.flush() command_with_args = ["rclone", command, "--config", cfg_name] command_with_args += extra_args command_result = self._execute(command_with_args) - os.close(cfg_fd) os.remove(cfg_name) return command_result