]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-config.py
- catch lines where no value is set
[packages/kernel.git] / kernel-config.py
CommitLineData
744f880e
ER
1#!/usr/bin/python
2
3# Generate kernel .config file based on special kernel.conf rules file.
4# arekm@pld-linux.org
3d878eb7 5# glen@pld-linux.org
744f880e
ER
6
7import sys
8import re
9
83c9dd8d
ER
10argc = len(sys.argv)
11if argc < 4 or argc > 5:
12 print "Usage: %s target_arch kernel.conf input-config [output-config]" % sys.argv[0]
744f880e
ER
13 sys.exit(1)
14
15arch = sys.argv[1]
16kernelconfig = sys.argv[2]
17inconfig = sys.argv[3]
83c9dd8d
ER
18if argc == 5:
19 outconfig = sys.argv[4]
20else:
21 outconfig = inconfig
744f880e
ER
22
23from UserDict import UserDict
24
25# odict from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
26class 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
77dict = odict()
78
fb2384ff 79rc = 0
744f880e
ER
80f = open(kernelconfig, 'r')
81for l in f:
82 if l[:6] == 'CONFIG_':
83 print "Omit CONFIG_ when specifing symbol name: %s" % l
fb2384ff 84 rc = 1
744f880e 85 continue
fb2384ff 86
744f880e
ER
87 if re.match('^#', l) or re.match('^\s*$', l):
88 continue
fb2384ff 89
744f880e
ER
90 if not re.match('^[0-9A-Z]+', l):
91 print "Unknown line: %s" % l
fb2384ff
ER
92 rc = 1
93 continue
94
744f880e
ER
95 c = l.strip().split()
96 symbol = c[0]
97 if dict.has_key(symbol):
9a337b10 98 print "Duplicate symbol: %s" % symbol
fb2384ff 99 rc = 1
98c34c69 100 continue
744f880e 101
83c9dd8d 102 # inline symbols: for current arch, duplicates allowed
e3d1eec9 103 if symbol.find('=') > 0:
83c9dd8d 104 (symbol, value) = symbol.split('=')
d03b3806
ER
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
83c9dd8d
ER
111 continue
112
9a337b10
ER
113 hash = {}
114 for item in c[1:]:
012edc48 115 try:
0ebaf214
ER
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
012edc48
ER
123 except IndexError:
124 print "Invalid line: %s" % l.strip()
9a337b10 125 err = 1
012edc48
ER
126 continue
127
9a337b10
ER
128 if len(hash) == 0:
129 print "Bad line: %s" % l
fb2384ff 130 rc = 1
9a337b10
ER
131
132 # take arch line, otherwise fallback to 'all'
133 if hash.has_key(arch):
7c1b197b
ER
134 # allow empty value to skip symbol on this arch
135 if not hash[arch] == "":
136 dict[symbol] = hash[arch]
3d878eb7
ER
137 else:
138 if hash.has_key('all'):
139 dict[symbol] = hash['all']
744f880e
ER
140
141f.close()
142
fb2384ff
ER
143if not rc == 0:
144 sys.exit(1)
145
744f880e
ER
146f = open(inconfig, 'r')
147cfg = f.read()
148f.close()
149
7c1b197b 150rc = 0
744f880e
ER
151cfg += '\n# PLD configs\n'
152for 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
708a0472
ER
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)
5e16350c 171 elif re.compile('^0x[0-9A-Fa-f]+$').match(val):
744f880e
ER
172 cfg += "CONFIG_%s=%s\n" % (key, val)
173 else:
174 print "Unknown value [%s] for key: %s" % (val, key)
7c1b197b
ER
175 rc = 1
176
177if not rc == 0:
178 sys.exit(rc)
744f880e
ER
179
180f = open(outconfig, 'w')
181f.write(cfg)
182f.close()
This page took 0.065361 seconds and 4 git commands to generate.