ui/cocoa.m: Update to new DisplayChangeListener member names
[qemu/pbrook.git] / QMP / qmp
blob1db3c7ffebaa323569989af987a480a41817786b
1 #!/usr/bin/python
3 # QMP command line tool
5 # Copyright IBM, Corp. 2011
7 # Authors:
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.
13 import sys, os
14 from qmp import QEMUMonitorProtocol
16 def print_response(rsp, prefix=[]):
17 if type(rsp) == list:
18 i = 0
19 for item in rsp:
20 if prefix == []:
21 prefix = ['item']
22 print_response(item, prefix[:-1] + ['%s[%d]' % (prefix[-1], i)])
23 i += 1
24 elif type(rsp) == dict:
25 for key in rsp.keys():
26 print_response(rsp[key], prefix + [key])
27 else:
28 if len(prefix):
29 print '%s: %s' % ('.'.join(prefix), rsp)
30 else:
31 print '%s' % (rsp)
33 def main(args):
34 path = None
36 # Use QMP_PATH if it's set
37 if os.environ.has_key('QMP_PATH'):
38 path = os.environ['QMP_PATH']
40 while len(args):
41 arg = args[0]
43 if arg.startswith('--'):
44 arg = arg[2:]
45 if arg.find('=') == -1:
46 value = True
47 else:
48 arg, value = arg.split('=', 1)
50 if arg in ['path']:
51 if type(value) == str:
52 path = value
53 elif arg in ['help']:
54 os.execlp('man', 'man', 'qmp')
55 else:
56 print 'Unknown argument "%s"' % arg
58 args = args[1:]
59 else:
60 break
62 if not path:
63 print "QMP path isn't set, use --path=qmp-monitor-address or set QMP_PATH"
64 return 1
66 if len(args):
67 command, args = args[0], args[1:]
68 else:
69 print 'No command found'
70 print 'Usage: "qmp [--path=qmp-monitor-address] qmp-cmd arguments"'
71 return 1
73 if command in ['help']:
74 os.execlp('man', 'man', 'qmp')
76 srv = QEMUMonitorProtocol(path)
77 srv.connect()
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'])
83 return rsp['return']
85 commands = map(lambda x: x['name'], do_command(srv, 'query-commands'))
87 srv.close()
89 if command not in commands:
90 fullcmd = 'qmp-%s' % command
91 try:
92 os.environ['QMP_PATH'] = path
93 os.execvp(fullcmd, [fullcmd] + args)
94 except OSError, (errno, msg):
95 if errno == 2:
96 print 'Command "%s" not found.' % (fullcmd)
97 return 1
98 raise
99 return 0
101 srv = QEMUMonitorProtocol(path)
102 srv.connect()
104 arguments = {}
105 for arg in args:
106 if not arg.startswith('--'):
107 print 'Unknown argument "%s"' % arg
108 return 1
110 arg = arg[2:]
111 if arg.find('=') == -1:
112 value = True
113 else:
114 arg, value = arg.split('=', 1)
116 if arg in ['help']:
117 os.execlp('man', 'man', 'qmp-%s' % command)
118 return 1
120 arguments[arg] = value
122 rsp = do_command(srv, command, **arguments)
123 print_response(rsp)
125 if __name__ == '__main__':
126 sys.exit(main(sys.argv[1:]))