dingjian / rpms / kernel-rt

Forked from rpms/kernel-rt 4 years ago
Clone

Blame SOURCES/sanity_check.py

19f436
#!/usr/bin/python
19f436
# script to sanity check config file values
19f436
19f436
import sys, os
19f436
import re
19f436
19f436
set_regex = re.compile(r'^(?P<key>CONFIG.+)=(?P<value>.+)$')
19f436
unset_regex = re.compile(r'^# (?P<key>CONFIG.+) is not set$')
19f436
19f436
class Config(object):
19f436
19f436
    def __init__(self, file):
19f436
        self.file = file
19f436
        f = open(file)
19f436
        self.lines = f.readlines()
19f436
        f.close()
19f436
        self.configs = {}
19f436
19f436
    def check_values(self):
19f436
        i = 0
19f436
        conflicts = 0
19f436
        while i < len(self.lines):
19f436
            l = self.lines[i].strip()
19f436
            m = set_regex.match(l)
19f436
            if m:
19f436
                key = m.group('key').strip()
19f436
                val = m.group('value').strip()
19f436
            else:
19f436
                m = unset_regex.match(l)
19f436
                if m:
19f436
                    key = m.group('key').strip()
19f436
                    val = None
19f436
                else:
19f436
                    i += 1
19f436
                    continue
19f436
            if self.configs.has_key(key):
19f436
                print "conflicting definition for %s at line %d" % (key, i+1)
19f436
                print "    previous definition at line %d" % (self.configs[key][0])
19f436
                print "    line %d:  %s = %s" % (self.configs[key][0], key, self.configs[key][1])
19f436
                print "    line %d:  %s = %s" % (i+1, key, val)
19f436
                conflicts += 1
19f436
            else:
19f436
                self.configs[key] = (i, val)
19f436
            i += 1
19f436
        return conflicts
19f436
19f436
if __name__ == "__main__":
19f436
    total = 0
19f436
    for f in sys.argv[1:]:
19f436
        c = Config(f)
19f436
        conflicts = c.check_values()
19f436
        if conflicts:
19f436
            print "%d conflicts found" % conflicts
19f436
            total += conflicts
19f436
    sys.exit(total)