Skip to content

[th/nmci-process-run-code] nmci.process: add nmci.process.run_code() helper

Thomas Haller requested to merge th/nmci-process-run-code into master

I guess, there could be cases where we are not interested in the output (nmci.process.run_check()), but in the returncode.

Add a wrapper that only gives the return code and throws away stdout. With this you could do

  if nmci.process.run_code("grep foo < file", shell=True): pass

But don't misuse that as

  if nmci.process.run_code("command | grep foo", shell=True): pass

because that would not catch an error code from "command". Better do

  stdout = nmci.process.run_code("command")
  if re.match("foo", stdout): pass

which does strict checking that command succeeds and does not need two other processes (a shell and grep) to do a simple pattern matching.

Don't misused this.


Btw, if you find to do above frequently, add the following to nmci/process.c:

def run_match_stdout(argv, pattern, *, shell=False, timeout=5, pattern_flags=0):
   stdout = run_code(argv, as_bytes=isinstance(patter, bytes), shell=shell, timeout=timeout)
   return re.match(pattern, stdout, flags=pattern_flags)

Merge request reports