Changes in tools/config.py [e3c3172:28f4adb] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/config.py
re3c3172 r28f4adb 3 3 # Copyright (c) 2006 Ondrej Palkovsky 4 4 # Copyright (c) 2009 Martin Decky 5 # Copyright (c) 2010 Jiri Svoboda6 5 # All rights reserved. 7 6 # … … 41 40 import xtui 42 41 43 RULES_FILE= sys.argv[1]42 INPUT = sys.argv[1] 44 43 MAKEFILE = 'Makefile.config' 45 44 MACROS = 'config.h' 46 PRE SETS_DIR= 'defaults'47 48 def read_ config(fname, config):49 "Read saved values from last configuration run or a preset file"45 PRECONF = 'defaults' 46 47 def read_defaults(fname, defaults): 48 "Read saved values from last configuration run" 50 49 51 50 inf = open(fname, 'r') … … 53 52 for line in inf: 54 53 res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line) 55 if res:56 config[res.group(1)] = res.group(2)54 if (res): 55 defaults[res.group(1)] = res.group(2) 57 56 58 57 inf.close() 59 58 60 def check_condition(text, config, rules):59 def check_condition(text, defaults, ask_names): 61 60 "Check that the condition specified on input line is True (only CNF and DNF is supported)" 62 61 63 62 ctype = 'cnf' 64 63 65 if ( ')|' in text) or ('|(' in text):64 if ((')|' in text) or ('|(' in text)): 66 65 ctype = 'dnf' 67 66 68 if ctype == 'cnf':67 if (ctype == 'cnf'): 69 68 conds = text.split('&') 70 69 else: … … 72 71 73 72 for cond in conds: 74 if cond.startswith('(') and cond.endswith(')'):73 if (cond.startswith('(')) and (cond.endswith(')')): 75 74 cond = cond[1:-1] 76 75 77 inside = check_inside(cond, config, ctype)76 inside = check_inside(cond, defaults, ctype) 78 77 79 78 if (ctype == 'cnf') and (not inside): 80 79 return False 81 80 82 if (ctype == 'dnf') and inside:81 if (ctype == 'dnf') and (inside): 83 82 return True 84 83 85 if ctype == 'cnf':84 if (ctype == 'cnf'): 86 85 return True 87 86 return False 88 87 89 def check_inside(text, config, ctype):88 def check_inside(text, defaults, ctype): 90 89 "Check for condition" 91 90 92 if ctype == 'cnf':91 if (ctype == 'cnf'): 93 92 conds = text.split('|') 94 93 else: … … 97 96 for cond in conds: 98 97 res = re.match(r'^(.*?)(!?=)(.*)$', cond) 99 if not res:98 if (not res): 100 99 raise RuntimeError("Invalid condition: %s" % cond) 101 100 … … 104 103 condval = res.group(3) 105 104 106 if not condname in config:105 if (not condname in defaults): 107 106 varval = '' 108 107 else: 109 varval = config[condname]108 varval = defaults[condname] 110 109 if (varval == '*'): 111 110 varval = 'y' 112 111 113 if ctype == 'cnf':112 if (ctype == 'cnf'): 114 113 if (oper == '=') and (condval == varval): 115 114 return True … … 124 123 return False 125 124 126 if ctype == 'cnf':125 if (ctype == 'cnf'): 127 126 return False 128 127 129 128 return True 130 129 131 def parse_ rules(fname, rules):132 "Parse rulesfile"130 def parse_config(fname, ask_names): 131 "Parse configuration file" 133 132 134 133 inf = open(fname, 'r') … … 139 138 for line in inf: 140 139 141 if line.startswith('!'):140 if (line.startswith('!')): 142 141 # Ask a question 143 142 res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line) 144 143 145 if not res:144 if (not res): 146 145 raise RuntimeError("Weird line: %s" % line) 147 146 … … 150 149 vartype = res.group(3) 151 150 152 rules.append((varname, vartype, name, choices, cond))151 ask_names.append((varname, vartype, name, choices, cond)) 153 152 name = '' 154 153 choices = [] 155 154 continue 156 155 157 if line.startswith('@'):156 if (line.startswith('@')): 158 157 # Add new line into the 'choices' array 159 158 res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line) … … 165 164 continue 166 165 167 if line.startswith('%'):166 if (line.startswith('%')): 168 167 # Name of the option 169 168 name = line[1:].strip() 170 169 continue 171 170 172 if line.startswith('#') or (line == '\n'):171 if ((line.startswith('#')) or (line == '\n')): 173 172 # Comment or empty line 174 173 continue … … 182 181 "Return '*' if yes, ' ' if no" 183 182 184 if default == 'y':183 if (default == 'y'): 185 184 return '*' 186 185 … … 200 199 cnt = 0 201 200 for key, val in choices: 202 if ( default) and (key == default):201 if ((default) and (key == default)): 203 202 position = cnt 204 203 … … 208 207 (button, value) = xtui.choice_window(screen, name, 'Choose value', options, position) 209 208 210 if button == 'cancel':209 if (button == 'cancel'): 211 210 return None 212 211 213 212 return choices[value][0] 214 213 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)): 233 continue 234 235 if not varname in config: 236 value = None 237 else: 238 value = config[varname] 239 240 if not validate_rule_value(rule, value): 241 value = None 242 243 default = get_default_rule(rule) 244 if default != None: 245 config[varname] = default 246 247 if not varname in config: 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))): 219 continue 220 221 if (not varname in defaults): 248 222 return False 249 223 250 224 return True 251 225 252 ## Get default value from a rule. 253 def get_default_rule(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 get_rule_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 validate_rule_value(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): 226 def create_output(mkname, mcname, defaults, ask_names): 341 227 "Create output configuration" 342 228 … … 352 238 sys.stderr.write("failed\n") 353 239 354 if len(version) == 3:240 if (len(version) == 3): 355 241 revision = version[1] 356 if version[0] != 1:242 if (version[0] != 1): 357 243 revision += 'M' 358 244 revision += ' (%s)' % version[2] … … 373 259 defs = 'CONFIG_DEFS =' 374 260 375 for varname, vartype, name, choices, cond in rules:376 if cond and (not check_condition(cond, config, rules)):377 continue 378 379 if not varname in config:380 value= ''261 for varname, vartype, name, choices, cond in ask_names: 262 if ((cond) and (not check_condition(cond, defaults, ask_names))): 263 continue 264 265 if (not varname in defaults): 266 default = '' 381 267 else: 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":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"): 390 276 outmc.write('/* %s */\n#define %s\n\n' % (name, varname)) 391 277 defs += ' -D%s' % varname 392 278 else: 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: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): 397 283 outmk.write('REVISION = %s\n' % revision) 398 284 outmc.write('#define REVISION %s\n' % revision) … … 413 299 return list 414 300 415 ## Ask user to choose a configuration profile. 416 # 417 def choose_profile(root, fname, screen, config): 301 def read_preconfigured(root, fname, screen, defaults): 418 302 options = [] 419 303 opt2path = {} … … 425 309 canon = os.path.join(path, fname) 426 310 427 if os.path.isdir(path) and os.path.exists(canon) and os.path.isfile(canon):311 if ((os.path.isdir(path)) and (os.path.exists(canon)) and (os.path.isfile(canon))): 428 312 subprofile = False 429 313 … … 433 317 subcanon = os.path.join(subpath, fname) 434 318 435 if os.path.isdir(subpath) and os.path.exists(subcanon) and os.path.isfile(subcanon):319 if ((os.path.isdir(subpath)) and (os.path.exists(subcanon)) and (os.path.isfile(subcanon))): 436 320 subprofile = True 437 321 options.append("%s (%s)" % (name, subname)) 438 opt2path[cnt] = [name, subname]322 opt2path[cnt] = (canon, subcanon) 439 323 cnt += 1 440 324 441 if not subprofile:325 if (not subprofile): 442 326 options.append(name) 443 opt2path[cnt] = [name]327 opt2path[cnt] = (canon, None) 444 328 cnt += 1 445 329 446 330 (button, value) = xtui.choice_window(screen, 'Load preconfigured defaults', 'Choose configuration profile', options, None) 447 331 448 if button == 'cancel':332 if (button == 'cancel'): 449 333 return None 450 334 451 return opt2path[value] 452 453 ## Read presets from a configuration profile. 454 # 455 # @param profile Profile to load from (a list of string components) 456 # @param config Output configuration 457 # 458 def read_presets(profile, config): 459 path = os.path.join(PRESETS_DIR, profile[0], MAKEFILE) 460 read_config(path, config) 461 462 if len(profile) > 1: 463 path = os.path.join(PRESETS_DIR, profile[0], profile[1], MAKEFILE) 464 read_config(path, config) 465 466 ## Parse profile name (relative OS path) into a list of components. 467 # 468 # @param profile_name Relative path (using OS separator) 469 # @return List of components 470 # 471 def parse_profile_name(profile_name): 472 profile = [] 473 474 head, tail = os.path.split(profile_name) 475 if head != '': 476 profile.append(head) 477 478 profile.append(tail) 479 return profile 335 read_defaults(opt2path[value][0], defaults) 336 if (opt2path[value][1] != None): 337 read_defaults(opt2path[value][1], defaults) 480 338 481 339 def main(): 482 profile = None 483 config = {} 484 rules = [] 485 486 # Parse rules file 487 parse_rules(RULES_FILE, rules) 488 489 # Input configuration file can be specified on command line 490 # otherwise configuration from previous run is used. 491 if len(sys.argv) >= 4: 492 profile = parse_profile_name(sys.argv[3]) 493 read_presets(profile, config) 494 elif os.path.exists(MAKEFILE): 495 read_config(MAKEFILE, config) 496 497 # Default mode: check values and regenerate configuration files 498 if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'): 499 if (infer_verify_choices(config, rules)): 500 create_output(MAKEFILE, MACROS, config, rules) 340 defaults = {} 341 ask_names = [] 342 343 # Parse configuration file 344 parse_config(INPUT, ask_names) 345 346 # Read defaults from previous run 347 if os.path.exists(MAKEFILE): 348 read_defaults(MAKEFILE, defaults) 349 350 # Default mode: only check defaults and regenerate configuration 351 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) 501 354 return 0 502 355 503 # Hands-off mode: check values and regenerate configuration files, 504 # but no interactive fallback 505 if (len(sys.argv) >= 3) and (sys.argv[2] == 'hands-off'): 506 # We deliberately test sys.argv >= 4 because we do not want 507 # to read implicitly any possible previous run configuration 508 if len(sys.argv) < 4: 509 sys.stderr.write("Configuration error: No presets specified\n") 510 return 2 511 512 if (infer_verify_choices(config, rules)): 513 create_output(MAKEFILE, MACROS, config, rules) 514 return 0 515 516 sys.stderr.write("Configuration error: The presets are ambiguous\n") 517 return 1 518 519 # Check mode: only check configuration 520 if (len(sys.argv) >= 3) and (sys.argv[2] == 'check'): 521 if infer_verify_choices(config, rules): 356 # Check mode: only check defaults 357 if ((len(sys.argv) >= 3) and (sys.argv[2] == 'check')): 358 if (check_choices(defaults, ask_names)): 522 359 return 0 523 360 return 1 … … 529 366 while True: 530 367 531 # Cancel out all values which have to be deduced532 for varname, vartype, name, choices, cond in rules:533 if ( vartype == 'y') and (varname in config) and (config[varname] == '*'):534 config[varname] = None368 # Cancel out all defaults which have to be deduced 369 for varname, vartype, name, choices, cond in ask_names: 370 if ((vartype == 'y') and (varname in defaults) and (defaults[varname] == '*')): 371 defaults[varname] = None 535 372 536 373 options = [] … … 540 377 options.append(" --- Load preconfigured defaults ... ") 541 378 542 for rule in rules: 543 varname, vartype, name, choices, cond = rule 544 545 if cond and (not check_condition(cond, config, rules)): 379 for varname, vartype, name, choices, cond in ask_names: 380 381 if ((cond) and (not check_condition(cond, defaults, ask_names))): 546 382 continue 547 383 548 if varname == selname:384 if (varname == selname): 549 385 position = cnt 550 386 551 if not varname in config:552 value= None387 if (not varname in defaults): 388 default = None 553 389 else: 554 value = config[varname] 555 556 if not validate_rule_value(rule, value): 557 value = None 558 559 default = get_default_rule(rule) 560 if default != None: 561 if value == None: 562 value = default 563 config[varname] = value 564 565 option = get_rule_option(rule, value) 566 if option != None: 567 options.append(option) 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)) 568 423 else: 569 continue424 raise RuntimeError("Unknown variable type: %s" % vartype) 570 425 571 426 opt2row[cnt] = (varname, vartype, name, choices) … … 578 433 (button, value) = xtui.choice_window(screen, 'HelenOS configuration', 'Choose configuration option', options, position) 579 434 580 if button == 'cancel':435 if (button == 'cancel'): 581 436 return 'Configuration canceled' 582 437 583 if button == 'done':584 if ( infer_verify_choices(config, rules)):438 if (button == 'done'): 439 if (check_choices(defaults, ask_names)): 585 440 break 586 441 else: … … 588 443 continue 589 444 590 if value == 0: 591 profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config) 592 if profile != None: 593 read_presets(profile, config) 445 if (value == 0): 446 read_preconfigured(PRECONF, MAKEFILE, screen, defaults) 594 447 position = 1 595 448 continue 596 449 597 450 position = None 598 if not value in opt2row:451 if (not value in opt2row): 599 452 raise RuntimeError("Error selecting value: %s" % value) 600 453 601 454 (selname, seltype, name, choices) = opt2row[value] 602 455 603 if not selname in config:604 value= None456 if (not selname in defaults): 457 default = None 605 458 else: 606 value = config[selname]607 608 if seltype == 'choice':609 config[selname] = subchoice(screen, name, choices, value)610 elif ( seltype == 'y/n') or (seltype == 'n/y'):611 if config[selname] == 'y':612 config[selname] = 'n'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' 613 466 else: 614 config[selname] = 'y'467 defaults[selname] = 'y' 615 468 finally: 616 469 xtui.screen_done(screen) 617 470 618 create_output(MAKEFILE, MACROS, config, rules)471 create_output(MAKEFILE, MACROS, defaults, ask_names) 619 472 return 0 620 473
Note:
See TracChangeset
for help on using the changeset viewer.