Changes in tools/config.py [c01f8e6:ba8de9c3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/config.py
rc01f8e6 rba8de9c3 47 47 48 48 def read_config(fname, config): 49 "Read saved values from last configuration run or a preset file"49 "Read saved values from last configuration run" 50 50 51 51 inf = open(fname, 'r') … … 218 218 # and verify that all variables have a value (previously specified or inferred). 219 219 # 220 # @param config 221 # @param rules 222 # 223 # @return 224 # 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 225 # 226 226 def infer_verify_choices(config, rules): … … 229 229 for rule in rules: 230 230 varname, vartype, name, choices, cond = rule 231 231 232 232 if cond and (not check_condition(cond, config, rules)): 233 233 continue … … 237 237 else: 238 238 value = config[varname] 239 240 if not validate_rule_value(rule, value):239 240 if not rule_value_is_valid(rule, value): 241 241 value = None 242 243 default = get_default_rule(rule) 244 245 # 246 # If we don't have a value but we do have 247 # a default, use it. 248 # 249 if value == None and default != None: 250 value = default 242 243 default = rule_get_default(rule) 244 if default != None: 251 245 config[varname] = default 252 246 253 247 if not varname in config: 254 248 return False … … 257 251 258 252 ## Get default value from a rule. 259 def get_default_rule(rule):253 def rule_get_default(rule): 260 254 varname, vartype, name, choices, cond = rule 261 255 262 256 default = None 263 257 264 258 if vartype == 'choice': 265 259 # If there is just one option, use it … … 276 270 else: 277 271 raise RuntimeError("Unknown variable type: %s" % vartype) 278 272 279 273 return default 280 274 281 275 ## Get option from a rule. 282 276 # 283 # @param rule 284 # @param value 277 # @param rule Rule for a variable 278 # @param value Current value of the variable 285 279 # 286 280 # @return Option (string) to ask or None which means not to ask. 287 281 # 288 def get_rule_option(rule, value):282 def rule_get_option(rule, value): 289 283 varname, vartype, name, choices, cond = rule 290 284 291 285 option = None 292 286 293 287 if vartype == 'choice': 294 288 # If there is just one option, don't ask … … 308 302 else: 309 303 raise RuntimeError("Unknown variable type: %s" % vartype) 310 304 311 305 return option 312 306 313 307 ## Check if variable value is valid. 314 308 # 315 # @param rule 316 # @param value 317 # 318 # @return 319 # 320 def validate_rule_value(rule, value):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): 321 315 varname, vartype, name, choices, cond = rule 322 316 323 317 if value == None: 324 318 return True 325 319 326 320 if vartype == 'choice': 327 321 if not value in [choice[0] for choice in choices]: … … 341 335 else: 342 336 raise RuntimeError("Unknown variable type: %s" % vartype) 343 337 344 338 return True 345 339 … … 419 413 return list 420 414 421 ## Ask user to choose a configuration profile.422 # 423 def choose_profile(root, fname, screen, config):415 ## Choose a profile and load configuration presets. 416 # 417 def load_presets(root, fname, screen, config): 424 418 options = [] 425 419 opt2path = {} … … 442 436 subprofile = True 443 437 options.append("%s (%s)" % (name, subname)) 444 opt2path[cnt] = [name, subname]438 opt2path[cnt] = (canon, subcanon) 445 439 cnt += 1 446 440 447 441 if not subprofile: 448 442 options.append(name) 449 opt2path[cnt] = [name]443 opt2path[cnt] = (canon, None) 450 444 cnt += 1 451 445 … … 455 449 return None 456 450 457 return opt2path[value] 458 459 ## Read presets from a configuration profile. 460 # 461 # @param profile Profile to load from (a list of string components) 462 # @param config Output configuration 463 # 464 def read_presets(profile, config): 465 path = os.path.join(PRESETS_DIR, profile[0], MAKEFILE) 466 read_config(path, config) 467 468 if len(profile) > 1: 469 path = os.path.join(PRESETS_DIR, profile[0], profile[1], MAKEFILE) 470 read_config(path, config) 471 472 ## Parse profile name (relative OS path) into a list of components. 473 # 474 # @param profile_name Relative path (using OS separator) 475 # @return List of components 476 # 477 def parse_profile_name(profile_name): 478 profile = [] 479 480 head, tail = os.path.split(profile_name) 481 if head != '': 482 profile.append(head) 483 484 profile.append(tail) 485 return profile 451 read_config(opt2path[value][0], config) 452 if opt2path[value][1] != None: 453 read_config(opt2path[value][1], config) 486 454 487 455 def main(): 488 profile = None489 456 config = {} 490 457 rules = [] … … 493 460 parse_rules(RULES_FILE, rules) 494 461 495 # Input configuration file can be specified on command line 496 # otherwise configuration from previous run is used. 497 if len(sys.argv) >= 4: 498 profile = parse_profile_name(sys.argv[3]) 499 read_presets(profile, config) 500 elif os.path.exists(MAKEFILE): 462 # Read configuration from previous run 463 if os.path.exists(MAKEFILE): 501 464 read_config(MAKEFILE, config) 502 465 503 # Default mode: check values and regenerate configuration files466 # Default mode: only check values and regenerate configuration files 504 467 if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'): 505 468 if (infer_verify_choices(config, rules)): 506 469 create_output(MAKEFILE, MACROS, config, rules) 507 470 return 0 508 509 # Hands-off mode: check values and regenerate configuration files,510 # but no interactive fallback511 if (len(sys.argv) >= 3) and (sys.argv[2] == 'hands-off'):512 # We deliberately test sys.argv >= 4 because we do not want513 # to read implicitly any possible previous run configuration514 if len(sys.argv) < 4:515 sys.stderr.write("Configuration error: No presets specified\n")516 return 2517 518 if (infer_verify_choices(config, rules)):519 create_output(MAKEFILE, MACROS, config, rules)520 return 0521 522 sys.stderr.write("Configuration error: The presets are ambiguous\n")523 return 1524 471 525 472 # Check mode: only check configuration … … 560 507 value = config[varname] 561 508 562 if not validate_rule_value(rule, value):509 if not rule_value_is_valid(rule, value): 563 510 value = None 564 565 default = get_default_rule(rule) 566 567 # 568 # If we don't have a value but we do have 569 # a default, use it. 570 # 571 if value == None and default != None: 511 512 default = rule_get_default(rule) 513 if default != None: 572 514 value = default 573 515 config[varname] = default 574 575 option = get_rule_option(rule, value)516 517 option = rule_get_option(rule, value) 576 518 if option != None: 577 519 options.append(option) 578 else:579 continue580 520 581 521 opt2row[cnt] = (varname, vartype, name, choices) … … 599 539 600 540 if value == 0: 601 profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config) 602 if profile != None: 603 read_presets(profile, config) 541 load_presets(PRESETS_DIR, MAKEFILE, screen, config) 604 542 position = 1 605 543 continue
Note:
See TracChangeset
for help on using the changeset viewer.