summaryrefslogtreecommitdiff
path: root/scripts/group-qemu-caps.py
blob: 8a899a76c2424e218021efaaac9d55c673aeb9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see
# <http://www.gnu.org/licenses/>.
#
#
# Regroup array values into smaller groups separated by numbered comments.
#
# If --check is the first parameter, the script will return
# a non-zero value if a file is not grouped correctly.
# Otherwise the files are regrouped in place.

import argparse
import re
import subprocess
import sys


def load_caps_flags(filename, start_regex, end_regex):
    capsflags = []
    game_on = False

    with open(filename, "r") as fh:
        for line in fh:
            line = line.rstrip("\n")
            if game_on:
                if re.search(r'''.*/\* [0-9]+ \*/.*''', line):
                    continue
                if re.search(r'''^\s*$''', line):
                    continue
                match = re.search(r'''[ ]+([A-Z0-9_]+)''', line)

                if match:
                    capsflags.append(match[1])

            if re.search(start_regex, line):
                game_on = True
            elif game_on and re.search(end_regex, line):
                game_on = False

    return capsflags


def regroup_caps(check, filename, start_regex, end_regex,
                 trailing_newline, counter_prefix, capsflags):
    step = 5

    original = []
    with open(filename, "r") as fh:
        for line in fh:
            original.append(line)

    fixed = []
    game_on = False
    counter = 0
    for line in original:
        line = line.rstrip("\n")
        if game_on:
            if re.search(r'''.*/\* [0-9]+ \*/.*''', line):
                continue
            if re.search(r'''^\s*$''', line):
                continue
            if counter % step == 0:
                if counter != 0:
                    fixed.append("\n")
                fixed.append("%s/* %d */\n" % (counter_prefix, counter))

            if not (line.find("/*") != -1 and line.find("*/") == -1):
                # count two-line comments as one line
                counter = counter + 1

        if re.search(start_regex, line):
            game_on = True
        elif game_on and re.search(end_regex, line):
            if (counter - 1) % step == 0:
                fixed = fixed[:-1]  # /* $counter */
                if counter != 1:
                    fixed = fixed[:-1]  # \n

            if trailing_newline:
                fixed.append("\n")

            game_on = False

        # ensure that flag names in the .c file have the correct flag in the comment
        if game_on and capsflags:
            flagname = re.search(r'''.*".*",''', line)
            if flagname:
                line = flagname[0] + " /* %s */" % capsflags[counter - 1]

        fixed.append(line + "\n")

    if check:
        orig = "".join(original)
        new = "".join(fixed)
        if new != orig:
            diff = subprocess.Popen(["diff", "-u", filename, "-"],
                                    stdin=subprocess.PIPE)
            diff.communicate(input=new.encode('utf-8'))

            print("Incorrect line wrapping in '%s'" %
                  filename, file=sys.stderr)
            print("Use group-qemu-caps.py to generate data files",
                  file=sys.stderr)
            return False
    else:
        with open(filename, "w") as fh:
            for line in fixed:
                print(line, file=fh, end='')

    return True


parser = argparse.ArgumentParser(description='QEMU capabilities group formatter')
parser.add_argument('--check', action="store_true",
                    help='check existing files only')
parser.add_argument('--prefix', default='',
                    help='source code tree prefix')
args = parser.parse_args()

errs = False

capsflags = load_caps_flags(args.prefix + 'src/qemu/qemu_capabilities.h',
                            r'virQEMUCapsFlags grouping marker',
                            r'QEMU_CAPS_LAST \/\* this must')

if not regroup_caps(args.check,
                    args.prefix + 'src/qemu/qemu_capabilities.c',
                    r'virQEMUCaps grouping marker',
                    r'\);',
                    0,
                    "              ",
                    capsflags):
    errs = True

if not regroup_caps(args.check,
                    args.prefix + 'src/qemu/qemu_capabilities.h',
                    r'virQEMUCapsFlags grouping marker',
                    r'QEMU_CAPS_LAST \/\* this must',
                    1,
                    "    ",
                    None):
    errs = True

if errs:
    sys.exit(1)
sys.exit(0)