]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-config.py
- applies
[packages/kernel.git] / kernel-config.py
1 #!/usr/bin/python
2
3 # Generate kernel .config file based on special kernel.conf rules file.
4 # arekm@pld-linux.org
5 # glen@pld-linux.org
6
7 import sys
8 import re
9
10 argc = len(sys.argv)
11 if argc < 4 or argc > 5:
12     print "Usage: %s target_arch kernel.conf input-config [output-config]" % sys.argv[0]
13     sys.exit(1)
14
15 arch = sys.argv[1]
16 kernelconfig = sys.argv[2]
17 inconfig = sys.argv[3]
18 if argc == 5:
19     outconfig = sys.argv[4]
20 else:
21     outconfig = inconfig
22
23 from UserDict import UserDict
24
25 # odict from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
26 class odict(UserDict):
27     def __init__(self, dict = None):
28         self._keys = []
29         UserDict.__init__(self, dict)
30
31     def __delitem__(self, key):
32         UserDict.__delitem__(self, key)
33         self._keys.remove(key)
34
35     def __setitem__(self, key, item):
36         UserDict.__setitem__(self, key, item)
37         if key not in self._keys: self._keys.append(key)
38
39     def clear(self):
40         UserDict.clear(self)
41         self._keys = []
42
43     def copy(self):
44         dict = UserDict.copy(self)
45         dict._keys = self._keys[:]
46         return dict
47
48     def items(self):
49         return zip(self._keys, self.values())
50
51     def keys(self):
52         return self._keys
53
54     def popitem(self):
55         try:
56             key = self._keys[-1]
57         except IndexError:
58             raise KeyError('dictionary is empty')
59
60         val = self[key]
61         del self[key]
62
63         return (key, val)
64
65     def setdefault(self, key, failobj = None):
66         UserDict.setdefault(self, key, failobj)
67         if key not in self._keys: self._keys.append(key)
68
69     def update(self, dict):
70         UserDict.update(self, dict)
71         for key in dict.keys():
72             if key not in self._keys: self._keys.append(key)
73
74     def values(self):
75         return map(self.get, self._keys)
76
77 dict = odict()
78
79 rc = 0
80 f = open(kernelconfig, 'r')
81 for l in f:
82     if l[:6] == 'CONFIG_':
83         print "Omit CONFIG_ when specifing symbol name: %s" % l
84         rc = 1
85         continue
86
87     if re.match('^#', l) or re.match('^\s*$', l):
88         continue
89
90     if not re.match('^[0-9A-Z]+', l):
91         print "Unknown line: %s" % l
92         rc = 1
93         continue
94
95     c = l.strip().split()
96     symbol = c[0]
97     if dict.has_key(symbol):
98         print "Duplicate symbol: %s" % symbol
99         rc = 1
100         continue
101
102     # inline symbols: for current arch, duplicates allowed
103     if symbol.find('=') > 0:
104         (symbol, value) = symbol.split('=')
105         # empty value means delete the symbol
106         if value == "":
107             if dict.has_key(symbol):
108                 del dict[symbol]
109         else:
110             dict[symbol] = value
111         continue
112
113     hash = {}
114     for item in c[1:]:
115         try:
116             try:
117                 (key, value) = item.split('=')
118                 hash[key] = value
119             except ValueError:
120                 print "Invalid line: %s" % l.strip()
121                 err = 1
122                 continue
123         except IndexError:
124             print "Invalid line: %s" % l.strip()
125             err = 1
126             continue
127
128     if len(hash) == 0:
129         print "Bad line: %s" % l
130         rc = 1
131
132     # take arch line, otherwise fallback to 'all'
133     if hash.has_key(arch):
134         # allow empty value to skip symbol on this arch
135         if not hash[arch] == "":
136             dict[symbol] = hash[arch]
137     else:
138         if hash.has_key('all'):
139             dict[symbol] = hash['all']
140
141 f.close()
142
143 if not rc == 0:
144     sys.exit(1)
145
146 f = open(inconfig, 'r')
147 cfg = f.read()
148 f.close()
149
150 rc = 0
151 cfg += '\n# PLD configs\n'
152 for symbol in dict.items():
153     (key, val) = symbol
154
155     # strip contents
156     rp = re.compile("^CONFIG_%s=.*$" % key, re.MULTILINE)
157     cfg = rp.sub("", cfg)
158     rp = re.compile("^# CONFIG_%s is not set$" % key, re.MULTILINE)
159     cfg = rp.sub("", cfg)
160     
161     if val == "y":
162         cfg += "CONFIG_%s=y\n" % key
163     elif val == "m":
164         cfg += "CONFIG_%s=m\n" % key
165     elif val == "n":
166         cfg += "# CONFIG_%s is not set\n" % key
167     elif re.compile('^"[^"]*"$').match(val):
168         cfg += "CONFIG_%s=%s\n" % (key, val)
169     elif re.compile('^-?[0-9]+$').match(val):
170         cfg += "CONFIG_%s=%s\n" % (key, val)
171     elif re.compile('^0x[0-9A-Fa-f]+$').match(val):
172         cfg += "CONFIG_%s=%s\n" % (key, val)
173     else:
174         print "Unknown value [%s] for key: %s" % (val, key)
175         rc = 1
176
177 if not rc == 0:
178     sys.exit(rc)
179
180 f = open(outconfig, 'w')
181 f.write(cfg)
182 f.close()
This page took 0.057555 seconds and 3 git commands to generate.