diff mbox

[2/3] iotests: _filter_qmp for pretty JSON output

Message ID 1415712873-29274-3-git-send-email-mreitz@redhat.com
State New
Headers show

Commit Message

Max Reitz Nov. 11, 2014, 1:34 p.m. UTC
_filter_qmp should be able to correctly filter out the QMP version
object for pretty JSON output.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 tests/qemu-iotests/common.filter | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Comments

Eric Blake Nov. 12, 2014, 4:47 a.m. UTC | #1
On 11/11/2014 06:34 AM, Max Reitz wrote:
> _filter_qmp should be able to correctly filter out the QMP version
> object for pretty JSON output.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  tests/qemu-iotests/common.filter | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/tests/qemu-iotests/common.filter b/tests/qemu-iotests/common.filter
> index 3acdb30..e24dab4 100644
> --- a/tests/qemu-iotests/common.filter
> +++ b/tests/qemu-iotests/common.filter
> @@ -165,9 +165,23 @@ _filter_qemu()
>  # replace problematic QMP output like timestamps
>  _filter_qmp()
>  {
> +    discard=0
> +
>      _filter_win32 | \
>      sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
> -        -e 's#^{"QMP":.*}$#QMP_VERSION#'
> +        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
> +        -e 's#\\#\\\\#g' | \
> +        while IFS='' read line; do
> +            if [[ $line == '    "QMP": {' ]]; then

Good that this is a /bin/bash script and not /bin/sh :)

But - is it really worth doing this in shell?  Why not just do it in sed?

sed -e ... \
    -e 's#\\#\\\\#g' \
    -e '/    "QMP": {/,/    }/ c\' \
    -e '    QMP_VERSION'
Max Reitz Nov. 12, 2014, 8:33 a.m. UTC | #2
On 2014-11-12 at 05:47, Eric Blake wrote:
> On 11/11/2014 06:34 AM, Max Reitz wrote:
>> _filter_qmp should be able to correctly filter out the QMP version
>> object for pretty JSON output.
>>
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
>> ---
>>   tests/qemu-iotests/common.filter | 16 +++++++++++++++-
>>   1 file changed, 15 insertions(+), 1 deletion(-)
>>
>> diff --git a/tests/qemu-iotests/common.filter b/tests/qemu-iotests/common.filter
>> index 3acdb30..e24dab4 100644
>> --- a/tests/qemu-iotests/common.filter
>> +++ b/tests/qemu-iotests/common.filter
>> @@ -165,9 +165,23 @@ _filter_qemu()
>>   # replace problematic QMP output like timestamps
>>   _filter_qmp()
>>   {
>> +    discard=0
>> +
>>       _filter_win32 | \
>>       sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
>> -        -e 's#^{"QMP":.*}$#QMP_VERSION#'
>> +        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
>> +        -e 's#\\#\\\\#g' | \
>> +        while IFS='' read line; do
>> +            if [[ $line == '    "QMP": {' ]]; then
> Good that this is a /bin/bash script and not /bin/sh :)

Ah, right, yes, I just copied the code I had written for filtering out 
the image-specific information from the qemu-img info output.

> But - is it really worth doing this in shell?  Why not just do it in sed?

Because I don't know sed well enough. ;-)

> sed -e ... \
>      -e 's#\\#\\\\#g' \
>      -e '/    "QMP": {/,/    }/ c\' \
>      -e '    QMP_VERSION'

Will do, thanks.

Max
Eric Blake Nov. 12, 2014, 1:19 p.m. UTC | #3
On 11/12/2014 01:33 AM, Max Reitz wrote:

>>> +        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
>>> +        -e 's#\\#\\\\#g' | \
>>> +        while IFS='' read line; do
>>> +            if [[ $line == '    "QMP": {' ]]; then
>> Good that this is a /bin/bash script and not /bin/sh :)
> 
> Ah, right, yes, I just copied the code I had written for filtering out
> the image-specific information from the qemu-img info output.
> 
>> But - is it really worth doing this in shell?  Why not just do it in sed?
> 
> Because I don't know sed well enough. ;-)
> 
>> sed -e ... \
>>      -e 's#\\#\\\\#g' \
>>      -e '/    "QMP": {/,/    }/ c\' \

Better make this line anchored in its searching:

-e '/^    "QMP": {$/,/^    }$/ c\' \

>>      -e '    QMP_VERSION'
> 
> Will do, thanks.
> 
> Max
> 
>
diff mbox

Patch

diff --git a/tests/qemu-iotests/common.filter b/tests/qemu-iotests/common.filter
index 3acdb30..e24dab4 100644
--- a/tests/qemu-iotests/common.filter
+++ b/tests/qemu-iotests/common.filter
@@ -165,9 +165,23 @@  _filter_qemu()
 # replace problematic QMP output like timestamps
 _filter_qmp()
 {
+    discard=0
+
     _filter_win32 | \
     sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
-        -e 's#^{"QMP":.*}$#QMP_VERSION#'
+        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
+        -e 's#\\#\\\\#g' | \
+        while IFS='' read line; do
+            if [[ $line == '    "QMP": {' ]]; then
+                discard=1
+                echo '    QMP_VERSION'
+            fi
+            if [[ $discard == 0 ]]; then
+                echo "$line"
+            elif [[ $discard == 1 && $line == '    }' ]]; then
+                discard=0
+            fi
+        done
 }
 
 # replace driver-specific options in the "Formatting..." line