diff mbox

[1/1] QMP test code - qmp.py

Message ID w2g555ed20a1004061740rc79f0640y64b52334c63bb9d1@mail.gmail.com
State New
Headers show

Commit Message

Costas Drogos April 7, 2010, 12:40 a.m. UTC
Hello there,

a very small patch to address two small issues:

1) The json state in python2.5 and python2.6. json module is included
by default on python2.6, whereas you have to import simplejson in
python2.5. I have this problem on Debian testing, so feel free to test
if this is applicable in your distribution.

2) For qmp commands to work (e.g. query-kvm) we have to give first the
command 'qmp_capabilities' because monitor is in 'Capabilities
Negotiation mode' on startup. The patch takes care of issuing that
command immediately after connecting.

The patch is more of a hack to ease qmp testing and development.


Costas Drogos

--

Comments

Luiz Capitulino April 7, 2010, 1:36 p.m. UTC | #1
On Wed, 7 Apr 2010 03:40:32 +0300
Costas Drogos <costas.drogos@gmail.com> wrote:

> Hello there,
> 
> a very small patch to address two small issues:
> 
> 1) The json state in python2.5 and python2.6. json module is included
> by default on python2.6, whereas you have to import simplejson in
> python2.5. I have this problem on Debian testing, so feel free to test
> if this is applicable in your distribution.
> 
> 2) For qmp commands to work (e.g. query-kvm) we have to give first the
> command 'qmp_capabilities' because monitor is in 'Capabilities
> Negotiation mode' on startup. The patch takes care of issuing that
> command immediately after connecting.
> 
> The patch is more of a hack to ease qmp testing and development.

 I already have a new version of that script in the master branch of:

	git://repo.or.cz/qemu/qmp-unstable.git

 It fixes current problems and has other improvements, I didn't submit
it yet because I didn't test it much.

 So, I will add a fix for the json problem and would appreciate any testing.

> diff --git a/QMP/qmp.py b/QMP/qmp.py
> index d9da603..f8581c4 100644
> --- a/QMP/qmp.py
> +++ b/QMP/qmp.py
> @@ -8,7 +8,15 @@
>  # This work is licensed under the terms of the GNU GPL, version 2.  See
>  # the COPYING file in the top-level directory.
> 
> -import socket, json
> +import socket
> +
> +from sys import version
> +ver = version[:3]
> +
> +if ver == '2.6':
> +    import json
> +else:
> +    import simplejson as json

 This won't do what we want for 2.7 and newer, so a better if would be:

if sys.version_info < (2, 6):
    import simplejson as json
else:
    import json
Avi Kivity April 7, 2010, 1:51 p.m. UTC | #2
On 04/07/2010 04:36 PM, Luiz Capitulino wrote:
>
>
>> diff --git a/QMP/qmp.py b/QMP/qmp.py
>> index d9da603..f8581c4 100644
>> --- a/QMP/qmp.py
>> +++ b/QMP/qmp.py
>> @@ -8,7 +8,15 @@
>>   # This work is licensed under the terms of the GNU GPL, version 2.  See
>>   # the COPYING file in the top-level directory.
>>
>> -import socket, json
>> +import socket
>> +
>> +from sys import version
>> +ver = version[:3]
>> +
>> +if ver == '2.6':
>> +    import json
>> +else:
>> +    import simplejson as json
>>      
>   This won't do what we want for 2.7 and newer, so a better if would be:
>
> if sys.version_info<  (2, 6):
>      import simplejson as json
> else:
>      import json
>
>    

try:
     import json
except:
     import simplejson as json
Costas Drogos April 7, 2010, 2:38 p.m. UTC | #3
On Wed, Apr 7, 2010 at 16:51, Avi Kivity <avi@redhat.com> wrote:
>>  This won't do what we want for 2.7 and newer, so a better if would be:

Thats why i said it is just an ugly hack :) Anyway, I've just cloned
the qmp-unstable tree to run some tests and report back

>> if sys.version_info<  (2, 6):
>>     import simplejson as json
>> else:
>>     import json
>>
>>
>
> try:
>    import json
> except:
>    import simplejson as json

I thought of that, but the thing is that, in Debian at least, there is
python-json for python 2.5 which does not work, but can be imported,
and also python-simplejson. On python2.6 json module is included and
there is a package python2.6-simplejson also. Anyway, we can't check
what every distribution does with modules, so I think Avi's solution
works most of the times. In the end it is just a test script :)

Thanks for the comments :)
diff mbox

Patch

diff --git a/QMP/qmp.py b/QMP/qmp.py
index d9da603..f8581c4 100644
--- a/QMP/qmp.py
+++ b/QMP/qmp.py
@@ -8,7 +8,15 @@ 
 # This work is licensed under the terms of the GNU GPL, version 2.  See
 # the COPYING file in the top-level directory.

-import socket, json
+import socket
+
+from sys import version
+ver = version[:3]
+
+if ver == '2.6':
+    import json
+else:
+    import simplejson as json

 class QMPError(Exception):
     pass
@@ -24,6 +32,9 @@  class QEMUMonitorProtocol:
             raise QMPConnectError
         if not data.has_key('QMP'):
             raise QMPConnectError
+        # initialize the qmp interface
+        cmd = {'execute':'qmp_capabilities' }
+        self.sock.send(str(cmd))
         return data['QMP']['capabilities']

     def close(self):