Changes in tools/config.py [f857e8b:571239a] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/config.py

    rf857e8b r571239a  
    4040import subprocess
    4141import xtui
    42 import random
    4342
    4443RULES_FILE = sys.argv[1]
     
    257256       
    258257        return True
    259 
    260 ## Fill the configuration with random (but valid) values.
    261 #
    262 # The random selection takes next rule and if the condition does
    263 # not violate existing configuration, random value of the variable
    264 # is selected.
    265 # This happens recursively as long as there are more rules.
    266 # If a conflict is found, we backtrack and try other settings of the
    267 # variable or ignoring the variable altogether.
    268 #
    269 # @param config Configuration to work on
    270 # @param rules  Rules
    271 # @param start_index With which rule to start (initial call must specify 0 here).
    272 # @return True if able to find a valid configuration
    273 def random_choices(config, rules, start_index):
    274         "Fill the configuration with random (but valid) values."
    275         if start_index >= len(rules):
    276                 return True
    277        
    278         varname, vartype, name, choices, cond = rules[start_index]
    279 
    280         # First check that this rule would make sense   
    281         if cond:
    282                 if not check_condition(cond, config, rules):
    283                         return random_choices(config, rules, start_index + 1)
    284        
    285         # Remember previous choices for backtracking
    286         yes_no = 0
    287         choices_indexes = range(0, len(choices))
    288         random.shuffle(choices_indexes)
    289        
    290         # Remember current configuration value
    291         old_value = None
    292         try:
    293                 old_value = config[varname]
    294         except KeyError:
    295                 old_value = None
    296        
    297         # For yes/no choices, we ran the loop at most 2 times, for select
    298         # choices as many times as there are options.
    299         try_counter = 0
    300         while True:
    301                 if vartype == 'choice':
    302                         if try_counter >= len(choices_indexes):
    303                                 break
    304                         value = choices[choices_indexes[try_counter]][0]
    305                 elif vartype == 'y' or vartype == 'n':
    306                         if try_counter > 0:
    307                                 break
    308                         value = vartype
    309                 elif vartype == 'y/n' or vartype == 'n/y':
    310                         if try_counter == 0:
    311                                 yes_no = random.randint(0, 1)
    312                         elif try_counter == 1:
    313                                 yes_no = 1 - yes_no
    314                         else:
    315                                 break
    316                         if yes_no == 0:
    317                                 value = 'n'
    318                         else:
    319                                 value = 'y'
    320                 else:
    321                         raise RuntimeError("Unknown variable type: %s" % vartype)
    322        
    323                 config[varname] = value
    324                
    325                 ok = random_choices(config, rules, start_index + 1)
    326                 if ok:
    327                         return True
    328                
    329                 try_counter = try_counter + 1
    330        
    331         # Restore the old value and backtrack
    332         # (need to delete to prevent "ghost" variables that do not exist under
    333         # certain configurations)
    334         config[varname] = old_value
    335         if old_value is None:
    336                 del config[varname]
    337        
    338         return random_choices(config, rules, start_index + 1)
    339        
    340258
    341259## Get default value from a rule.
     
    445363def create_output(mkname, mcname, config, rules):
    446364        "Create output configuration"
    447 
    448         timestamp_unix = int(time.time())
    449         timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp_unix))
     365       
     366        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    450367       
    451368        sys.stderr.write("Fetching current revision identifier ... ")
     
    506423                outmc.write('#define REVISION %s\n' % revision)
    507424                defs += ' "-DREVISION=%s"' % revision
    508        
    509         outmk.write('TIMESTAMP_UNIX = %d\n' % timestamp_unix)
    510         outmc.write('#define TIMESTAMP_UNIX %d\n' % timestamp_unix)
    511         defs += ' "-DTIMESTAMP_UNIX=%d"\n' % timestamp_unix
    512425       
    513426        outmk.write('TIMESTAMP = %s\n' % timestamp)
     
    636549                        return 0
    637550                return 1
    638        
    639         # Random mode
    640         if (len(sys.argv) == 3) and (sys.argv[2] == 'random'):
    641                 ok = random_choices(config, rules, 0)
    642                 if not ok:
    643                         sys.stderr.write("Internal error: unable to generate random config.\n")
    644                         return 2
    645                 if not infer_verify_choices(config, rules):
    646                         sys.stderr.write("Internal error: random configuration not consistent.\n")
    647                         return 2
    648                 preprocess_config(config, rules)
    649                 create_output(MAKEFILE, MACROS, config, rules)
    650                
    651                 return 0       
    652551       
    653552        screen = xtui.screen_init()
Note: See TracChangeset for help on using the changeset viewer.