xref: /openbmc/qemu/scripts/qmp/qom-get (revision 09a274d8)
1#!/usr/bin/python
2##
3# QEMU Object Model test tools
4#
5# Copyright IBM, Corp. 2011
6#
7# Authors:
8#  Anthony Liguori   <aliguori@us.ibm.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2 or later.  See
11# the COPYING file in the top-level directory.
12##
13
14from __future__ import print_function
15import sys
16import os
17from qmp import QEMUMonitorProtocol
18
19cmd, args = sys.argv[0], sys.argv[1:]
20socket_path = None
21path = None
22prop = None
23
24def usage():
25    return '''environment variables:
26    QMP_SOCKET=<path | addr:port>
27usage:
28    %s [-h] [-s <QMP socket path | addr:port>] <path>.<property>
29''' % cmd
30
31def usage_error(error_msg = "unspecified error"):
32    sys.stderr.write('%s\nERROR: %s\n' % (usage(), error_msg))
33    exit(1)
34
35if len(args) > 0:
36    if args[0] == "-h":
37        print(usage())
38        exit(0);
39    elif args[0] == "-s":
40        try:
41            socket_path = args[1]
42        except:
43            usage_error("missing argument: QMP socket path or address");
44        args = args[2:]
45
46if not socket_path:
47    if 'QMP_SOCKET' in os.environ:
48        socket_path = os.environ['QMP_SOCKET']
49    else:
50        usage_error("no QMP socket path or address given");
51
52if len(args) > 0:
53    try:
54        path, prop = args[0].rsplit('.', 1)
55    except:
56        usage_error("invalid format for path/property/value")
57else:
58    usage_error("not enough arguments")
59
60srv = QEMUMonitorProtocol(socket_path)
61srv.connect()
62
63rsp = srv.command('qom-get', path=path, property=prop)
64if type(rsp) == dict:
65    for i in rsp.keys():
66        print('%s: %s' % (i, rsp[i]))
67else:
68    print(rsp)
69