Changeset 7964475 in mainline for tools/config.py


Ignore:
Timestamp:
2010-12-10T15:08:26Z (13 years ago)
Author:
Matus Dekanek <smekideki@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c01255c
Parents:
b5ec347 (diff), cdc1aa1 (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.
Message:

merge with development/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/config.py

    rb5ec347 r7964475  
    4747
    4848def read_config(fname, config):
    49         "Read saved values from last configuration run"
     49        "Read saved values from last configuration run or a preset file"
    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 rule_value_is_valid(rule, value):
     239               
     240                if not validate_rule_value(rule, value):
    241241                        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
    245251                        config[varname] = default
    246 
     252               
    247253                if not varname in config:
    248254                        return False
     
    251257
    252258## Get default value from a rule.
    253 def rule_get_default(rule):
     259def get_default_rule(rule):
    254260        varname, vartype, name, choices, cond = rule
    255 
     261       
    256262        default = None
    257 
     263       
    258264        if vartype == 'choice':
    259265                # If there is just one option, use it
     
    270276        else:
    271277                raise RuntimeError("Unknown variable type: %s" % vartype)
    272 
     278       
    273279        return default
    274280
    275281## Get option from a rule.
    276282#
    277 # @param rule   Rule for a variable
    278 # @param value  Current value of the variable
     283# @param rule  Rule for a variable
     284# @param value Current value of the variable
    279285#
    280286# @return Option (string) to ask or None which means not to ask.
    281287#
    282 def rule_get_option(rule, value):
     288def get_rule_option(rule, value):
    283289        varname, vartype, name, choices, cond = rule
    284 
     290       
    285291        option = None
    286 
     292       
    287293        if vartype == 'choice':
    288294                # If there is just one option, don't ask
     
    302308        else:
    303309                raise RuntimeError("Unknown variable type: %s" % vartype)
    304 
     310       
    305311        return option
    306312
    307313## Check if variable value is valid.
    308314#
    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# @param rule  Rule for the variable
     316# @param value Value of the variable
     317#
     318# @return True if valid, False if not valid.
     319#
     320def validate_rule_value(rule, value):
    315321        varname, vartype, name, choices, cond = rule
    316322       
    317323        if value == None:
    318324                return True
    319 
     325       
    320326        if vartype == 'choice':
    321327                if not value in [choice[0] for choice in choices]:
     
    335341        else:
    336342                raise RuntimeError("Unknown variable type: %s" % vartype)
    337 
     343       
    338344        return True
    339345
     
    413419        return list
    414420
    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#
     423def choose_profile(root, fname, screen, config):
    418424        options = []
    419425        opt2path = {}
     
    436442                                        subprofile = True
    437443                                        options.append("%s (%s)" % (name, subname))
    438                                         opt2path[cnt] = (canon, subcanon)
     444                                        opt2path[cnt] = [name, subname]
    439445                                        cnt += 1
    440446                       
    441447                        if not subprofile:
    442448                                options.append(name)
    443                                 opt2path[cnt] = (canon, None)
     449                                opt2path[cnt] = [name]
    444450                                cnt += 1
    445451       
     
    449455                return None
    450456       
    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#
     464def 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#
     477def 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
    454486
    455487def main():
     488        profile = None
    456489        config = {}
    457490        rules = []
     
    460493        parse_rules(RULES_FILE, rules)
    461494       
    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):
    464501                read_config(MAKEFILE, config)
    465502       
    466         # Default mode: only check values and regenerate configuration files
     503        # Default mode: check values and regenerate configuration files
    467504        if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'):
    468505                if (infer_verify_choices(config, rules)):
    469506                        create_output(MAKEFILE, MACROS, config, rules)
    470507                        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
    471524       
    472525        # Check mode: only check configuration
     
    507560                                        value = config[varname]
    508561                               
    509                                 if not rule_value_is_valid(rule, value):
     562                                if not validate_rule_value(rule, value):
    510563                                        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:
    514572                                        value = default
    515573                                        config[varname] = default
    516 
    517                                 option = rule_get_option(rule, value)
     574                               
     575                                option = get_rule_option(rule, value)
    518576                                if option != None:
    519577                                        options.append(option)
     578                                else:
     579                                        continue
    520580                               
    521581                                opt2row[cnt] = (varname, vartype, name, choices)
     
    539599                       
    540600                        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)
    542604                                position = 1
    543605                                continue
Note: See TracChangeset for help on using the changeset viewer.