#!/usr/bin/python import re import os class Kbuild: def __init__(self, root): self.root = root self.confs = {} # dict of lists self.modules = {} self.read_kbuild() def all_kbuild(self): cmd = 'find . '\ ' -regex "%s/[a-z].*/\\(Makefile\\|Kbuild\\)"' \ ' -not -regex ".*/\(' \ 'arch\|Documents\|firmware\|scripts\|raid6test\)/.*"' \ % self.root return os.popen(cmd).read().split() def read_kbuild(self): allfile = [open(f).read() for f in self.all_kbuild()] data = "\n".join(allfile) data = re.sub(r"(?m)^ifeq\s+\(\$\((CONFIG_\S+)\s*\),\s*m\).*", r"obj-$(\1) \\", data) data = re.sub(r"(?m)\\\n\s*|obj-m ", '', data) all = re.findall( r"(?m)obj-\$\((CONFIG_\S+)\)\s*[:+]=(.*)", data) self.data = data for (conf, modules) in all: mods = re.findall(r"(\S+)\.o", modules) for m in mods: m = re.sub("-","_",m) #print conf, "->", m self.confs.setdefault(conf, []).append(m) self.modules.setdefault(m, []).append(conf) def read_modules(self): data = open("/proc/modules").read() return re.findall(r"(?m)^\S+", data) def test_module(self, m): all, conf = m.group(0,1) if conf in self.confs: return "# %s is not set"%conf return all def filter(self, config): #print self.data for m in self.read_modules(): if not self.modules.has_key(m): print m,": Uknown module, may be firemware?" continue for c in self.modules[m]: if c in self.confs: del self.confs[c] data = open(config).read() data = re.sub(r"(?m)^(CONFIG_\S+)=m", self.test_module, data) out = config + ".min" print "Writing new config to:", out open(out, "w").write(data) def main(): kbuild = Kbuild(".") kbuild.filter(".config") main()