diff --git a/rclone.py b/rclone.py index 3798036..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): + def _execute(self, command_with_args, redirect_stdout=True, redirect_stderr=True): """ 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=True, redirect_stderr=True): """ 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_stdout, redirect_stderr=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