aboutsummaryrefslogtreecommitdiff
path: root/scripts/check-config.py
blob: 17d5d5ad5218b57422c32361d70a0cfc46c662dd (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
#! /usr/bin/env python
# SPDX-License-Identifier: GPL-2.0-only

# Copyright (c) 2015, 2018 The Linux Foundation. All rights reserved.

"""
Android kernel configuration validator.

The Android kernel reference trees contain some config stubs of
configuration options that are required for Android to function
correctly, and additional ones that are recommended.

This script can help compare these base configs with the ".config"
output of the compiler to determine if the proper configs are defined.
"""

from collections import namedtuple
from optparse import OptionParser
import re
import sys

version = "check-config.py, version 0.0.1"

req_re = re.compile(r'''^CONFIG_(.*)=(.*)$''')
forb_re = re.compile(r'''^# CONFIG_(.*) is not set$''')
comment_re = re.compile(r'''^(#.*|)$''')

Enabled = namedtuple('Enabled', ['name', 'value'])
Disabled = namedtuple('Disabled', ['name'])

def walk_config(name):
    with open(name, 'r') as fd:
        for line in fd:
            line = line.rstrip()
            m = req_re.match(line)
            if m:
                yield Enabled(m.group(1), m.group(2))
                continue

            m = forb_re.match(line)
            if m:
                yield Disabled(m.group(1))
                continue

            m = comment_re.match(line)
            if m:
                continue

            print "WARNING: Unknown .config line: ", line

class Checker():
    def __init__(self):
        self.required = {}
        self.exempted = set()
        self.forbidden = set()

    def add_required(self, fname):
        for ent in walk_config(fname):
            if type(ent) is Enabled:
                self.required[ent.name] = ent.value
            elif type(ent) is Disabled:
                if ent.name in self.required:
                    del self.required[ent.name]
                self.forbidden.add(ent.name)

    def add_exempted(self, fname):
        with open(fname, 'r') as fd:
            for line in fd:
                line = line.rstrip()
                self.exempted.add(line)

    def check(self, path):
        failure = False

        # Don't run this for mdm targets
        if re.search('mdm', path):
            print "Not applicable to mdm targets... bypassing"
        else:
            for ent in walk_config(path):
                # Go to the next iteration if this config is exempt
                if ent.name in self.exempted:
                   continue

                if type(ent) is Enabled:
                    if ent.name in self.forbidden:
                        print "error: Config should not be present: %s" %ent.name
                        failure = True

                    if ent.name in self.required and ent.value != self.required[ent.name]:
                        print "error: Config has wrong value: %s %s expecting: %s" \
                                 %(ent.name, ent.value, self.required[ent.name])
                        failure = True

                elif type(ent) is Disabled:
                    if ent.name in self.required:
                        print "error: Config should be present, but is disabled: %s" %ent.name
                        failure = True

        if failure:
            sys.exit(1)

def main():
    usage = """%prog [options] path/to/.config"""
    parser = OptionParser(usage=usage, version=version)
    parser.add_option('-r', '--required', dest="required",
            action="append")
    parser.add_option('-e', '--exempted', dest="exempted",
            action="append")
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("Expecting a single path argument to .config")
    elif options.required is None or options.exempted is None:
        parser.error("Expecting a file containing required configurations")

    ch = Checker()
    for r in options.required:
        ch.add_required(r)
    for e in options.exempted:
        ch.add_exempted(e)

    ch.check(args[0])

if __name__ == '__main__':
    main()