[Kimchi-devel] [project-kimchi][PATCH 1/3] utils: Add a helper function to parse cmd result
Ramon Medeiros
ramonn at linux.vnet.ibm.com
Wed Dec 18 12:28:19 UTC 2013
On 12/18/2013 05:12 AM, Royce Lv wrote:
> On 2013年12月18日 14:40, Sheldon wrote:
>> Reviewed-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
>> On 12/18/2013 02:31 PM, Royce Lv wrote:
>>> On 2013年12月17日 20:50, Ramon Medeiros wrote:
>>>> On 12/17/2013 10:10 AM, lvroyce0210 at gmail.com wrote:
>>>>> From: Royce Lv <lvroyce at linux.vnet.ibm.com>
>>>>>
>>>>> Abstract a helper function to parse cmd result.
>>>>> Usage:
>>>>> (1)get cmd result with subprocess.call or subprocess.Popen
>>>>> (2) call pass_cmd_output to get formated outputs
>>>>> Example:
>>>>> blkid = subprocess.Popen(["cat", "/proc/mounts"],
>>>>> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>>>> outs = blkid.communicate()[0]
>>>>> output_items= ['path', 'mnt_point', 'type', 'option']
>>>>> utils.pass_cmd_output(outs, output_items)
>>>>> Sample output:
>>>>> [{'path': '/dev/sda8', 'type': 'ext4',
>>>>> 'option': 'rw,relatime,data=ordered', 'mnt_point': '/home'},
>>>>> {'path': 'localhost:/home/royce/isorepo', 'type': 'nfs4',
>>>>> 'option': 'rw...addr=127.0.0.1', 'mnt_point': '/mnt'}]
>>>>>
>>>>> Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
>>>>> ---
>>>>> src/kimchi/utils.py | 7 +++++++
>>>>> 1 file changed, 7 insertions(+)
>>>>>
>>>>> diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
>>>>> index f7eda93..1890a28 100644
>>>>> --- a/src/kimchi/utils.py
>>>>> +++ b/src/kimchi/utils.py
>>>>> @@ -84,3 +84,10 @@ def import_class(class_path):
>>>>>
>>>>> def import_module(module_name):
>>>>> return __import__(module_name, fromlist=[''])
>>>>> +
>>>>> +def parse_cmd_output(output, output_items):
>>>>> + res = []
>>>>> + for line in output.split("\n"):
>>>> can you get the Popen result and use readlines, to get a array with
>>>> the lines, instead of striping it?
>>> Hi Ramon,
>>>
>>> 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.
>>> (http://docs.python.org/3.3/library/subprocess.html#subprocess.Popen.communicate).
>> I think Ramon want:
>> + 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
yes, this was my wish!
>> maybe you want a common function to parse both the output from stdout
>> and files.
>>
>> blkid = subprocess.Popen(["cat", "/proc/mounts"],
> True, I want a helper function that all command can use to get a
> structural result.
> So that in the future if we want to get iscsi session, or other things
> , we can reuse this function.
ok
>>>
>>>>> + res.append(dict(zip(output_items, line.split())))
>>>>> +
>>>>> + return res
>>>>
>>>>
>>>
>>
>>
>> Sheldon Feng(冯少合)
>> IBM Linux Technology Center
>> --
>> project-kimchi mailing list <project-kimchi at googlegroups.com>
>> https://groups.google.com/forum/#!forum/project-kimchi
>> <https://groups.google.com/forum/#%21forum/project-kimchi>
>> ---
>> You received this message because you are subscribed to the Google
>> Groups "project-kimchi" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to project-kimchi+unsubscribe at googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>
> --
> project-kimchi mailing list <project-kimchi at googlegroups.com>
> https://groups.google.com/forum/#!forum/project-kimchi
> <https://groups.google.com/forum/#%21forum/project-kimchi>
> ---
> You received this message because you are subscribed to the Google
> Groups "project-kimchi" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to project-kimchi+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
--
Ramon Nunes Medeiros
Software Engineer - Linux Technology Center Brazil
IBM Systems & Technology Group
Phone : +55 19 2132 7878
ramonn at br.ibm.com
--
project-kimchi mailing list <project-kimchi at googlegroups.com>
https://groups.google.com/forum/#!forum/project-kimchi
---
You received this message because you are subscribed to the Google Groups "project-kimchi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-kimchi+unsubscribe at googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/kimchi-devel/attachments/20131218/b1ebbb8e/attachment.html>
More information about the Kimchi-devel
mailing list