Changeset fe2333d in mainline for tools/config.py
- Timestamp:
- 2010-12-09T00:08:57Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0a5a950
- Parents:
- 07b9203e (diff), c01f8e6 (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
r07b9203e rfe2333d 47 47 48 48 def read_config(fname, config): 49 "Read saved values from last configuration run "49 "Read saved values from last configuration run or a preset file" 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 rule_value_is_valid(rule, value):239 240 if not validate_rule_value(rule, value): 241 241 value = None 242 243 default = rule_get_default(rule) 244 if default != 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 245 251 config[varname] = default 246 252 247 253 if not varname in config: 248 254 return False … … 251 257 252 258 ## Get default value from a rule. 253 def rule_get_default(rule):259 def get_default_rule(rule): 254 260 varname, vartype, name, choices, cond = rule 255 261 256 262 default = None 257 263 258 264 if vartype == 'choice': 259 265 # If there is just one option, use it … … 270 276 else: 271 277 raise RuntimeError("Unknown variable type: %s" % vartype) 272 278 273 279 return default 274 280 275 281 ## Get option from a rule. 276 282 # 277 # @param rule 278 # @param value 283 # @param rule Rule for a variable 284 # @param value Current value of the variable 279 285 # 280 286 # @return Option (string) to ask or None which means not to ask. 281 287 # 282 def rule_get_option(rule, value):288 def get_rule_option(rule, value): 283 289 varname, vartype, name, choices, cond = rule 284 290 285 291 option = None 286 292 287 293 if vartype == 'choice': 288 294 # If there is just one option, don't ask … … 302 308 else: 303 309 raise RuntimeError("Unknown variable type: %s" % vartype) 304 310 305 311 return option 306 312 307 313 ## Check if variable value is valid. 308 314 # 309 # @param rule 310 # @param value 311 # 312 # @return 313 # 314 def rule_value_is_valid(rule, value):315 # @param rule Rule for the variable 316 # @param value Value of the variable 317 # 318 # @return True if valid, False if not valid. 319 # 320 def validate_rule_value(rule, value): 315 321 varname, vartype, name, choices, cond = rule 316 322 317 323 if value == None: 318 324 return True 319 325 320 326 if vartype == 'choice': 321 327 if not value in [choice[0] for choice in choices]: … … 335 341 else: 336 342 raise RuntimeError("Unknown variable type: %s" % vartype) 337 343 338 344 return True 339 345 … … 413 419 return list 414 420 415 ## Choose a profile and load configuration presets.416 # 417 def load_presets(root, fname, screen, config):421 ## Ask user to choose a configuration profile. 422 # 423 def choose_profile(root, fname, screen, config): 418 424 options = [] 419 425 opt2path = {} … … 436 442 subprofile = True 437 443 options.append("%s (%s)" % (name, subname)) 438 opt2path[cnt] = (canon, subcanon)444 opt2path[cnt] = [name, subname] 439 445 cnt += 1 440 446 441 447 if not subprofile: 442 448 options.append(name) 443 opt2path[cnt] = (canon, None)449 opt2path[cnt] = [name] 444 450 cnt += 1 445 451 … … 449 455 return None 450 456 451 read_config(opt2path[value][0], config) 452 if opt2path[value][1] != None: 453 read_config(opt2path[value][1], config) 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 454 486 455 487 def main(): 488 profile = None 456 489 config = {} 457 490 rules = [] … … 460 493 parse_rules(RULES_FILE, rules) 461 494 462 # Read configuration from previous run 463 if os.path.exists(MAKEFILE): 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): 464 501 read_config(MAKEFILE, config) 465 502 466 # Default mode: onlycheck values and regenerate configuration files503 # Default mode: check values and regenerate configuration files 467 504 if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'): 468 505 if (infer_verify_choices(config, rules)): 469 506 create_output(MAKEFILE, MACROS, config, rules) 470 507 return 0 508 509 # Hands-off mode: check values and regenerate configuration files, 510 # but no interactive fallback 511 if (len(sys.argv) >= 3) and (sys.argv[2] == 'hands-off'): 512 # We deliberately test sys.argv >= 4 because we do not want 513 # to read implicitly any possible previous run configuration 514 if len(sys.argv) < 4: 515 sys.stderr.write("Configuration error: No presets specified\n") 516 return 2 517 518 if (infer_verify_choices(config, rules)): 519 create_output(MAKEFILE, MACROS, config, rules) 520 return 0 521 522 sys.stderr.write("Configuration error: The presets are ambiguous\n") 523 return 1 471 524 472 525 # Check mode: only check configuration … … 507 560 value = config[varname] 508 561 509 if not rule_value_is_valid(rule, value):562 if not validate_rule_value(rule, value): 510 563 value = None 511 512 default = rule_get_default(rule) 513 if default != 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: 514 572 value = default 515 573 config[varname] = default 516 517 option = rule_get_option(rule, value)574 575 option = get_rule_option(rule, value) 518 576 if option != None: 519 577 options.append(option) 578 else: 579 continue 520 580 521 581 opt2row[cnt] = (varname, vartype, name, choices) … … 539 599 540 600 if value == 0: 541 load_presets(PRESETS_DIR, MAKEFILE, screen, config) 601 profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config) 602 if profile != None: 603 read_presets(profile, config) 542 604 position = 1 543 605 continue
Note:
See TracChangeset
for help on using the changeset viewer.