From b05e663079dc60ec6c45e62b4d9d23a39a31a6b3 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Oct 2020 09:09:32 +0300 Subject: chore: update runcmd.py from Subplot, use new getter functions --- subplot/daemon.py | 9 ++++--- subplot/runcmd.py | 74 ++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 31 deletions(-) (limited to 'subplot') diff --git a/subplot/daemon.py b/subplot/daemon.py index 5fd959a..c6285bf 100644 --- a/subplot/daemon.py +++ b/subplot/daemon.py @@ -12,6 +12,8 @@ import time def start_daemon(ctx, name, argv): runcmd_run = globals()["runcmd_run"] runcmd_exit_code_is = globals()["runcmd_exit_code_is"] + runcmd_get_exit_code = globals()["runcmd_get_exit_code"] + runcmd_get_stderr = globals()["runcmd_get_stderr"] logging.debug(f"Starting daemon {name}") logging.debug(f" ctx={ctx.as_dict()}") @@ -44,9 +46,10 @@ def start_daemon(ctx, name, argv): # Wait for a bit for daemon to start and maybe find a problem and die. time.sleep(3) - ns = ctx.declare("_runcmd") - if ns["exit"] != 0: - logging.error(f"obnam-server stderr: {ns['stderr']}") + exit = runcmd_get_exit_code(ctx) + stderr = runcmd_get_stderr(ctx) + if exit != 0: + logging.error(f"obnam-server stderr: {stderr}") runcmd_exit_code_is(ctx, 0) this["pid"] = int(open(this["pid-file"]).read().strip()) diff --git a/subplot/runcmd.py b/subplot/runcmd.py index 6ea5e47..532b60b 100644 --- a/subplot/runcmd.py +++ b/subplot/runcmd.py @@ -9,6 +9,38 @@ import subprocess # Helper functions. # +# Get exit code or other stored data about the latest command run by +# runcmd_run. + + +def _runcmd_get(ctx, name): + ns = ctx.declare("_runcmd") + return ns[name] + + +def runcmd_get_exit_code(ctx): + return _runcmd_get(ctx, "exit") + + +def runcmd_get_stdout(ctx): + return _runcmd_get(ctx, "stdout") + + +def runcmd_get_stdout_raw(ctx): + return _runcmd_get(ctx, "stdout.raw") + + +def runcmd_get_stderr(ctx): + return _runcmd_get(ctx, "stderr") + + +def runcmd_get_stderr_raw(ctx): + return _runcmd_get(ctx, "stderr.raw") + + +def runcmd_get_argv(ctx): + return _runcmd_get(ctx, "argv") + # Run a command, given an argv and other arguments for subprocess.Popen. # @@ -88,8 +120,7 @@ def runcmd_exit_code_is_zero(ctx): def runcmd_exit_code_is(ctx, exit=None): assert_eq = globals()["assert_eq"] - ns = ctx.declare("_runcmd") - assert_eq(ns["exit"], int(exit)) + assert_eq(runcmd_get_exit_code(ctx), int(exit)) def runcmd_exit_code_is_nonzero(ctx): @@ -98,8 +129,7 @@ def runcmd_exit_code_is_nonzero(ctx): def runcmd_exit_code_is_not(ctx, exit=None): assert_ne = globals()["assert_ne"] - ns = ctx.declare("_runcmd") - assert_ne(ns["exit"], int(exit)) + assert_ne(runcmd_get_exit_code(ctx), int(exit)) # @@ -108,23 +138,19 @@ def runcmd_exit_code_is_not(ctx, exit=None): def runcmd_stdout_is(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_is(ns["stdout"], text) + _runcmd_output_is(runcmd_get_stdout(ctx), text) def runcmd_stdout_isnt(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_isnt(ns["stdout"], text) + _runcmd_output_isnt(runcmd_get_stdout(ctx), text) def runcmd_stderr_is(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_is(ns["stderr"], text) + _runcmd_output_is(runcmd_get_stderr(ctx), text) def runcmd_stderr_isnt(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_isnt(ns["stderr"], text) + _runcmd_output_isnt(runcmd_get_stderr(ctx), text) def _runcmd_output_is(actual, wanted): @@ -146,23 +172,19 @@ def _runcmd_output_isnt(actual, wanted): def runcmd_stdout_contains(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_contains(ns["stdout"], text) + _runcmd_output_contains(runcmd_get_stdout(ctx), text) def runcmd_stdout_doesnt_contain(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_doesnt_contain(ns["stdout"], text) + _runcmd_output_doesnt_contain(runcmd_get_stdout(ctx), text) def runcmd_stderr_contains(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_contains(ns["stderr"], text) + _runcmd_output_contains(runcmd_get_stderr(ctx), text) def runcmd_stderr_doesnt_contain(ctx, text=None): - ns = ctx.declare("_runcmd") - _runcmd_output_doesnt_contain(ns["stderr"], text) + _runcmd_output_doesnt_contain(runcmd_get_stderr(ctx), text) def _runcmd_output_contains(actual, wanted): @@ -184,23 +206,19 @@ def _runcmd_output_doesnt_contain(actual, wanted): def runcmd_stdout_matches_regex(ctx, regex=None): - ns = ctx.declare("_runcmd") - _runcmd_output_matches_regex(ns["stdout"], regex) + _runcmd_output_matches_regex(runcmd_get_stdout(ctx), regex) def runcmd_stdout_doesnt_match_regex(ctx, regex=None): - ns = ctx.declare("_runcmd") - _runcmd_output_doesnt_match_regex(ns["stdout"], regex) + _runcmd_output_doesnt_match_regex(runcmd_get_stdout(ctx), regex) def runcmd_stderr_matches_regex(ctx, regex=None): - ns = ctx.declare("_runcmd") - _runcmd_output_matches_regex(ns["stderr"], regex) + _runcmd_output_matches_regex(runcmd_get_stderr(ctx), regex) def runcmd_stderr_doesnt_match_regex(ctx, regex=None): - ns = ctx.declare("_runcmd") - _runcmd_output_doesnt_match_regex(ns["stderr"], regex) + _runcmd_output_doesnt_match_regex(runcmd_get_stderr(ctx), regex) def _runcmd_output_matches_regex(actual, regex): -- cgit v1.2.1