xref: /qemu/scripts/qmp/qom-set (revision f03868bd5653265e97b253102d77d83ea85efdea)
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
14*f03868bdSEduardo Habkostfrom __future__ import print_function
15235fe3bfSAnthony Liguoriimport sys
16235fe3bfSAnthony Liguoriimport os
17235fe3bfSAnthony Liguorifrom qmp import QEMUMonitorProtocol
18235fe3bfSAnthony Liguori
19235fe3bfSAnthony Liguoricmd, args = sys.argv[0], sys.argv[1:]
20235fe3bfSAnthony Liguorisocket_path = None
21235fe3bfSAnthony Liguoripath = None
22235fe3bfSAnthony Liguoriprop = None
23235fe3bfSAnthony Liguorivalue = 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>.<property> <value>
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":
38*f03868bdSEduardo 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:
48235fe3bfSAnthony Liguori    if os.environ.has_key('QMP_SOCKET'):
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 Liguoriif len(args) > 1:
54235fe3bfSAnthony Liguori    try:
55235fe3bfSAnthony Liguori        path, prop = args[0].rsplit('.', 1)
56235fe3bfSAnthony Liguori    except:
57235fe3bfSAnthony Liguori        usage_error("invalid format for path/property/value")
58235fe3bfSAnthony Liguori    value = args[1]
59235fe3bfSAnthony Liguorielse:
60235fe3bfSAnthony Liguori    usage_error("not enough arguments")
61235fe3bfSAnthony Liguori
62235fe3bfSAnthony Liguorisrv = QEMUMonitorProtocol(socket_path)
63235fe3bfSAnthony Liguorisrv.connect()
64235fe3bfSAnthony Liguori
65*f03868bdSEduardo Habkostprint(srv.command('qom-set', path=path, property=prop, value=value))
66