Changes in tools/config.py [c01f8e6:ba8de9c3] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/config.py

    rc01f8e6 rba8de9c3  
    4747
    4848def read_config(fname, config):
    49         "Read saved values from last configuration run or a preset file"
     49        "Read saved values from last configuration run"
    5050       
    5151        inf = open(fname, 'r')
     
    218218# and verify that all variables have a value (previously specified or inferred).
    219219#
    220 # @param config Configuration to work on
    221 # @param rules  Rules
    222 #
    223 # @return True if configuration is complete and valid, False
    224 #         otherwise.
     220# @param config Configuration to work on
     221# @param rules  Rules
     222#
     223# @return       True if configuration is complete and valid, False
     224#               otherwise.
    225225#
    226226def infer_verify_choices(config, rules):
     
    229229        for rule in rules:
    230230                varname, vartype, name, choices, cond = rule
    231                
     231
    232232                if cond and (not check_condition(cond, config, rules)):
    233233                        continue
     
    237237                else:
    238238                        value = config[varname]
    239                
    240                 if not validate_rule_value(rule, value):
     239
     240                if not rule_value_is_valid(rule, value):
    241241                        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:
    251245                        config[varname] = default
    252                
     246
    253247                if not varname in config:
    254248                        return False
     
    257251
    258252## Get default value from a rule.
    259 def get_default_rule(rule):
     253def rule_get_default(rule):
    260254        varname, vartype, name, choices, cond = rule
    261        
     255
    262256        default = None
    263        
     257
    264258        if vartype == 'choice':
    265259                # If there is just one option, use it
     
    276270        else:
    277271                raise RuntimeError("Unknown variable type: %s" % vartype)
    278        
     272
    279273        return default
    280274
    281275## Get option from a rule.
    282276#
    283 # @param rule  Rule for a variable
    284 # @param value Current value of the variable
     277# @param rule   Rule for a variable
     278# @param value  Current value of the variable
    285279#
    286280# @return Option (string) to ask or None which means not to ask.
    287281#
    288 def get_rule_option(rule, value):
     282def rule_get_option(rule, value):
    289283        varname, vartype, name, choices, cond = rule
    290        
     284
    291285        option = None
    292        
     286
    293287        if vartype == 'choice':
    294288                # If there is just one option, don't ask
     
    308302        else:
    309303                raise RuntimeError("Unknown variable type: %s" % vartype)
    310        
     304
    311305        return option
    312306
    313307## Check if variable value is valid.
    314308#
    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):
     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#
     314def rule_value_is_valid(rule, value):
    321315        varname, vartype, name, choices, cond = rule
    322316       
    323317        if value == None:
    324318                return True
    325        
     319
    326320        if vartype == 'choice':
    327321                if not value in [choice[0] for choice in choices]:
     
    341335        else:
    342336                raise RuntimeError("Unknown variable type: %s" % vartype)
    343        
     337
    344338        return True
    345339
     
    419413        return list
    420414
    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#
     417def load_presets(root, fname, screen, config):
    424418        options = []
    425419        opt2path = {}
     
    442436                                        subprofile = True
    443437                                        options.append("%s (%s)" % (name, subname))
    444                                         opt2path[cnt] = [name, subname]
     438                                        opt2path[cnt] = (canon, subcanon)
    445439                                        cnt += 1
    446440                       
    447441                        if not subprofile:
    448442                                options.append(name)
    449                                 opt2path[cnt] = [name]
     443                                opt2path[cnt] = (canon, None)
    450444                                cnt += 1
    451445       
     
    455449                return None
    456450       
    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)
    486454
    487455def main():
    488         profile = None
    489456        config = {}
    490457        rules = []
     
    493460        parse_rules(RULES_FILE, rules)
    494461       
    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):
    501464                read_config(MAKEFILE, config)
    502465       
    503         # Default mode: check values and regenerate configuration files
     466        # Default mode: only check values and regenerate configuration files
    504467        if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'):
    505468                if (infer_verify_choices(config, rules)):
    506469                        create_output(MAKEFILE, MACROS, config, rules)
    507470                        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
    524471       
    525472        # Check mode: only check configuration
     
    560507                                        value = config[varname]
    561508                               
    562                                 if not validate_rule_value(rule, value):
     509                                if not rule_value_is_valid(rule, value):
    563510                                        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:
    572514                                        value = default
    573515                                        config[varname] = default
    574                                
    575                                 option = get_rule_option(rule, value)
     516
     517                                option = rule_get_option(rule, value)
    576518                                if option != None:
    577519                                        options.append(option)
    578                                 else:
    579                                         continue
    580520                               
    581521                                opt2row[cnt] = (varname, vartype, name, choices)
     
    599539                       
    600540                        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)
    604542                                position = 1
    605543                                continue
Note: See TracChangeset for help on using the changeset viewer.