dingjian / rpms / kernel-rt

Forked from rpms/kernel-rt 3 years ago
Clone

Blame SOURCES/sanity_check.py

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