From: Jiri Pirko <jiri(a)mellanox.com>
This is handy for processing output of commands producing JSON.
Signed-off-by: Jiri Pirko <jiri(a)mellanox.com>
---
lnst/Common/ExecCmd.py | 6 +++++-
lnst/Common/NetTestCommand.py | 3 ++-
lnst/Controller/Task.py | 4 ++++
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/lnst/Common/ExecCmd.py b/lnst/Common/ExecCmd.py
index d432c2c..24715ae 100644
--- a/lnst/Common/ExecCmd.py
+++ b/lnst/Common/ExecCmd.py
@@ -51,7 +51,7 @@ def log_output(log_func, out_type, out):
"----------------------------"
% (out_type, out))
-def exec_cmd(cmd, die_on_err=True, log_outputs=True, report_stderr=False):
+def exec_cmd(cmd, die_on_err=True, log_outputs=True, report_stderr=False, json=False):
cmd = cmd.rstrip(" ")
logging.debug("Executing: \"%s\"" % cmd)
subp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
@@ -72,4 +72,8 @@ def exec_cmd(cmd, die_on_err=True, log_outputs=True,
report_stderr=False):
logging.error(err)
raise err
+ if json:
+ import json
+ data_stdout = json.loads(data_stdout)
+
return data_stdout, data_stderr
diff --git a/lnst/Common/NetTestCommand.py b/lnst/Common/NetTestCommand.py
index 3c290dc..52f2a73 100644
--- a/lnst/Common/NetTestCommand.py
+++ b/lnst/Common/NetTestCommand.py
@@ -407,7 +407,8 @@ class NetTestCommandExec(NetTestCommandGeneric):
stdout, stderr = self.exec_from(self._command["from"],
self._command["command"])
else:
- stdout, stderr = self.exec_cmd(self._command["command"])
+ json = True if "json" in self._command else False
+ stdout, stderr = self.exec_cmd(self._command["command"],
json=json)
res_data = {"stdout": stdout, "stderr": stderr}
self.set_pass(res_data)
except ExecCmdFail as e:
diff --git a/lnst/Controller/Task.py b/lnst/Controller/Task.py
index 1226ce2..80e4b75 100644
--- a/lnst/Controller/Task.py
+++ b/lnst/Controller/Task.py
@@ -242,6 +242,8 @@ class HostAPI(object):
:type timeout: int
:param tool: Run from a tool (the same as 'from' in XML).
:type tool: string
+ :param json: Process JSON output into dictionary.
+ :type json: bool
:return: A handle for process.
:rtype: ProcessAPI
@@ -283,6 +285,8 @@ class HostAPI(object):
cmd["netns"] = argval
elif arg == "save_output":
pass # now ignored as output is saved always
+ elif arg == "json":
+ cmd["json"] = argval
else:
msg = "Argument '%s' not recognised by the run()
method." % arg
raise TaskError(msg)
--
2.5.5