summaryrefslogtreecommitdiff
path: root/scripts/check-config.py
blob: 79c2bdf5975948157014dc20d9dadec62208401e (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
#! /usr/bin/env python

# Copyright (c) 2015, The Linux Foundation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of The Linux Foundation nor
#       the names of its contributors may be used to endorse or promote
#       products derived from this software without specific prior written
#       permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
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()