|
|
1.1 root 1: #!/usr/bin/python
2: #
3: # QMP command line tool
4: #
5: # Copyright IBM, Corp. 2011
6: #
7: # Authors:
8: # Anthony Liguori <[email protected]>
9: #
10: # This work is licensed under the terms of the GNU GPLv2 or later.
11: # See the COPYING file in the top-level directory.
12:
13: import sys, os
14: from qmp import QEMUMonitorProtocol
15:
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)
32:
33: def main(args):
34: path = None
35:
36: # Use QMP_PATH if it's set
37: if os.environ.has_key('QMP_PATH'):
38: path = os.environ['QMP_PATH']
39:
40: while len(args):
41: arg = args[0]
42:
43: if arg.startswith('--'):
44: arg = arg[2:]
45: if arg.find('=') == -1:
46: value = True
47: else:
48: arg, value = arg.split('=', 1)
49:
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
57:
58: args = args[1:]
59: else:
60: break
61:
62: if not path:
63: print "QMP path isn't set, use --path=qmp-monitor-address or set QMP_PATH"
64: return 1
65:
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
72:
73: if command in ['help']:
74: os.execlp('man', 'man', 'qmp')
75:
76: srv = QEMUMonitorProtocol(path)
77: srv.connect()
78:
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']
84:
85: commands = map(lambda x: x['name'], do_command(srv, 'query-commands'))
86:
87: srv.close()
88:
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
100:
101: srv = QEMUMonitorProtocol(path)
102: srv.connect()
103:
104: arguments = {}
105: for arg in args:
106: if not arg.startswith('--'):
107: print 'Unknown argument "%s"' % arg
108: return 1
109:
110: arg = arg[2:]
111: if arg.find('=') == -1:
112: value = True
113: else:
114: arg, value = arg.split('=', 1)
115:
116: if arg in ['help']:
117: os.execlp('man', 'man', 'qmp-%s' % command)
118: return 1
119:
120: arguments[arg] = value
121:
122: rsp = do_command(srv, command, **arguments)
123: print_response(rsp)
124:
125: if __name__ == '__main__':
126: sys.exit(main(sys.argv[1:]))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.