summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Mangin <thomas.mangin@exa.net.uk>2020-04-26 22:00:26 +0100
committerThomas Mangin <thomas.mangin@exa.net.uk>2020-04-26 22:27:08 +0100
commite768f52b45e3f0d354c788c38d7dfc9964c4d8aa (patch)
treeff136f35db89140ec79d4f312e45ff39f904777b
parent44011942ef8376985dde24ebebe2b60e622737dd (diff)
downloadvyos-1x-e768f52b45e3f0d354c788c38d7dfc9964c4d8aa.tar.gz
vyos-1x-e768f52b45e3f0d354c788c38d7dfc9964c4d8aa.zip
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).
-rw-r--r--python/vyos/util.py5
1 files 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'