<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body smarttemplateinserted="true" text="#000000" bgcolor="#FFFFFF">
<div id="smartTemplate4-quoteHeader">Reviewed-by: ShaoHe Feng <a class="moz-txt-link-rfc2396E" href="mailto:shaohef@linux.vnet.ibm.com"><shaohef@linux.vnet.ibm.com></a><br>
</div>
<div class="moz-cite-prefix">On 12/18/2013 02:31 PM, Royce Lv wrote:<br>
</div>
<blockquote cite="mid:52B1415F.1050902@linux.vnet.ibm.com"
type="cite">On 2013年12月17日 20:50, Ramon Medeiros wrote:
<br>
<blockquote type="cite">On 12/17/2013 10:10 AM,
<a class="moz-txt-link-abbreviated" href="mailto:lvroyce0210@gmail.com">lvroyce0210@gmail.com</a> wrote:
<br>
<blockquote type="cite">From: Royce Lv
<a class="moz-txt-link-rfc2396E" href="mailto:lvroyce@linux.vnet.ibm.com"><lvroyce@linux.vnet.ibm.com></a>
<br>
<br>
Abstract a helper function to parse cmd result.
<br>
Usage:
<br>
(1)get cmd result with subprocess.call or subprocess.Popen
<br>
(2) call pass_cmd_output to get formated outputs
<br>
Example:
<br>
blkid = subprocess.Popen(["cat", "/proc/mounts"],
<br>
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
<br>
outs = blkid.communicate()[0]
<br>
output_items= ['path', 'mnt_point', 'type', 'option']
<br>
utils.pass_cmd_output(outs, output_items)
<br>
Sample output:
<br>
[{'path': '/dev/sda8', 'type': 'ext4',
<br>
'option': 'rw,relatime,data=ordered', 'mnt_point': '/home'},
<br>
{'path': 'localhost:/home/royce/isorepo', 'type': 'nfs4',
<br>
'option': 'rw...addr=127.0.0.1', 'mnt_point': '/mnt'}]
<br>
<br>
Signed-off-by: Royce Lv <a class="moz-txt-link-rfc2396E" href="mailto:lvroyce@linux.vnet.ibm.com"><lvroyce@linux.vnet.ibm.com></a>
<br>
---
<br>
src/kimchi/utils.py | 7 +++++++
<br>
1 file changed, 7 insertions(+)
<br>
<br>
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
<br>
index f7eda93..1890a28 100644
<br>
--- a/src/kimchi/utils.py
<br>
+++ b/src/kimchi/utils.py
<br>
@@ -84,3 +84,10 @@ def import_class(class_path):
<br>
<br>
def import_module(module_name):
<br>
return __import__(module_name, fromlist=[''])
<br>
+
<br>
+def parse_cmd_output(output, output_items):
<br>
+ res = []
<br>
+ for line in output.split("\n"):
<br>
</blockquote>
can you get the Popen result and use readlines, to get a array
with the lines, instead of striping it?
<br>
</blockquote>
Hi Ramon,
<br>
<br>
Thanks for your review, I think for file read result it is OK, but
seems for popen.communicate just pull out the raw output , and
only stdout provide readlines interface, which official doc does
not recommend because of deadlock problem.
(<a class="moz-txt-link-freetext" href="http://docs.python.org/3.3/library/subprocess.html#subprocess.Popen.communicate">http://docs.python.org/3.3/library/subprocess.html#subprocess.Popen.communicate</a>).<br>
</blockquote>
I think Ramon want:<br>
<pre wrap="">+ with open("/proc/mounts" , "rb") as f:
+ res = []
+ for output in f.readlines():
+ res.append(dict(zip(['dev_path', 'mnt_point', 'type'], output.split()))
+ return res
</pre>
maybe you want a common function to parse both the output from
stdout and files. <br>
<br>
blkid = subprocess.Popen(["cat", "/proc/mounts"],<br>
<blockquote cite="mid:52B1415F.1050902@linux.vnet.ibm.com"
type="cite">
<br>
<blockquote type="cite">
<blockquote type="cite">+ res.append(dict(zip(output_items,
line.split())))
<br>
+
<br>
+ return res
<br>
</blockquote>
<br>
<br>
</blockquote>
<br>
</blockquote>
<br>
<br>
<div id="smartTemplate4-template"> </div>
<div class="moz-signature">
<pre>Sheldon Feng(冯少合)<shaohef@linux.vnet.ibm.com>
IBM Linux Technology Center</shaohef@linux.vnet.ibm.com></pre>
</div>
</body>
</html>
<p></p>
-- <br />
project-kimchi mailing list <project-kimchi@googlegroups.com><br />
<a href="https://groups.google.com/forum/#!forum/project-kimchi">https://groups.google.com/forum/#!forum/project-kimchi</a><br />
--- <br />
You received this message because you are subscribed to the Google Groups "project-kimchi" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to project-kimchi+unsubscribe@googlegroups.com.<br />
For more options, visit <a href="https://groups.google.com/groups/opt_out">https://groups.google.com/groups/opt_out</a>.<br />