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.
15 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
16 from qemu.qmp import QEMUMonitorProtocol
18 def print_response(rsp, prefix=[]):
24 print_response(item, prefix[:-1] + ['%s[%d]' % (prefix[-1], i)])
26 elif type(rsp) == dict:
27 for key in rsp.keys():
28 print_response(rsp[key], prefix + [key])
31 print('%s: %s' % ('.'.join(prefix), rsp))
38 # Use QMP_PATH if it's set
39 if 'QMP_PATH' in os.environ:
40 path = os.environ['QMP_PATH']
45 if arg.startswith('--'):
47 if arg.find('=') == -1:
50 arg, value = arg.split('=', 1)
53 if type(value) == str:
56 os.execlp('man', 'man', 'qmp')
58 print('Unknown argument "%s"' % arg)
65 print("QMP path isn't set, use --path=qmp-monitor-address or set QMP_PATH")
69 command, args = args[0], args[1:]
71 print('No command found')
72 print('Usage: "qmp [--path=qmp-monitor-address] qmp-cmd arguments"')
75 if command in ['help']:
76 os.execlp('man', 'man', 'qmp')
78 srv = QEMUMonitorProtocol(path)
81 def do_command(srv, cmd, **kwds):
82 rsp = srv.cmd(cmd, kwds)
84 raise Exception(rsp['error']['desc'])
87 commands = map(lambda x: x['name'], do_command(srv, 'query-commands'))
91 if command not in commands:
92 fullcmd = 'qmp-%s' % command
94 os.environ['QMP_PATH'] = path
95 os.execvp(fullcmd, [fullcmd] + args)
96 except OSError as exc:
98 print('Command "%s" not found.' % (fullcmd))
103 srv = QEMUMonitorProtocol(path)
108 if not arg.startswith('--'):
109 print('Unknown argument "%s"' % arg)
113 if arg.find('=') == -1:
116 arg, value = arg.split('=', 1)
119 os.execlp('man', 'man', 'qmp-%s' % command)
122 arguments[arg] = value
124 rsp = do_command(srv, command, **arguments)
127 if __name__ == '__main__':
128 sys.exit(main(sys.argv[1:]))