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