3 # QMP command line tool
5 # Copyright IBM, Corp. 2011
8 # Anthony Liguori <aliguori@us.ibm.com>
10 # This work is licensed under the terms of the GNU GPLv2 or later.
11 # See the COPYING file in the top-level directory.
14 from qmp
import QEMUMonitorProtocol
16 def print_response(rsp
, prefix
=[]):
22 print_response(item
, prefix
[:-1] + ['%s[%d]' % (prefix
[-1], i
)])
24 elif type(rsp
) == dict:
25 for key
in rsp
.keys():
26 print_response(rsp
[key
], prefix
+ [key
])
29 print '%s: %s' % ('.'.join(prefix
), rsp
)
36 # Use QMP_PATH if it's set
37 if os
.environ
.has_key('QMP_PATH'):
38 path
= os
.environ
['QMP_PATH']
43 if arg
.startswith('--'):
45 if arg
.find('=') == -1:
48 arg
, value
= arg
.split('=', 1)
51 if type(value
) == str:
54 os
.execlp('man', 'man', 'qmp')
56 print 'Unknown argument "%s"' % arg
63 print "QMP path isn't set, use --path=qmp-monitor-address or set QMP_PATH"
67 command
, args
= args
[0], args
[1:]
69 print 'No command found'
70 print 'Usage: "qmp [--path=qmp-monitor-address] qmp-cmd arguments"'
73 if command
in ['help']:
74 os
.execlp('man', 'man', 'qmp')
76 srv
= QEMUMonitorProtocol(path
)
79 def do_command(srv
, cmd
, **kwds
):
80 rsp
= srv
.cmd(cmd
, kwds
)
81 if rsp
.has_key('error'):
82 raise Exception(rsp
['error']['desc'])
85 commands
= map(lambda x
: x
['name'], do_command(srv
, 'query-commands'))
89 if command
not in commands
:
90 fullcmd
= 'qmp-%s' % command
92 os
.environ
['QMP_PATH'] = path
93 os
.execvp(fullcmd
, [fullcmd
] + args
)
94 except OSError, (errno
, msg
):
96 print 'Command "%s" not found.' % (fullcmd
)
101 srv
= QEMUMonitorProtocol(path
)
106 if not arg
.startswith('--'):
107 print 'Unknown argument "%s"' % arg
111 if arg
.find('=') == -1:
114 arg
, value
= arg
.split('=', 1)
117 os
.execlp('man', 'man', 'qmp-%s' % command
)
120 arguments
[arg
] = value
122 rsp
= do_command(srv
, command
, **arguments
)
125 if __name__
== '__main__':
126 sys
.exit(main(sys
.argv
[1:]))