Rev 545 | Rev 549 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 545 | Rev 547 | ||
---|---|---|---|
1 | #!/usr/bin/env python |
1 | #!/usr/bin/env python |
2 | """ |
2 | """ |
3 | Kernel configuration script |
3 | Kernel configuration script |
4 | """ |
4 | """ |
5 | import sys |
5 | import sys |
6 | import os |
6 | import os |
7 | import re |
7 | import re |
8 | 8 | ||
9 | INPUT = 'kernel.config' |
9 | INPUT = 'kernel.config' |
10 | OUTPUT = 'Makefile.config' |
10 | OUTPUT = 'Makefile.config' |
11 | TMPOUTPUT = 'Makefile.config.tmp' |
11 | TMPOUTPUT = 'Makefile.config.tmp' |
12 | 12 | ||
13 | class DefaultDialog: |
13 | class DefaultDialog: |
14 | "Wrapper dialog that tries to return default values" |
14 | "Wrapper dialog that tries to return default values" |
15 | def __init__(self, dlg): |
15 | def __init__(self, dlg): |
16 | self.dlg = dlg |
16 | self.dlg = dlg |
17 | 17 | ||
18 | def set_title(self,text): |
18 | def set_title(self,text): |
19 | self.dlg.set_title(text) |
19 | self.dlg.set_title(text) |
20 | 20 | ||
21 | def yesno(self, text, default=None): |
21 | def yesno(self, text, default=None): |
22 | if default is not None: |
22 | if default is not None: |
23 | return default |
23 | return default |
24 | return self.dlg.yesno(text, default) |
24 | return self.dlg.yesno(text, default) |
25 | def noyes(self, text, default=None): |
25 | def noyes(self, text, default=None): |
26 | if default is not None: |
26 | if default is not None: |
27 | return default |
27 | return default |
28 | return self.dlg.noyes(text, default) |
28 | return self.dlg.noyes(text, default) |
29 | 29 | ||
30 | def choice(self, text, choices, defopt=None): |
30 | def choice(self, text, choices, defopt=None): |
31 | if defopt is not None: |
31 | if defopt is not None: |
32 | return choices[defopt][0] |
32 | return choices[defopt][0] |
33 | return self.dlg.choice(text, choices, defopt) |
33 | return self.dlg.choice(text, choices, defopt) |
34 | 34 | ||
35 | class NoDialog: |
35 | class NoDialog: |
36 | def __init__(self): |
36 | def __init__(self): |
37 | self.printed = None |
37 | self.printed = None |
38 | self.title = 'HelenOS Configuration' |
38 | self.title = 'HelenOS Configuration' |
39 | 39 | ||
40 | def print_title(self): |
40 | def print_title(self): |
41 | if not self.printed: |
41 | if not self.printed: |
42 | sys.stdout.write("*** %s ***\n" % self.title) |
42 | sys.stdout.write("*** %s ***\n" % self.title) |
43 | self.printed = True |
43 | self.printed = True |
44 | 44 | ||
45 | def set_title(self, text): |
45 | def set_title(self, text): |
46 | self.title = text |
46 | self.title = text |
47 | self.printed = False |
47 | self.printed = False |
48 | 48 | ||
49 | def noyes(self, text, default=None): |
49 | def noyes(self, text, default=None): |
50 | if not default: |
50 | if not default: |
51 | default = 'n' |
51 | default = 'n' |
52 | return self.yesno(text, default) |
52 | return self.yesno(text, default) |
53 | 53 | ||
54 | def yesno(self, text, default=None): |
54 | def yesno(self, text, default=None): |
55 | self.print_title() |
55 | self.print_title() |
56 | 56 | ||
57 | if default != 'n': |
57 | if default != 'n': |
58 | default = 'y' |
58 | default = 'y' |
59 | while 1: |
59 | while 1: |
60 | sys.stdout.write("%s (y/n)[%s]: " % (text,default)) |
60 | sys.stdout.write("%s (y/n)[%s]: " % (text,default)) |
61 | inp = sys.stdin.readline() |
61 | inp = sys.stdin.readline() |
62 | if not inp: |
62 | if not inp: |
63 | raise EOFError |
63 | raise EOFError |
64 | inp = inp.strip().lower() |
64 | inp = inp.strip().lower() |
65 | if not inp: |
65 | if not inp: |
66 | return default |
66 | return default |
67 | if inp == 'y': |
67 | if inp == 'y': |
68 | return 'y' |
68 | return 'y' |
69 | elif inp == 'n': |
69 | elif inp == 'n': |
70 | return 'n' |
70 | return 'n' |
71 | 71 | ||
72 | def _print_choice(self, text, choices, defopt): |
72 | def _print_choice(self, text, choices, defopt): |
73 | sys.stdout.write('%s:\n' % text) |
73 | sys.stdout.write('%s:\n' % text) |
74 | for i,(text,descr) in enumerate(choices): |
74 | for i,(text,descr) in enumerate(choices): |
75 | sys.stdout.write('\t%2d. %s\n' % (i, descr)) |
75 | sys.stdout.write('\t%2d. %s\n' % (i, descr)) |
76 | if defopt is not None: |
76 | if defopt is not None: |
77 | sys.stdout.write('Enter choice number[%d]: ' % defopt) |
77 | sys.stdout.write('Enter choice number[%d]: ' % defopt) |
78 | else: |
78 | else: |
79 | sys.stdout.write('Enter choice number: ') |
79 | sys.stdout.write('Enter choice number: ') |
80 | 80 | ||
81 | def choice(self, text, choices, defopt=None): |
81 | def choice(self, text, choices, defopt=None): |
82 | self.print_title() |
82 | self.print_title() |
83 | while 1: |
83 | while 1: |
84 | self._print_choice(text, choices, defopt) |
84 | self._print_choice(text, choices, defopt) |
85 | inp = sys.stdin.readline() |
85 | inp = sys.stdin.readline() |
86 | if not inp: |
86 | if not inp: |
87 | raise EOFError |
87 | raise EOFError |
88 | if not inp.strip(): |
88 | if not inp.strip(): |
89 | if defopt is not None: |
89 | if defopt is not None: |
90 | return choices[defopt][0] |
90 | return choices[defopt][0] |
91 | continue |
91 | continue |
92 | try: |
92 | try: |
93 | number = int(inp.strip()) |
93 | number = int(inp.strip()) |
94 | except ValueError: |
94 | except ValueError: |
95 | continue |
95 | continue |
96 | if number < 0 or number >= len(choices): |
96 | if number < 0 or number >= len(choices): |
97 | continue |
97 | continue |
98 | return choices[number][0] |
98 | return choices[number][0] |
99 | 99 | ||
100 | 100 | ||
101 | class Dialog(NoDialog): |
101 | class Dialog(NoDialog): |
102 | def __init__(self): |
102 | def __init__(self): |
103 | NoDialog.__init__(self) |
103 | NoDialog.__init__(self) |
104 | self.dlgcmd = os.environ.get('DIALOG','dialog') |
104 | self.dlgcmd = os.environ.get('DIALOG','dialog') |
105 | self.title = 'HelenOS Configuration' |
105 | self.title = 'HelenOS Configuration' |
106 | 106 | ||
107 | if os.system('%s --print-maxsize >/dev/null 2>&1' % self.dlgcmd) != 0: |
107 | if os.system('%s --print-maxsize >/dev/null 2>&1' % self.dlgcmd) != 0: |
108 | raise NotImplementedError |
108 | raise NotImplementedError |
109 | 109 | ||
110 | def set_title(self,text): |
110 | def set_title(self,text): |
111 | self.title = text |
111 | self.title = text |
112 | 112 | ||
113 | def calldlg(self,*args,**kw): |
113 | def calldlg(self,*args,**kw): |
114 | indesc, outdesc = os.pipe() |
114 | indesc, outdesc = os.pipe() |
115 | pid = os.fork() |
115 | pid = os.fork() |
116 | if not pid: |
116 | if not pid: |
117 | os.close(2) |
117 | os.close(2) |
118 | os.dup(outdesc) |
118 | os.dup(outdesc) |
119 | os.close(indesc) |
119 | os.close(indesc) |
120 | 120 | ||
121 | dlgargs = [self.dlgcmd,'--title',self.title] |
121 | dlgargs = [self.dlgcmd,'--title',self.title] |
122 | for key,val in kw.items(): |
122 | for key,val in kw.items(): |
123 | dlgargs.append('--'+key) |
123 | dlgargs.append('--'+key) |
124 | dlgargs.append(val) |
124 | dlgargs.append(val) |
125 | dlgargs += args |
125 | dlgargs += args |
126 | os.execlp(self.dlgcmd,*dlgargs) |
126 | os.execlp(self.dlgcmd,*dlgargs) |
127 | 127 | ||
128 | os.close(outdesc) |
128 | os.close(outdesc) |
129 | errout = os.fdopen(indesc,'r') |
129 | errout = os.fdopen(indesc,'r') |
130 | data = errout.read() |
130 | data = errout.read() |
131 | errout.close() |
131 | errout.close() |
132 | 132 | ||
133 | pid,status = os.wait() |
133 | pid,status = os.wait() |
134 | if not os.WIFEXITED(status): |
134 | if not os.WIFEXITED(status): |
135 | raise EOFError |
135 | raise EOFError |
136 | status = os.WEXITSTATUS(status) |
136 | status = os.WEXITSTATUS(status) |
137 | if status == 255: |
137 | if status == 255: |
138 | raise EOFError |
138 | raise EOFError |
139 | return status,data |
139 | return status,data |
140 | 140 | ||
141 | def yesno(self, text, default=None): |
141 | def yesno(self, text, default=None): |
142 | text = text + ':' |
142 | text = text + ':' |
143 | width = '50' |
143 | width = '50' |
144 | height = '5' |
144 | height = '5' |
145 | if len(text) < 48: |
145 | if len(text) < 48: |
146 | text = ' '*int(((48-len(text))/2)) + text |
146 | text = ' '*int(((48-len(text))/2)) + text |
147 | else: |
147 | else: |
148 | width = '0' |
148 | width = '0' |
149 | height = '0' |
149 | height = '0' |
150 | if default == 'n': |
150 | if default == 'n': |
151 | res,data = self.calldlg('--defaultno','--yesno',text,height,width) |
151 | res,data = self.calldlg('--defaultno','--yesno',text,height,width) |
152 | else: |
152 | else: |
153 | res,data = self.calldlg('--yesno',text,height,width) |
153 | res,data = self.calldlg('--yesno',text,height,width) |
154 | 154 | ||
155 | if res == 0: |
155 | if res == 0: |
156 | return 'y' |
156 | return 'y' |
157 | return 'n' |
157 | return 'n' |
158 | 158 | ||
159 | def choice(self, text, choices, defopt=None): |
159 | def choice(self, text, choices, defopt=None): |
160 | text = text + ':' |
160 | text = text + ':' |
161 | width = '50' |
161 | width = '50' |
162 | height = str(8 + len(choices)) |
162 | height = str(8 + len(choices)) |
163 | args = [] |
163 | args = [] |
164 | for key,val in choices: |
164 | for key,val in choices: |
165 | args.append(key) |
165 | args.append(key) |
166 | args.append(val) |
166 | args.append(val) |
167 | 167 | ||
168 | kw = {} |
168 | kw = {} |
169 | if defopt: |
169 | if defopt: |
170 | kw['default-item'] = choices[defopt][0] |
170 | kw['default-item'] = choices[defopt][0] |
171 | res,data = self.calldlg('--nocancel','--menu',text,height,width, |
171 | res,data = self.calldlg('--nocancel','--menu',text,height,width, |
172 | str(len(choices)),*args, **kw) |
172 | str(len(choices)),*args, **kw) |
173 | if res: |
173 | if res: |
174 | print data |
174 | print data |
175 | raise EOFError |
175 | raise EOFError |
176 | return data |
176 | return data |
177 | 177 | ||
178 | def read_defaults(fname): |
178 | def read_defaults(fname,defaults): |
179 | defaults = {} |
- | |
180 | f = file(fname,'r') |
179 | f = file(fname,'r') |
181 | for line in f: |
180 | for line in f: |
182 | res = re.match(r'^([^#]\w*)\s*=\s*(.*?)\s*$', line) |
181 | res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line) |
183 | if res: |
182 | if res: |
184 | defaults[res.group(1)] = res.group(2) |
183 | defaults[res.group(1)] = res.group(2) |
185 | f.close() |
184 | f.close() |
- | 185 | ||
- | 186 | def check_condition(text, defaults): |
|
- | 187 | result = False |
|
- | 188 | conds = text.split('|') |
|
- | 189 | for cond in conds: |
|
- | 190 | condname,condval = cond.split('=') |
|
- | 191 | if not defaults.has_key(condname): |
|
- | 192 | raise RuntimeError("Condition var %s does not exist: %s" % \ |
|
- | 193 | (condname,line)) |
|
- | 194 | # None means wildcard |
|
- | 195 | if defaults[condname] is None: |
|
- | 196 | return True |
|
- | 197 | if condval == defaults[condname]: |
|
- | 198 | return True |
|
186 | return defaults |
199 | return False |
187 | 200 | ||
188 | def parse_config(input, output, dlg, defaults={}): |
201 | def parse_config(input, output, dlg, defaults={}): |
189 | f = file(input, 'r') |
202 | f = file(input, 'r') |
190 | outf = file(output, 'w') |
203 | outf = file(output, 'w') |
191 | 204 | ||
192 | outf.write('#########################################\n') |
205 | outf.write('#########################################\n') |
193 | outf.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n') |
206 | outf.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n') |
194 | outf.write('#########################################\n\n') |
207 | outf.write('#########################################\n\n') |
195 | 208 | ||
196 | comment = '' |
209 | comment = '' |
197 | default = None |
210 | default = None |
198 | choices = [] |
211 | choices = [] |
199 | for line in f: |
212 | for line in f: |
200 | if line.startswith('!'): |
213 | if line.startswith('!'): |
201 | res = re.search(r'!\s*([^\s]+)\s*\((.*)\)\s*$', line) |
214 | res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line) |
202 | if not res: |
215 | if not res: |
203 | raise RuntimeError("Weird line: %s" % line) |
216 | raise RuntimeError("Weird line: %s" % line) |
204 | varname = res.group(1) |
217 | varname = res.group(2) |
205 | vartype = res.group(2) |
218 | vartype = res.group(3) |
206 | 219 | ||
207 | default = defaults.get(varname,None) |
220 | default = defaults.get(varname,None) |
208 | 221 | ||
- | 222 | if res.group(1): |
|
- | 223 | if not check_condition(res.group(1), defaults): |
|
- | 224 | if default is not None: |
|
- | 225 | outf.write('#!# %s = %s\n' % (varname, default)) |
|
- | 226 | continue |
|
- | 227 | ||
209 | if vartype == 'y/n': |
228 | if vartype == 'y/n': |
210 | result = dlg.yesno(comment, default) |
229 | result = dlg.yesno(comment, default) |
211 | elif vartype == 'n/y': |
230 | elif vartype == 'n/y': |
212 | result = dlg.noyes(comment, default) |
231 | result = dlg.noyes(comment, default) |
213 | elif vartype == 'choice': |
232 | elif vartype == 'choice': |
214 | defopt = None |
233 | defopt = None |
215 | if default is not None: |
234 | if default is not None: |
216 | for i,(key,val) in enumerate(choices): |
235 | for i,(key,val) in enumerate(choices): |
217 | if key == default: |
236 | if key == default: |
218 | defopt = i |
237 | defopt = i |
219 | break |
238 | break |
220 | result = dlg.choice(comment, choices, defopt) |
239 | result = dlg.choice(comment, choices, defopt) |
221 | else: |
240 | else: |
222 | raise RuntimeError("Bad method: %s" % vartype) |
241 | raise RuntimeError("Bad method: %s" % vartype) |
223 | outf.write('%s = %s\n' % (varname, result)) |
242 | outf.write('%s = %s\n' % (varname, result)) |
- | 243 | # Remeber the selected value |
|
- | 244 | defaults[varname] = result |
|
224 | # Clear cumulated values |
245 | # Clear cumulated values |
225 | comment = '' |
246 | comment = '' |
226 | default = None |
247 | default = None |
227 | choices = [] |
248 | choices = [] |
228 | continue |
249 | continue |
229 | 250 | ||
230 | if line.startswith('@'): |
251 | if line.startswith('@'): |
231 | res = re.match(r'@\s*"(.*?)"\s*(.*)$', line) |
252 | res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line) |
232 | if not res: |
253 | if not res: |
233 | raise RuntimeError("Bad line: %s" % line) |
254 | raise RuntimeError("Bad line: %s" % line) |
- | 255 | if res.group(1): |
|
- | 256 | if not check_condition(res.group(1),defaults): |
|
- | 257 | continue |
|
234 | choices.append((res.group(1), res.group(2))) |
258 | choices.append((res.group(2), res.group(3))) |
235 | continue |
259 | continue |
236 | 260 | ||
237 | outf.write(line) |
261 | outf.write(line) |
238 | if re.match(r'^#[^#]', line): |
262 | if re.match(r'^#[^#]', line): |
239 | comment = line[1:].strip() |
263 | comment = line[1:].strip() |
240 | elif line.startswith('##'): |
264 | elif line.startswith('##'): |
241 | dlg.set_title(line[2:].strip()) |
265 | dlg.set_title(line[2:].strip()) |
242 | 266 | ||
243 | outf.close() |
267 | outf.close() |
244 | f.close() |
268 | f.close() |
245 | 269 | ||
246 | def main(): |
270 | def main(): |
247 | defaults = {} |
271 | defaults = {'ARCH':None} |
248 | try: |
272 | try: |
249 | dlg = Dialog() |
273 | dlg = Dialog() |
250 | except NotImplementedError: |
274 | except NotImplementedError: |
251 | dlg = NoDialog() |
275 | dlg = NoDialog() |
252 | 276 | ||
253 | # Default run will update the configuration file |
277 | # Default run will update the configuration file |
254 | # with newest options |
278 | # with newest options |
- | 279 | if len(sys.argv) >= 2: |
|
- | 280 | defaults['ARCH'] = sys.argv[1] |
|
255 | if len(sys.argv) == 2 and sys.argv[1]=='default': |
281 | if len(sys.argv) == 3 and sys.argv[2]=='default': |
256 | dlg = DefaultDialog(dlg) |
282 | dlg = DefaultDialog(dlg) |
257 | 283 | ||
258 | if os.path.exists(OUTPUT): |
284 | if os.path.exists(OUTPUT): |
259 | defaults = read_defaults(OUTPUT) |
285 | read_defaults(OUTPUT, defaults) |
260 | 286 | ||
261 | parse_config(INPUT, TMPOUTPUT, dlg, defaults) |
287 | parse_config(INPUT, TMPOUTPUT, dlg, defaults) |
262 | if os.path.exists(OUTPUT): |
288 | if os.path.exists(OUTPUT): |
263 | os.unlink(OUTPUT) |
289 | os.unlink(OUTPUT) |
264 | os.rename(TMPOUTPUT, OUTPUT) |
290 | os.rename(TMPOUTPUT, OUTPUT) |
265 | 291 | ||
266 | 292 | ||
267 | if __name__ == '__main__': |
293 | if __name__ == '__main__': |
268 | main() |
294 | main() |
269 | 295 |