From f6497245b9282a037c7a6348ffcf3abcca543490 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Mon, 4 May 2020 22:06:46 +0200 Subject: [PATCH] Fix running commands with directories; also fix editing --- markdownrunner.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/markdownrunner.py b/markdownrunner.py index 463820a..93ac0be 100755 --- a/markdownrunner.py +++ b/markdownrunner.py @@ -128,7 +128,7 @@ def user_action(hunk: Sequence[Command], context=None) -> Sequence[Command]: with tempfile.NamedTemporaryFile(mode='r+') as tmpfile: # Fill the file with data for cmd in hunk: - tmpfile.file.write(f"{cmd.user}\t{cmd.directory}\t{cmd.command}\n") + tmpfile.file.write(f"{cmd.user}\t{cmd.directory if cmd.directory else ''}\t{cmd.command}\n") tmpfile.file.flush() # Should not be needed, but better safe than sorry # Let the user edit the file import os @@ -146,9 +146,11 @@ def user_action(hunk: Sequence[Command], context=None) -> Sequence[Command]: if line == '': continue parts = line.split('\t', maxsplit=2) + # Change directory to None if it is not specified + parts[1] = parts[1].strip() if parts[1].strip() != '' else None new_hunk.append(Command(user=parts[0],directory=parts[1],command=parts[2],context=None)) - # Python has no goto for restarting, but we can recurse - return user_action(new_hunk, context) + # Python has no goto for restarting, but we can recurse + return user_action(new_hunk, context) elif answer.startswith(('y', 'Y')): return hunk elif answer.startswith(('n', 'N')): @@ -160,11 +162,12 @@ def run_command(cmd: Command): # Generate and run the SSH command. global host local_command = ['ssh', host] + remote_command = cmd.command if cmd.user is not None: local_command.extend(['-l', cmd.user]) if cmd.directory is not None: - cmd.command += f'cd {cmd.directory}; {cmd.command}' - local_command.append(cmd.command) + remote_command = f'cd {cmd.directory}; {remote_command}' + local_command.append(remote_command) # Run import subprocess