Skip to content

run

Tools to run commands.

Authors:

  • Stijn De Weirdt (Ghent University)
  • Dries Verdegem (Ghent University)
  • Kenneth Hoste (Ghent University)
  • Pieter De Baets (Ghent University)
  • Jens Timmerman (Ghent University)
  • Toon Willems (Ghent University)
  • Ward Poelmans (Ghent University)

RunShellCmdError

Bases: BaseException

__init__(cmd_result, caller_info, *args, **kwargs)

Constructor for RunShellCmdError.

print()

Report failed shell command for this RunShellCmdError instance

create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file)

Create helper scripts for specified command in specified directory: - env.sh which can be sourced to define environment in which command was run; - cmd.sh to create interactive (bash) shell session with working directory and environment, and with the command in shell history;

fileprefix_from_cmd(cmd, allowed_chars=False)

Simplify the cmd to only the allowed_chars we want in a filename

PARAMETER DESCRIPTION
cmd

the cmd (string)

allowed_chars

characters allowed in filename (defaults to string.ascii_letters + string.digits + "_-")

DEFAULT: False

raise_run_shell_cmd_error(cmd_res)

Raise RunShellCmdError for failed shell command, after collecting additional caller info

run_shell_cmd(cmd, fail_on_error=True, split_stderr=False, stdin=None, env=None, hidden=False, in_dry_run=False, verbose_dry_run=False, work_dir=None, use_bash=True, output_file=True, stream_output=None, asynchronous=False, task_id=None, with_hooks=True, qa_patterns=None, qa_wait_patterns=None, qa_timeout=100)

Run specified (interactive) shell command, and capture output + exit code.

PARAMETER DESCRIPTION
fail_on_error

fail on non-zero exit code (enabled by default)

DEFAULT: True

split_stderr

split of stderr from stdout output

DEFAULT: False

stdin

input to be sent to stdin (nothing if set to None)

DEFAULT: None

env

environment to use to run command (if None, inherit current process environment)

DEFAULT: None

hidden

do not show command in terminal output (when using --trace, or with --extended-dry-run / -x)

DEFAULT: False

in_dry_run

also run command in dry run mode

DEFAULT: False

verbose_dry_run

show that command is run in dry run mode (overrules 'hidden')

DEFAULT: False

work_dir

working directory to run command in (current working directory if None)

DEFAULT: None

use_bash

execute command through bash shell (enabled by default)

DEFAULT: True

output_file

collect command output in temporary output file

DEFAULT: True

stream_output

stream command output to stdout (auto-enabled with --logtostdout if None)

DEFAULT: None

asynchronous

indicate that command is being run asynchronously

DEFAULT: False

task_id

task ID for specified shell command (included in return value)

DEFAULT: None

with_hooks

trigger pre/post run_shell_cmd hooks (if defined)

DEFAULT: True

qa_patterns

list of 2-tuples with patterns for questions + corresponding answers

DEFAULT: None

qa_wait_patterns

list of strings with patterns for non-questions

DEFAULT: None

qa_timeout

amount of seconds to wait until more output is produced when there is no matching question

DEFAULT: 100

RETURNS DESCRIPTION

Named tuple with: - output: command output, stdout+stderr combined if split_stderr is disabled, only stdout otherwise - exit_code: exit code of command (integer) - stderr: stderr output if split_stderr is enabled, None otherwise

run_shell_cmd_cache(func)

Function decorator to cache (and retrieve cached) results of running commands.

subprocess_popen_text(cmd, **kwargs)

Call subprocess.Popen in text mode with specified named arguments.

subprocess_terminate(proc, timeout)

Terminate the subprocess if it hasn't finished after the given timeout