Rev 567 | Rev 573 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 545 | palkovsky | 1 | #!/usr/bin/env python |
| 2 | """ |
||
| 3 | Kernel configuration script |
||
| 4 | """ |
||
| 5 | import sys |
||
| 6 | import os |
||
| 7 | import re |
||
| 561 | decky | 8 | import commands |
| 545 | palkovsky | 9 | |
| 10 | INPUT = 'kernel.config' |
||
| 11 | OUTPUT = 'Makefile.config' |
||
| 12 | TMPOUTPUT = 'Makefile.config.tmp' |
||
| 13 | |||
| 14 | class DefaultDialog: |
||
| 15 | "Wrapper dialog that tries to return default values" |
||
| 16 | def __init__(self, dlg): |
||
| 17 | self.dlg = dlg |
||
| 18 | |||
| 19 | def set_title(self,text): |
||
| 20 | self.dlg.set_title(text) |
||
| 21 | |||
| 22 | def yesno(self, text, default=None): |
||
| 23 | if default is not None: |
||
| 24 | return default |
||
| 25 | return self.dlg.yesno(text, default) |
||
| 26 | def noyes(self, text, default=None): |
||
| 27 | if default is not None: |
||
| 28 | return default |
||
| 29 | return self.dlg.noyes(text, default) |
||
| 30 | |||
| 31 | def choice(self, text, choices, defopt=None): |
||
| 32 | if defopt is not None: |
||
| 33 | return choices[defopt][0] |
||
| 34 | return self.dlg.choice(text, choices, defopt) |
||
| 35 | |||
| 36 | class NoDialog: |
||
| 37 | def __init__(self): |
||
| 38 | self.printed = None |
||
| 39 | self.title = 'HelenOS Configuration' |
||
| 40 | |||
| 41 | def print_title(self): |
||
| 42 | if not self.printed: |
||
| 552 | palkovsky | 43 | sys.stdout.write("\n*** %s ***\n" % self.title) |
| 545 | palkovsky | 44 | self.printed = True |
| 45 | |||
| 46 | def set_title(self, text): |
||
| 47 | self.title = text |
||
| 48 | self.printed = False |
||
| 49 | |||
| 50 | def noyes(self, text, default=None): |
||
| 51 | if not default: |
||
| 52 | default = 'n' |
||
| 53 | return self.yesno(text, default) |
||
| 54 | |||
| 55 | def yesno(self, text, default=None): |
||
| 56 | self.print_title() |
||
| 57 | |||
| 58 | if default != 'n': |
||
| 59 | default = 'y' |
||
| 60 | while 1: |
||
| 61 | sys.stdout.write("%s (y/n)[%s]: " % (text,default)) |
||
| 62 | inp = sys.stdin.readline() |
||
| 63 | if not inp: |
||
| 64 | raise EOFError |
||
| 65 | inp = inp.strip().lower() |
||
| 66 | if not inp: |
||
| 67 | return default |
||
| 68 | if inp == 'y': |
||
| 69 | return 'y' |
||
| 70 | elif inp == 'n': |
||
| 71 | return 'n' |
||
| 72 | |||
| 73 | def _print_choice(self, text, choices, defopt): |
||
| 74 | sys.stdout.write('%s:\n' % text) |
||
| 75 | for i,(text,descr) in enumerate(choices): |
||
| 76 | sys.stdout.write('\t%2d. %s\n' % (i, descr)) |
||
| 77 | if defopt is not None: |
||
| 78 | sys.stdout.write('Enter choice number[%d]: ' % defopt) |
||
| 79 | else: |
||
| 80 | sys.stdout.write('Enter choice number: ') |
||
| 554 | palkovsky | 81 | |
| 82 | def menu(self, text, choices, button, defopt=None): |
||
| 555 | palkovsky | 83 | menu = [] |
| 84 | for key, descr in choices: |
||
| 85 | txt = key + (45-len(key))*' ' + ': ' + descr |
||
| 86 | menu.append((key, txt)) |
||
| 87 | |||
| 88 | return self.choice(text, [button] + menu) |
||
| 545 | palkovsky | 89 | |
| 90 | def choice(self, text, choices, defopt=None): |
||
| 91 | self.print_title() |
||
| 92 | while 1: |
||
| 93 | self._print_choice(text, choices, defopt) |
||
| 94 | inp = sys.stdin.readline() |
||
| 95 | if not inp: |
||
| 96 | raise EOFError |
||
| 97 | if not inp.strip(): |
||
| 98 | if defopt is not None: |
||
| 99 | return choices[defopt][0] |
||
| 100 | continue |
||
| 101 | try: |
||
| 102 | number = int(inp.strip()) |
||
| 103 | except ValueError: |
||
| 104 | continue |
||
| 105 | if number < 0 or number >= len(choices): |
||
| 106 | continue |
||
| 107 | return choices[number][0] |
||
| 108 | |||
| 109 | |||
| 110 | class Dialog(NoDialog): |
||
| 111 | def __init__(self): |
||
| 112 | NoDialog.__init__(self) |
||
| 113 | self.dlgcmd = os.environ.get('DIALOG','dialog') |
||
| 554 | palkovsky | 114 | self.title = '' |
| 115 | self.backtitle = 'HelenOS Kernel Configuration' |
||
| 545 | palkovsky | 116 | |
| 117 | if os.system('%s --print-maxsize >/dev/null 2>&1' % self.dlgcmd) != 0: |
||
| 118 | raise NotImplementedError |
||
| 119 | |||
| 120 | def set_title(self,text): |
||
| 121 | self.title = text |
||
| 122 | |||
| 123 | def calldlg(self,*args,**kw): |
||
| 549 | palkovsky | 124 | "Wrapper for calling 'dialog' program" |
| 545 | palkovsky | 125 | indesc, outdesc = os.pipe() |
| 126 | pid = os.fork() |
||
| 127 | if not pid: |
||
| 128 | os.close(2) |
||
| 129 | os.dup(outdesc) |
||
| 130 | os.close(indesc) |
||
| 131 | |||
| 554 | palkovsky | 132 | dlgargs = [self.dlgcmd,'--title',self.title, |
| 133 | '--backtitle', self.backtitle] |
||
| 545 | palkovsky | 134 | for key,val in kw.items(): |
| 135 | dlgargs.append('--'+key) |
||
| 136 | dlgargs.append(val) |
||
| 137 | dlgargs += args |
||
| 138 | os.execlp(self.dlgcmd,*dlgargs) |
||
| 139 | |||
| 140 | os.close(outdesc) |
||
| 558 | palkovsky | 141 | |
| 142 | try: |
||
| 143 | errout = os.fdopen(indesc,'r') |
||
| 144 | data = errout.read() |
||
| 145 | errout.close() |
||
| 146 | pid,status = os.wait() |
||
| 147 | except: |
||
| 148 | os.system('reset') # Reset terminal |
||
| 149 | raise |
||
| 150 | |||
| 545 | palkovsky | 151 | if not os.WIFEXITED(status): |
| 558 | palkovsky | 152 | os.system('reset') # Reset terminal |
| 545 | palkovsky | 153 | raise EOFError |
| 558 | palkovsky | 154 | |
| 545 | palkovsky | 155 | status = os.WEXITSTATUS(status) |
| 156 | if status == 255: |
||
| 157 | raise EOFError |
||
| 158 | return status,data |
||
| 159 | |||
| 160 | def yesno(self, text, default=None): |
||
| 558 | palkovsky | 161 | if text[-1] not in ('?',':'): |
| 162 | text = text + ':' |
||
| 545 | palkovsky | 163 | width = '50' |
| 164 | height = '5' |
||
| 165 | if len(text) < 48: |
||
| 166 | text = ' '*int(((48-len(text))/2)) + text |
||
| 167 | else: |
||
| 168 | width = '0' |
||
| 169 | height = '0' |
||
| 170 | if default == 'n': |
||
| 171 | res,data = self.calldlg('--defaultno','--yesno',text,height,width) |
||
| 172 | else: |
||
| 173 | res,data = self.calldlg('--yesno',text,height,width) |
||
| 174 | |||
| 175 | if res == 0: |
||
| 176 | return 'y' |
||
| 177 | return 'n' |
||
| 554 | palkovsky | 178 | |
| 179 | def menu(self, text, choices, button, defopt=None): |
||
| 180 | text = text + ':' |
||
| 181 | width = '70' |
||
| 182 | height = str(8 + len(choices)) |
||
| 183 | args = [] |
||
| 184 | for key,val in choices: |
||
| 185 | args.append(key) |
||
| 186 | args.append(val) |
||
| 187 | |||
| 188 | kw = {} |
||
| 189 | if defopt: |
||
| 190 | kw['default-item'] = choices[defopt][0] |
||
| 567 | palkovsky | 191 | res,data = self.calldlg('--ok-label','Change', |
| 192 | '--extra-label',button[1], |
||
| 559 | palkovsky | 193 | '--extra-button', |
| 554 | palkovsky | 194 | '--menu',text,height,width, |
| 195 | str(len(choices)),*args,**kw) |
||
| 559 | palkovsky | 196 | if res == 3: |
| 554 | palkovsky | 197 | return button[0] |
| 559 | palkovsky | 198 | if res == 1: # Cancel |
| 199 | sys.exit(1) |
||
| 554 | palkovsky | 200 | elif res: |
| 201 | print data |
||
| 202 | raise EOFError |
||
| 203 | return data |
||
| 545 | palkovsky | 204 | |
| 205 | def choice(self, text, choices, defopt=None): |
||
| 206 | text = text + ':' |
||
| 207 | width = '50' |
||
| 208 | height = str(8 + len(choices)) |
||
| 209 | args = [] |
||
| 210 | for key,val in choices: |
||
| 211 | args.append(key) |
||
| 212 | args.append(val) |
||
| 213 | |||
| 214 | kw = {} |
||
| 215 | if defopt: |
||
| 216 | kw['default-item'] = choices[defopt][0] |
||
| 217 | res,data = self.calldlg('--nocancel','--menu',text,height,width, |
||
| 218 | str(len(choices)),*args, **kw) |
||
| 219 | if res: |
||
| 220 | print data |
||
| 221 | raise EOFError |
||
| 222 | return data |
||
| 223 | |||
| 547 | palkovsky | 224 | def read_defaults(fname,defaults): |
| 549 | palkovsky | 225 | "Read saved values from last configuration run" |
| 545 | palkovsky | 226 | f = file(fname,'r') |
| 227 | for line in f: |
||
| 547 | palkovsky | 228 | res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line) |
| 545 | palkovsky | 229 | if res: |
| 230 | defaults[res.group(1)] = res.group(2) |
||
| 231 | f.close() |
||
| 232 | |||
| 568 | palkovsky | 233 | def check_condition(text, defaults, asked_names): |
| 234 | seen_vars = [ x[0] for x in asked_names ] |
||
| 235 | ctype = 'cnf' |
||
| 236 | if ')|' in text or '|(' in text: |
||
| 237 | ctype = 'dnf' |
||
| 238 | |||
| 239 | if ctype == 'cnf': |
||
| 240 | conds = text.split('&') |
||
| 241 | else: |
||
| 242 | conds = text.split('|') |
||
| 243 | |||
| 550 | palkovsky | 244 | for cond in conds: |
| 245 | if cond.startswith('(') and cond.endswith(')'): |
||
| 246 | cond = cond[1:-1] |
||
| 568 | palkovsky | 247 | |
| 248 | inside = check_inside(cond, defaults, ctype, seen_vars) |
||
| 249 | |||
| 250 | if ctype == 'cnf' and not inside: |
||
| 550 | palkovsky | 251 | return False |
| 568 | palkovsky | 252 | if ctype == 'dnf' and inside: |
| 253 | return True |
||
| 550 | palkovsky | 254 | |
| 568 | palkovsky | 255 | if ctype == 'cnf': |
| 256 | return True |
||
| 257 | return False |
||
| 258 | |||
| 259 | def check_inside(text, defaults, ctype, seen_vars): |
||
| 550 | palkovsky | 260 | """ |
| 261 | Check that the condition specified on input line is True |
||
| 262 | |||
| 263 | only CNF is supported |
||
| 264 | """ |
||
| 568 | palkovsky | 265 | if ctype == 'cnf': |
| 266 | conds = text.split('|') |
||
| 267 | else: |
||
| 268 | conds = text.split('&') |
||
| 547 | palkovsky | 269 | for cond in conds: |
| 550 | palkovsky | 270 | res = re.match(r'^(.*?)(!?=)(.*)$', cond) |
| 271 | if not res: |
||
| 272 | raise RuntimeError("Invalid condition: %s" % cond) |
||
| 273 | condname = res.group(1) |
||
| 274 | oper = res.group(2) |
||
| 275 | condval = res.group(3) |
||
| 568 | palkovsky | 276 | if condname not in seen_vars: |
| 277 | raise RuntimeError("Variable %s not defined before being asked." %\ |
||
| 278 | condname) |
||
| 547 | palkovsky | 279 | if not defaults.has_key(condname): |
| 280 | raise RuntimeError("Condition var %s does not exist: %s" % \ |
||
| 550 | palkovsky | 281 | (condname,text)) |
| 282 | |||
| 568 | palkovsky | 283 | if ctype == 'cnf': |
| 284 | if oper == '=' and condval == defaults[condname]: |
||
| 285 | return True |
||
| 286 | if oper == '!=' and condval != defaults[condname]: |
||
| 287 | return True |
||
| 288 | else: |
||
| 289 | if oper== '=' and condval != defaults[condname]: |
||
| 290 | return False |
||
| 291 | if oper== '!=' and condval == defaults[condname]: |
||
| 292 | print 2 |
||
| 293 | return False |
||
| 294 | if ctype=='cnf': |
||
| 295 | return False |
||
| 296 | return True |
||
| 547 | palkovsky | 297 | |
| 554 | palkovsky | 298 | def parse_config(input, output, dlg, defaults={}, askonly=None): |
| 549 | palkovsky | 299 | "Parse configuration file and create Makefile.config on the fly" |
| 566 | palkovsky | 300 | def ask_the_question(dialog): |
| 556 | palkovsky | 301 | "Ask question based on the type of variables to ask" |
| 302 | # This is quite a hack, this thingy is written just to |
||
| 303 | # have access to local variables.. |
||
| 304 | if vartype == 'y/n': |
||
| 566 | palkovsky | 305 | return dialog.yesno(comment, default) |
| 556 | palkovsky | 306 | elif vartype == 'n/y': |
| 566 | palkovsky | 307 | return dialog.noyes(comment, default) |
| 556 | palkovsky | 308 | elif vartype == 'choice': |
| 309 | defopt = None |
||
| 310 | if default is not None: |
||
| 311 | for i,(key,val) in enumerate(choices): |
||
| 312 | if key == default: |
||
| 313 | defopt = i |
||
| 314 | break |
||
| 566 | palkovsky | 315 | return dialog.choice(comment, choices, defopt) |
| 556 | palkovsky | 316 | else: |
| 317 | raise RuntimeError("Bad method: %s" % vartype) |
||
| 318 | |||
| 319 | |||
| 545 | palkovsky | 320 | f = file(input, 'r') |
| 321 | outf = file(output, 'w') |
||
| 322 | |||
| 323 | outf.write('#########################################\n') |
||
| 324 | outf.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n') |
||
| 325 | outf.write('#########################################\n\n') |
||
| 326 | |||
| 554 | palkovsky | 327 | asked_names = [] |
| 328 | |||
| 545 | palkovsky | 329 | comment = '' |
| 330 | default = None |
||
| 331 | choices = [] |
||
| 550 | palkovsky | 332 | for line in f: |
| 333 | if line.startswith('%'): |
||
| 334 | res = re.match(r'^%\s*(?:\[(.*?)\])?\s*(.*)$', line) |
||
| 335 | if not res: |
||
| 336 | raise RuntimeError('Invalid command: %s' % line) |
||
| 337 | if res.group(1): |
||
| 568 | palkovsky | 338 | if not check_condition(res.group(1), defaults, |
| 339 | asked_names): |
||
| 550 | palkovsky | 340 | continue |
| 341 | args = res.group(2).strip().split(' ') |
||
| 342 | cmd = args[0].lower() |
||
| 343 | args = args[1:] |
||
| 554 | palkovsky | 344 | if cmd == 'saveas': |
| 550 | palkovsky | 345 | outf.write('%s = %s\n' % (args[1],defaults[args[0]])) |
| 560 | palkovsky | 346 | elif cmd == 'shellcmd': |
| 347 | varname = args[0] |
||
| 348 | args = args[1:] |
||
| 349 | for i,arg in enumerate(args): |
||
| 350 | if arg.startswith('$'): |
||
| 351 | args[i] = defaults[arg[1:]] |
||
| 566 | palkovsky | 352 | data,status = commands.getstatusoutput(' '.join(args)) |
| 353 | if status: |
||
| 560 | palkovsky | 354 | raise RuntimeError('Error running: %s' % ' '.join(args)) |
| 566 | palkovsky | 355 | outf.write('%s = %s\n' % (varname,data.strip())) |
| 550 | palkovsky | 356 | continue |
| 357 | |||
| 545 | palkovsky | 358 | if line.startswith('!'): |
| 549 | palkovsky | 359 | # Ask a question |
| 547 | palkovsky | 360 | res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line) |
| 545 | palkovsky | 361 | if not res: |
| 362 | raise RuntimeError("Weird line: %s" % line) |
||
| 547 | palkovsky | 363 | varname = res.group(2) |
| 364 | vartype = res.group(3) |
||
| 545 | palkovsky | 365 | |
| 366 | default = defaults.get(varname,None) |
||
| 554 | palkovsky | 367 | |
| 547 | palkovsky | 368 | if res.group(1): |
| 568 | palkovsky | 369 | if not check_condition(res.group(1), defaults, |
| 370 | asked_names): |
||
| 547 | palkovsky | 371 | if default is not None: |
| 372 | outf.write('#!# %s = %s\n' % (varname, default)) |
||
| 550 | palkovsky | 373 | # Clear cumulated values |
| 374 | comment = '' |
||
| 375 | default = None |
||
| 376 | choices = [] |
||
| 547 | palkovsky | 377 | continue |
| 554 | palkovsky | 378 | |
| 379 | asked_names.append((varname,comment)) |
||
| 547 | palkovsky | 380 | |
| 556 | palkovsky | 381 | if default is None or not askonly or askonly == varname: |
| 566 | palkovsky | 382 | default = ask_the_question(dlg) |
| 383 | else: |
||
| 384 | default = ask_the_question(DefaultDialog(dlg)) |
||
| 554 | palkovsky | 385 | |
| 556 | palkovsky | 386 | outf.write('%s = %s\n' % (varname, default)) |
| 547 | palkovsky | 387 | # Remeber the selected value |
| 556 | palkovsky | 388 | defaults[varname] = default |
| 545 | palkovsky | 389 | # Clear cumulated values |
| 390 | comment = '' |
||
| 391 | default = None |
||
| 392 | choices = [] |
||
| 393 | continue |
||
| 394 | |||
| 395 | if line.startswith('@'): |
||
| 549 | palkovsky | 396 | # Add new line into the 'choice array' |
| 547 | palkovsky | 397 | res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line) |
| 545 | palkovsky | 398 | if not res: |
| 399 | raise RuntimeError("Bad line: %s" % line) |
||
| 547 | palkovsky | 400 | if res.group(1): |
| 568 | palkovsky | 401 | if not check_condition(res.group(1),defaults, |
| 402 | asked_names): |
||
| 547 | palkovsky | 403 | continue |
| 404 | choices.append((res.group(2), res.group(3))) |
||
| 545 | palkovsky | 405 | continue |
| 549 | palkovsky | 406 | |
| 407 | # All other things print to output file |
||
| 545 | palkovsky | 408 | outf.write(line) |
| 409 | if re.match(r'^#[^#]', line): |
||
| 549 | palkovsky | 410 | # Last comment before question will be displayed to the user |
| 545 | palkovsky | 411 | comment = line[1:].strip() |
| 552 | palkovsky | 412 | elif line.startswith('## '): |
| 549 | palkovsky | 413 | # Set title of the dialog window |
| 545 | palkovsky | 414 | dlg.set_title(line[2:].strip()) |
| 561 | decky | 415 | |
| 416 | outf.write('\n') |
||
| 417 | outf.write('REVISION=%s\n' % commands.getoutput('svnversion . 2> /dev/null')) |
||
| 418 | outf.write('TIMESTAMP=%s\n' % commands.getoutput('date "+%Y-%m-%d %H:%M:%S"')) |
||
| 545 | palkovsky | 419 | outf.close() |
| 420 | f.close() |
||
| 554 | palkovsky | 421 | return asked_names |
| 545 | palkovsky | 422 | |
| 423 | def main(): |
||
| 550 | palkovsky | 424 | defaults = {} |
| 545 | palkovsky | 425 | try: |
| 426 | dlg = Dialog() |
||
| 427 | except NotImplementedError: |
||
| 428 | dlg = NoDialog() |
||
| 429 | |||
| 554 | palkovsky | 430 | if len(sys.argv) == 2 and sys.argv[1]=='default': |
| 431 | defmode = True |
||
| 432 | else: |
||
| 433 | defmode = False |
||
| 434 | |||
| 545 | palkovsky | 435 | # Default run will update the configuration file |
| 436 | # with newest options |
||
| 437 | if os.path.exists(OUTPUT): |
||
| 547 | palkovsky | 438 | read_defaults(OUTPUT, defaults) |
| 555 | palkovsky | 439 | |
| 440 | # Dry run only with defaults |
||
| 554 | palkovsky | 441 | varnames = parse_config(INPUT, TMPOUTPUT, DefaultDialog(dlg), defaults) |
| 442 | # If not in default mode, present selection of all possibilities |
||
| 443 | if not defmode: |
||
| 444 | defopt = 0 |
||
| 445 | while 1: |
||
| 555 | palkovsky | 446 | # varnames contains variable names that were in the |
| 447 | # last question set |
||
| 554 | palkovsky | 448 | choices = [ (x[1],defaults[x[0]]) for x in varnames ] |
| 449 | res = dlg.menu('Configuration',choices,('save','Save'),defopt) |
||
| 450 | if res == 'save': |
||
| 451 | parse_config(INPUT, TMPOUTPUT, DefaultDialog(dlg), defaults) |
||
| 452 | break |
||
| 453 | # transfer description back to varname |
||
| 454 | for i,(vname,descr) in enumerate(varnames): |
||
| 455 | if res == descr: |
||
| 456 | defopt = i |
||
| 457 | break |
||
| 555 | palkovsky | 458 | # Ask the user a simple question, produce output |
| 459 | # as if the user answered all the other questions |
||
| 460 | # with default answer |
||
| 554 | palkovsky | 461 | varnames = parse_config(INPUT, TMPOUTPUT, dlg, defaults, |
| 462 | askonly=varnames[i][0]) |
||
| 463 | |||
| 464 | |||
| 545 | palkovsky | 465 | if os.path.exists(OUTPUT): |
| 466 | os.unlink(OUTPUT) |
||
| 467 | os.rename(TMPOUTPUT, OUTPUT) |
||
| 558 | palkovsky | 468 | |
| 469 | if not defmode and dlg.yesno('Rebuild kernel?') == 'y': |
||
| 568 | palkovsky | 470 | os.execlp('make','make','clean','build') |
| 545 | palkovsky | 471 | |
| 472 | if __name__ == '__main__': |
||
| 473 | main() |