From 5927c79e55c2c0d970c91441be9d9cc1b65b1623 Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 30 Dec 2021 13:34:31 -0700 Subject: [PATCH 1/3] added option to not redirect std out and std err --- rclone.py | 17 +++++++++++------ rclone_test.py | 4 ++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/rclone.py b/rclone.py index 3798036..e83d88e 100644 --- a/rclone.py +++ b/rclone.py @@ -34,7 +34,7 @@ def __init__(self, cfg): self.cfg = cfg.replace("\\n", "\n") self.log = logging.getLogger("RClone") - def _execute(self, command_with_args): + def _execute(self, command_with_args, redirect_stdout=False, redirect_stderr=False): """ Execute the given `command_with_args` using Popen @@ -45,10 +45,12 @@ def _execute(self, command_with_args): """ self.log.debug("Invoking : %s", " ".join(command_with_args)) try: + stdout = subprocess.PIPE if redirect_stdout else None + stderr = subprocess.PIPE if redirect_stderr else None with subprocess.Popen( command_with_args, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) as proc: + stdout=stdout, + stderr=stderr) as proc: (out, err) = proc.communicate() #out = proc.stdout.read() @@ -75,8 +77,9 @@ def _execute(self, command_with_args): "code": -30, "error": generic_e } - - def run_cmd(self, command, extra_args=[]): + + + def run_cmd(self, command, extra_args=[], redirect_stdout=False, redirect_stderr=False): """ Execute rclone command @@ -93,7 +96,9 @@ def run_cmd(self, command, extra_args=[]): command_with_args = ["rclone", command, "--config", cfg_file.name] command_with_args += extra_args - command_result = self._execute(command_with_args) + + command_result = self._execute(command_with_args, redirect_stdout, redirect_stderr) + cfg_file.close() return command_result diff --git a/rclone_test.py b/rclone_test.py index 1afd4f5..54459bc 100644 --- a/rclone_test.py +++ b/rclone_test.py @@ -104,3 +104,7 @@ def test_copy_lsjson_and_delete(self): self.assertEqual(result.get('code'), 0) result_json = json.loads(result.get('out').decode("utf-8")) self.assertEqual(len(result_json), 0) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 7ac37fcd80a4f3fb8c85c99e3a725dd4c1567e0d Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 30 Dec 2021 13:41:15 -0700 Subject: [PATCH 2/3] fixed default values for redirect flags --- rclone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rclone.py b/rclone.py index e83d88e..fbd625b 100644 --- a/rclone.py +++ b/rclone.py @@ -79,7 +79,7 @@ def _execute(self, command_with_args, redirect_stdout=False, redirect_stderr=Fal } - def run_cmd(self, command, extra_args=[], redirect_stdout=False, redirect_stderr=False): + def run_cmd(self, command, extra_args=[], redirect_stdout=True, redirect_stderr=True): """ Execute rclone command From 7a83bf93565d3418214f4016ceb6d64c2f9ae610 Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 30 Dec 2021 13:42:59 -0700 Subject: [PATCH 3/3] fixed default values for redirect flags --- rclone.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rclone.py b/rclone.py index fbd625b..3e379f2 100644 --- a/rclone.py +++ b/rclone.py @@ -34,7 +34,7 @@ def __init__(self, cfg): self.cfg = cfg.replace("\\n", "\n") self.log = logging.getLogger("RClone") - def _execute(self, command_with_args, redirect_stdout=False, redirect_stderr=False): + def _execute(self, command_with_args, redirect_stdout=True, redirect_stderr=True): """ Execute the given `command_with_args` using Popen @@ -97,7 +97,7 @@ def run_cmd(self, command, extra_args=[], redirect_stdout=True, redirect_stderr= command_with_args = ["rclone", command, "--config", cfg_file.name] command_with_args += extra_args - command_result = self._execute(command_with_args, redirect_stdout, redirect_stderr) + command_result = self._execute(command_with_args, redirect_stdout=redirect_stdout, redirect_stderr=redirect_stderr) cfg_file.close() return command_result