Changeset 8c2b3ef in mainline
- Timestamp:
- 2010-12-03T22:22:15Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 35537a7, f5211ee
- Parents:
- 41a7f62 (diff), ba8de9c3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/config.py
r41a7f62 r8c2b3ef 3 3 # Copyright (c) 2006 Ondrej Palkovsky 4 4 # Copyright (c) 2009 Martin Decky 5 # Copyright (c) 2010 Jiri Svoboda 5 6 # All rights reserved. 6 7 # … … 40 41 import xtui 41 42 42 INPUT= sys.argv[1]43 RULES_FILE = sys.argv[1] 43 44 MAKEFILE = 'Makefile.config' 44 45 MACROS = 'config.h' 45 PRE CONF= 'defaults'46 47 def read_ defaults(fname, defaults):46 PRESETS_DIR = 'defaults' 47 48 def read_config(fname, config): 48 49 "Read saved values from last configuration run" 49 50 … … 52 53 for line in inf: 53 54 res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line) 54 if (res):55 defaults[res.group(1)] = res.group(2)55 if res: 56 config[res.group(1)] = res.group(2) 56 57 57 58 inf.close() 58 59 59 def check_condition(text, defaults, ask_names):60 def check_condition(text, config, rules): 60 61 "Check that the condition specified on input line is True (only CNF and DNF is supported)" 61 62 62 63 ctype = 'cnf' 63 64 64 if ( (')|' in text) or ('|(' in text)):65 if (')|' in text) or ('|(' in text): 65 66 ctype = 'dnf' 66 67 67 if (ctype == 'cnf'):68 if ctype == 'cnf': 68 69 conds = text.split('&') 69 70 else: … … 71 72 72 73 for cond in conds: 73 if (cond.startswith('(')) and (cond.endswith(')')):74 if cond.startswith('(') and cond.endswith(')'): 74 75 cond = cond[1:-1] 75 76 76 inside = check_inside(cond, defaults, ctype)77 inside = check_inside(cond, config, ctype) 77 78 78 79 if (ctype == 'cnf') and (not inside): 79 80 return False 80 81 81 if (ctype == 'dnf') and (inside):82 if (ctype == 'dnf') and inside: 82 83 return True 83 84 84 if (ctype == 'cnf'):85 if ctype == 'cnf': 85 86 return True 86 87 return False 87 88 88 def check_inside(text, defaults, ctype):89 def check_inside(text, config, ctype): 89 90 "Check for condition" 90 91 91 if (ctype == 'cnf'):92 if ctype == 'cnf': 92 93 conds = text.split('|') 93 94 else: … … 96 97 for cond in conds: 97 98 res = re.match(r'^(.*?)(!?=)(.*)$', cond) 98 if (not res):99 if not res: 99 100 raise RuntimeError("Invalid condition: %s" % cond) 100 101 … … 103 104 condval = res.group(3) 104 105 105 if (not condname in defaults):106 if not condname in config: 106 107 varval = '' 107 108 else: 108 varval = defaults[condname]109 varval = config[condname] 109 110 if (varval == '*'): 110 111 varval = 'y' 111 112 112 if (ctype == 'cnf'):113 if ctype == 'cnf': 113 114 if (oper == '=') and (condval == varval): 114 115 return True … … 123 124 return False 124 125 125 if (ctype == 'cnf'):126 if ctype == 'cnf': 126 127 return False 127 128 128 129 return True 129 130 130 def parse_ config(fname, ask_names):131 "Parse configurationfile"131 def parse_rules(fname, rules): 132 "Parse rules file" 132 133 133 134 inf = open(fname, 'r') … … 138 139 for line in inf: 139 140 140 if (line.startswith('!')):141 if line.startswith('!'): 141 142 # Ask a question 142 143 res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line) 143 144 144 if (not res):145 if not res: 145 146 raise RuntimeError("Weird line: %s" % line) 146 147 … … 149 150 vartype = res.group(3) 150 151 151 ask_names.append((varname, vartype, name, choices, cond))152 rules.append((varname, vartype, name, choices, cond)) 152 153 name = '' 153 154 choices = [] 154 155 continue 155 156 156 if (line.startswith('@')):157 if line.startswith('@'): 157 158 # Add new line into the 'choices' array 158 159 res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line) … … 164 165 continue 165 166 166 if (line.startswith('%')):167 if line.startswith('%'): 167 168 # Name of the option 168 169 name = line[1:].strip() 169 170 continue 170 171 171 if ((line.startswith('#')) or (line == '\n')):172 if line.startswith('#') or (line == '\n'): 172 173 # Comment or empty line 173 174 continue … … 181 182 "Return '*' if yes, ' ' if no" 182 183 183 if (default == 'y'):184 if default == 'y': 184 185 return '*' 185 186 … … 199 200 cnt = 0 200 201 for key, val in choices: 201 if ( (default) and (key == default)):202 if (default) and (key == default): 202 203 position = cnt 203 204 … … 207 208 (button, value) = xtui.choice_window(screen, name, 'Choose value', options, position) 208 209 209 if (button == 'cancel'):210 if button == 'cancel': 210 211 return None 211 212 212 213 return choices[value][0] 213 214 214 def check_choices(defaults, ask_names): 215 "Check whether all accessible variables have a default" 216 217 for varname, vartype, name, choices, cond in ask_names: 218 if ((cond) and (not check_condition(cond, defaults, ask_names))): 215 ## Infer and verify configuration values. 216 # 217 # Augment @a config with values that can be inferred, purge invalid ones 218 # and verify that all variables have a value (previously specified or inferred). 219 # 220 # @param config Configuration to work on 221 # @param rules Rules 222 # 223 # @return True if configuration is complete and valid, False 224 # otherwise. 225 # 226 def infer_verify_choices(config, rules): 227 "Infer and verify configuration values." 228 229 for rule in rules: 230 varname, vartype, name, choices, cond = rule 231 232 if cond and (not check_condition(cond, config, rules)): 219 233 continue 220 234 221 if (not varname in defaults): 235 if not varname in config: 236 value = None 237 else: 238 value = config[varname] 239 240 if not rule_value_is_valid(rule, value): 241 value = None 242 243 default = rule_get_default(rule) 244 if default != None: 245 config[varname] = default 246 247 if not varname in config: 222 248 return False 223 249 224 250 return True 225 251 226 def create_output(mkname, mcname, defaults, ask_names): 252 ## Get default value from a rule. 253 def rule_get_default(rule): 254 varname, vartype, name, choices, cond = rule 255 256 default = None 257 258 if vartype == 'choice': 259 # If there is just one option, use it 260 if len(choices) == 1: 261 default = choices[0][0] 262 elif vartype == 'y': 263 default = '*' 264 elif vartype == 'n': 265 default = 'n' 266 elif vartype == 'y/n': 267 default = 'y' 268 elif vartype == 'n/y': 269 default = 'n' 270 else: 271 raise RuntimeError("Unknown variable type: %s" % vartype) 272 273 return default 274 275 ## Get option from a rule. 276 # 277 # @param rule Rule for a variable 278 # @param value Current value of the variable 279 # 280 # @return Option (string) to ask or None which means not to ask. 281 # 282 def rule_get_option(rule, value): 283 varname, vartype, name, choices, cond = rule 284 285 option = None 286 287 if vartype == 'choice': 288 # If there is just one option, don't ask 289 if len(choices) != 1: 290 if (value == None): 291 option = "? %s --> " % name 292 else: 293 option = " %s [%s] --> " % (name, value) 294 elif vartype == 'y': 295 pass 296 elif vartype == 'n': 297 pass 298 elif vartype == 'y/n': 299 option = " <%s> %s " % (yes_no(value), name) 300 elif vartype == 'n/y': 301 option =" <%s> %s " % (yes_no(value), name) 302 else: 303 raise RuntimeError("Unknown variable type: %s" % vartype) 304 305 return option 306 307 ## Check if variable value is valid. 308 # 309 # @param rule Rule for the variable 310 # @param value Value of the variable 311 # 312 # @return True if valid, False if not valid. 313 # 314 def rule_value_is_valid(rule, value): 315 varname, vartype, name, choices, cond = rule 316 317 if value == None: 318 return True 319 320 if vartype == 'choice': 321 if not value in [choice[0] for choice in choices]: 322 return False 323 elif vartype == 'y': 324 if value != 'y': 325 return False 326 elif vartype == 'n': 327 if value != 'n': 328 return False 329 elif vartype == 'y/n': 330 if not value in ['y', 'n']: 331 return False 332 elif vartype == 'n/y': 333 if not value in ['y', 'n']: 334 return False 335 else: 336 raise RuntimeError("Unknown variable type: %s" % vartype) 337 338 return True 339 340 def create_output(mkname, mcname, config, rules): 227 341 "Create output configuration" 228 342 … … 238 352 sys.stderr.write("failed\n") 239 353 240 if (len(version) == 3):354 if len(version) == 3: 241 355 revision = version[1] 242 if (version[0] != 1):356 if version[0] != 1: 243 357 revision += 'M' 244 358 revision += ' (%s)' % version[2] … … 259 373 defs = 'CONFIG_DEFS =' 260 374 261 for varname, vartype, name, choices, cond in ask_names:262 if ((cond) and (not check_condition(cond, defaults, ask_names))):375 for varname, vartype, name, choices, cond in rules: 376 if cond and (not check_condition(cond, config, rules)): 263 377 continue 264 378 265 if (not varname in defaults):266 default= ''379 if not varname in config: 380 value = '' 267 381 else: 268 default = defaults[varname]269 if ( default== '*'):270 default= 'y'271 272 outmk.write('# %s\n%s = %s\n\n' % (name, varname, default))273 274 if ((vartype == "y") or (vartype == "n") or (vartype == "y/n") or (vartype == "n/y")):275 if (default == "y"):382 value = config[varname] 383 if (value == '*'): 384 value = 'y' 385 386 outmk.write('# %s\n%s = %s\n\n' % (name, varname, value)) 387 388 if vartype in ["y", "n", "y/n", "n/y"]: 389 if value == "y": 276 390 outmc.write('/* %s */\n#define %s\n\n' % (name, varname)) 277 391 defs += ' -D%s' % varname 278 392 else: 279 outmc.write('/* %s */\n#define %s %s\n#define %s_%s\n\n' % (name, varname, default, varname, default))280 defs += ' -D%s=%s -D%s_%s' % (varname, default, varname, default)281 282 if (revision is not None):393 outmc.write('/* %s */\n#define %s %s\n#define %s_%s\n\n' % (name, varname, value, varname, value)) 394 defs += ' -D%s=%s -D%s_%s' % (varname, value, varname, value) 395 396 if revision is not None: 283 397 outmk.write('REVISION = %s\n' % revision) 284 398 outmc.write('#define REVISION %s\n' % revision) … … 299 413 return list 300 414 301 def read_preconfigured(root, fname, screen, defaults): 415 ## Choose a profile and load configuration presets. 416 # 417 def load_presets(root, fname, screen, config): 302 418 options = [] 303 419 opt2path = {} … … 309 425 canon = os.path.join(path, fname) 310 426 311 if ((os.path.isdir(path)) and (os.path.exists(canon)) and (os.path.isfile(canon))):427 if os.path.isdir(path) and os.path.exists(canon) and os.path.isfile(canon): 312 428 subprofile = False 313 429 … … 317 433 subcanon = os.path.join(subpath, fname) 318 434 319 if ((os.path.isdir(subpath)) and (os.path.exists(subcanon)) and (os.path.isfile(subcanon))):435 if os.path.isdir(subpath) and os.path.exists(subcanon) and os.path.isfile(subcanon): 320 436 subprofile = True 321 437 options.append("%s (%s)" % (name, subname)) … … 323 439 cnt += 1 324 440 325 if (not subprofile):441 if not subprofile: 326 442 options.append(name) 327 443 opt2path[cnt] = (canon, None) … … 330 446 (button, value) = xtui.choice_window(screen, 'Load preconfigured defaults', 'Choose configuration profile', options, None) 331 447 332 if (button == 'cancel'):448 if button == 'cancel': 333 449 return None 334 450 335 read_ defaults(opt2path[value][0], defaults)336 if (opt2path[value][1] != None):337 read_ defaults(opt2path[value][1], defaults)451 read_config(opt2path[value][0], config) 452 if opt2path[value][1] != None: 453 read_config(opt2path[value][1], config) 338 454 339 455 def main(): 340 defaults= {}341 ask_names = []342 343 # Parse configurationfile344 parse_ config(INPUT, ask_names)345 346 # Read defaultsfrom previous run456 config = {} 457 rules = [] 458 459 # Parse rules file 460 parse_rules(RULES_FILE, rules) 461 462 # Read configuration from previous run 347 463 if os.path.exists(MAKEFILE): 348 read_ defaults(MAKEFILE, defaults)349 350 # Default mode: only check defaults and regenerate configuration351 if ( (len(sys.argv) >= 3) and (sys.argv[2] == 'default')):352 if ( check_choices(defaults, ask_names)):353 create_output(MAKEFILE, MACROS, defaults, ask_names)464 read_config(MAKEFILE, config) 465 466 # Default mode: only check values and regenerate configuration files 467 if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'): 468 if (infer_verify_choices(config, rules)): 469 create_output(MAKEFILE, MACROS, config, rules) 354 470 return 0 355 471 356 # Check mode: only check defaults357 if ( (len(sys.argv) >= 3) and (sys.argv[2] == 'check')):358 if (check_choices(defaults, ask_names)):472 # Check mode: only check configuration 473 if (len(sys.argv) >= 3) and (sys.argv[2] == 'check'): 474 if infer_verify_choices(config, rules): 359 475 return 0 360 476 return 1 … … 366 482 while True: 367 483 368 # Cancel out all defaults which have to be deduced369 for varname, vartype, name, choices, cond in ask_names:370 if ( (vartype == 'y') and (varname in defaults) and (defaults[varname] == '*')):371 defaults[varname] = None484 # Cancel out all values which have to be deduced 485 for varname, vartype, name, choices, cond in rules: 486 if (vartype == 'y') and (varname in config) and (config[varname] == '*'): 487 config[varname] = None 372 488 373 489 options = [] … … 377 493 options.append(" --- Load preconfigured defaults ... ") 378 494 379 for varname, vartype, name, choices, cond in ask_names: 380 381 if ((cond) and (not check_condition(cond, defaults, ask_names))): 495 for rule in rules: 496 varname, vartype, name, choices, cond = rule 497 498 if cond and (not check_condition(cond, config, rules)): 382 499 continue 383 500 384 if (varname == selname):501 if varname == selname: 385 502 position = cnt 386 503 387 if (not varname in defaults):388 default= None504 if not varname in config: 505 value = None 389 506 else: 390 default = defaults[varname] 391 392 if (vartype == 'choice'): 393 # Check if the default is an acceptable value 394 if ((default) and (not default in [choice[0] for choice in choices])): 395 default = None 396 defaults.pop(varname) 397 398 # If there is just one option, use it 399 if (len(choices) == 1): 400 defaults[varname] = choices[0][0] 401 continue 402 403 if (default == None): 404 options.append("? %s --> " % name) 405 else: 406 options.append(" %s [%s] --> " % (name, default)) 407 elif (vartype == 'y'): 408 defaults[varname] = '*' 409 continue 410 elif (vartype == 'n'): 411 defaults[varname] = 'n' 412 continue 413 elif (vartype == 'y/n'): 414 if (default == None): 415 default = 'y' 416 defaults[varname] = default 417 options.append(" <%s> %s " % (yes_no(default), name)) 418 elif (vartype == 'n/y'): 419 if (default == None): 420 default = 'n' 421 defaults[varname] = default 422 options.append(" <%s> %s " % (yes_no(default), name)) 423 else: 424 raise RuntimeError("Unknown variable type: %s" % vartype) 507 value = config[varname] 508 509 if not rule_value_is_valid(rule, value): 510 value = None 511 512 default = rule_get_default(rule) 513 if default != None: 514 value = default 515 config[varname] = default 516 517 option = rule_get_option(rule, value) 518 if option != None: 519 options.append(option) 425 520 426 521 opt2row[cnt] = (varname, vartype, name, choices) … … 433 528 (button, value) = xtui.choice_window(screen, 'HelenOS configuration', 'Choose configuration option', options, position) 434 529 435 if (button == 'cancel'):530 if button == 'cancel': 436 531 return 'Configuration canceled' 437 532 438 if (button == 'done'):439 if ( check_choices(defaults, ask_names)):533 if button == 'done': 534 if (infer_verify_choices(config, rules)): 440 535 break 441 536 else: … … 443 538 continue 444 539 445 if (value == 0):446 read_preconfigured(PRECONF, MAKEFILE, screen, defaults)540 if value == 0: 541 load_presets(PRESETS_DIR, MAKEFILE, screen, config) 447 542 position = 1 448 543 continue 449 544 450 545 position = None 451 if (not value in opt2row):546 if not value in opt2row: 452 547 raise RuntimeError("Error selecting value: %s" % value) 453 548 454 549 (selname, seltype, name, choices) = opt2row[value] 455 550 456 if (not selname in defaults):457 default= None551 if not selname in config: 552 value = None 458 553 else: 459 default = defaults[selname]460 461 if (seltype == 'choice'):462 defaults[selname] = subchoice(screen, name, choices, default)463 elif ( (seltype == 'y/n') or (seltype == 'n/y')):464 if (defaults[selname] == 'y'):465 defaults[selname] = 'n'554 value = config[selname] 555 556 if seltype == 'choice': 557 config[selname] = subchoice(screen, name, choices, value) 558 elif (seltype == 'y/n') or (seltype == 'n/y'): 559 if config[selname] == 'y': 560 config[selname] = 'n' 466 561 else: 467 defaults[selname] = 'y'562 config[selname] = 'y' 468 563 finally: 469 564 xtui.screen_done(screen) 470 565 471 create_output(MAKEFILE, MACROS, defaults, ask_names)566 create_output(MAKEFILE, MACROS, config, rules) 472 567 return 0 473 568
Note:
See TracChangeset
for help on using the changeset viewer.