xref: /qemu/scripts/qmp/qom-list (revision d7a4228ebb0150e4c97a19077f3fac81d843e2c3)
1235fe3bfSAnthony Liguori#!/usr/bin/python
2235fe3bfSAnthony Liguori##
3235fe3bfSAnthony Liguori# QEMU Object Model test tools
4235fe3bfSAnthony Liguori#
5235fe3bfSAnthony Liguori# Copyright IBM, Corp. 2011
6235fe3bfSAnthony Liguori#
7235fe3bfSAnthony Liguori# Authors:
8235fe3bfSAnthony Liguori#  Anthony Liguori   <aliguori@us.ibm.com>
9235fe3bfSAnthony Liguori#
10235fe3bfSAnthony Liguori# This work is licensed under the terms of the GNU GPL, version 2 or later.  See
11235fe3bfSAnthony Liguori# the COPYING file in the top-level directory.
12235fe3bfSAnthony Liguori##
13235fe3bfSAnthony Liguori
14f03868bdSEduardo Habkostfrom __future__ import print_function
15068cf7a4SEduardo Habkostfrom __future__ import absolute_import
16235fe3bfSAnthony Liguoriimport sys
17235fe3bfSAnthony Liguoriimport os
18068cf7a4SEduardo Habkostfrom .qmp import QEMUMonitorProtocol
19235fe3bfSAnthony Liguori
20235fe3bfSAnthony Liguoricmd, args = sys.argv[0], sys.argv[1:]
21235fe3bfSAnthony Liguorisocket_path = None
22235fe3bfSAnthony Liguoripath = None
23235fe3bfSAnthony Liguoriprop = None
24235fe3bfSAnthony Liguori
25235fe3bfSAnthony Liguoridef usage():
26235fe3bfSAnthony Liguori    return '''environment variables:
27235fe3bfSAnthony Liguori    QMP_SOCKET=<path | addr:port>
28235fe3bfSAnthony Liguoriusage:
29235fe3bfSAnthony Liguori    %s [-h] [-s <QMP socket path | addr:port>] [<path>]
30235fe3bfSAnthony Liguori''' % cmd
31235fe3bfSAnthony Liguori
32235fe3bfSAnthony Liguoridef usage_error(error_msg = "unspecified error"):
33235fe3bfSAnthony Liguori    sys.stderr.write('%s\nERROR: %s\n' % (usage(), error_msg))
34235fe3bfSAnthony Liguori    exit(1)
35235fe3bfSAnthony Liguori
36235fe3bfSAnthony Liguoriif len(args) > 0:
37235fe3bfSAnthony Liguori    if args[0] == "-h":
38f03868bdSEduardo Habkost        print(usage())
39235fe3bfSAnthony Liguori        exit(0);
40235fe3bfSAnthony Liguori    elif args[0] == "-s":
41235fe3bfSAnthony Liguori        try:
42235fe3bfSAnthony Liguori            socket_path = args[1]
43235fe3bfSAnthony Liguori        except:
44235fe3bfSAnthony Liguori            usage_error("missing argument: QMP socket path or address");
45235fe3bfSAnthony Liguori        args = args[2:]
46235fe3bfSAnthony Liguori
47235fe3bfSAnthony Liguoriif not socket_path:
48*d7a4228eSEduardo Habkost    if 'QMP_SOCKET' in os.environ:
49235fe3bfSAnthony Liguori        socket_path = os.environ['QMP_SOCKET']
50235fe3bfSAnthony Liguori    else:
51235fe3bfSAnthony Liguori        usage_error("no QMP socket path or address given");
52235fe3bfSAnthony Liguori
53235fe3bfSAnthony Liguorisrv = QEMUMonitorProtocol(socket_path)
54235fe3bfSAnthony Liguorisrv.connect()
55235fe3bfSAnthony Liguori
56235fe3bfSAnthony Liguoriif len(args) == 0:
57f03868bdSEduardo Habkost    print('/')
58235fe3bfSAnthony Liguori    sys.exit(0)
59235fe3bfSAnthony Liguori
60235fe3bfSAnthony Liguorifor item in srv.command('qom-list', path=args[0]):
61235fe3bfSAnthony Liguori    if item['type'].startswith('child<'):
62f03868bdSEduardo Habkost        print('%s/' % item['name'])
63235fe3bfSAnthony Liguori    elif item['type'].startswith('link<'):
64f03868bdSEduardo Habkost        print('@%s/' % item['name'])
65235fe3bfSAnthony Liguori    else:
66f03868bdSEduardo Habkost        print('%s' % item['name'])
67