]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/fromfolder.py
kconfig2sat: More work
[linux-conf-perf.git] / scripts / fromfolder.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import re
5 from conf import conf
6 from conf import sf
7
8 def loadfromKconfig(file):
9         found = set()
10         for ln in open(file):
11                 if re.search('^config', ln):
12                         name = ln[7:].rstrip()
13                         found.add(name)
14         return found
15
16 def loadfromfolder(folder):
17         found = set()
18         for l in os.listdir(folder):
19                 if os.path.isdir(folder + '/' + l):
20                         found = found | loadfromfolder(folder + '/' + l)
21                 elif os.path.isfile(folder + '/' + l) and re.search('Kconfig', l):
22                         found =  found | loadfromKconfig(folder + '/' + l)
23         return found
24
25 def removefrom(file, outfile, removelist):
26         alllines = []
27         for ln in open(file):
28                 alllines.append(ln.rstrip())
29         
30         for rl in removelist:
31                 for ln in alllines:
32                         if re.search('^CONFIG_' + rl + '=', ln):
33                                 print('removing ' + ln)
34                                 alllines.remove(ln)
35         
36         with open(outfile, 'w') as f:
37                 for ln in alllines:
38                         f.write(ln + '\n')
39         
40 #################################################################################
41
42 # TODO propper argument parsing
43 if __name__ == '__main__':
44         folder = sys.argv[1]
45         inputfile = sys.argv[2]
46         outputfile = sys.argv[3]
47         if not len(sys.argv) < 5:
48                 exceptfile = sys.argv[4]
49
50         exceptconf = set()
51         try:
52                 if os.path.isfile(exceptfile):
53                         for ln in open(exceptfile):
54                                 lns = ln.rstrip()
55                                 if lns:
56                                         exceptconf.add(lns)
57         except NameError:
58                 pass
59
60         rem = loadfromfolder(sf(conf.linux_sources + '/' + folder))
61
62         for ec in exceptconf:
63                 rrlist = []
64                 for r in rem:
65                         if re.match(ec, r):
66                                 print('except ' + r)
67                                 rrlist.append(r)
68                 for r in rrlist:
69                         try:
70                                 rem.remove(r)
71                         except KeyError:
72                                 pass
73
74         removefrom(inputfile, outputfile, rem)