From e768f52b45e3f0d354c788c38d7dfc9964c4d8aa Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Sun, 26 Apr 2020 22:00:26 +0100 Subject: util: T2226: expected return code for cmd add an option to cmd() allowing to define a list of error codes which are expected when runnin the command. the default is only to expect zero (no error). --- python/vyos/util.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/vyos/util.py b/python/vyos/util.py index f416e79f9..8430799a1 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -123,13 +123,14 @@ def run(command, flag='', shell=None, input=None, timeout=None, env=None, def cmd(command, flag='', shell=None, input=None, timeout=None, env=None, stdout=PIPE, stderr=PIPE, decode='utf-8', - raising=None, message=''): + raising=None, message='', expect=[0]): """ A wrapper around vyos.util.popen, which returns the stdout and will raise the error code of a command raising: specify which call should be used when raising (default is OSError) the class should only require a string as parameter + expect: a list of error codes to consider as normal """ decoded, code = popen( command, flag, @@ -138,7 +139,7 @@ def cmd(command, flag='', shell=None, input=None, timeout=None, env=None, env=env, shell=shell, decode=decode, ) - if code != 0: + if code not in expect: feedback = message + '\n' if message else '' feedback += f'failed to run command: {command}\n' feedback += f'returned: {decoded}\n' -- cgit v1.2.3