74 lines
1.8 KiB
Python
74 lines
1.8 KiB
Python
import argparse
|
|
import pathlib
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
import patch
|
|
from tmux import TmuxSession
|
|
|
|
|
|
def parse_args():
|
|
""" Parse command-line arguments """
|
|
parser = argparse.ArgumentParser(prog="patch2vimedit")
|
|
parser.add_argument(
|
|
"-C",
|
|
"--directory",
|
|
metavar="DIR",
|
|
help="Change to DIR before performing any operations.",
|
|
)
|
|
parser.add_argument(
|
|
"patch",
|
|
help=(
|
|
"The patch file to be applied. If '-' is supplied, reads from STDIN "
|
|
"instead."
|
|
),
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def get_patch(patch_path):
|
|
if patch_path == "-":
|
|
return patch.PatchSet(sys.stdin)
|
|
with open(patch_path, "rb") as stream:
|
|
return patch.PatchSet(stream)
|
|
|
|
|
|
def main():
|
|
""" Entry-point function """
|
|
args = parse_args()
|
|
|
|
if args.directory:
|
|
chdir_to = pathlib.Path(args.directory)
|
|
if not chdir_to.is_dir():
|
|
print(
|
|
(
|
|
"The required working directory '{}' does not exist, or is not a "
|
|
"directory."
|
|
).format(str(chdir_to)),
|
|
file=sys.stderr,
|
|
)
|
|
sys.exit(1)
|
|
os.chdir(chdir_to)
|
|
|
|
patchset = get_patch(args.patch)
|
|
|
|
tmux_session = TmuxSession.create_detached()
|
|
tmux_fg_process = tmux_session.process_attach(read_only=True)
|
|
|
|
time.sleep(0.2)
|
|
tmux_session.send_keys('echo "Hello, world!"', "enter")
|
|
time.sleep(1)
|
|
tmux_session.send_keys('echo "Bye, world!"', "enter")
|
|
time.sleep(1)
|
|
tmux_session.send_keys("exit", "enter")
|
|
|
|
tmux_fg_process.join(1)
|
|
if tmux_fg_process.exitcode is None: # Did not terminate -- kill it
|
|
tmux_fg_process.kill()
|
|
if tmux_session.session_exists():
|
|
tmux_session.session.kill_session()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|