Changes in tools/config.py [f857e8b:571239a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/config.py
rf857e8b r571239a 40 40 import subprocess 41 41 import xtui 42 import random43 42 44 43 RULES_FILE = sys.argv[1] … … 257 256 258 257 return True 259 260 ## Fill the configuration with random (but valid) values.261 #262 # The random selection takes next rule and if the condition does263 # not violate existing configuration, random value of the variable264 # 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 the267 # variable or ignoring the variable altogether.268 #269 # @param config Configuration to work on270 # @param rules Rules271 # @param start_index With which rule to start (initial call must specify 0 here).272 # @return True if able to find a valid configuration273 def random_choices(config, rules, start_index):274 "Fill the configuration with random (but valid) values."275 if start_index >= len(rules):276 return True277 278 varname, vartype, name, choices, cond = rules[start_index]279 280 # First check that this rule would make sense281 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 backtracking286 yes_no = 0287 choices_indexes = range(0, len(choices))288 random.shuffle(choices_indexes)289 290 # Remember current configuration value291 old_value = None292 try:293 old_value = config[varname]294 except KeyError:295 old_value = None296 297 # For yes/no choices, we ran the loop at most 2 times, for select298 # choices as many times as there are options.299 try_counter = 0300 while True:301 if vartype == 'choice':302 if try_counter >= len(choices_indexes):303 break304 value = choices[choices_indexes[try_counter]][0]305 elif vartype == 'y' or vartype == 'n':306 if try_counter > 0:307 break308 value = vartype309 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_no314 else:315 break316 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] = value324 325 ok = random_choices(config, rules, start_index + 1)326 if ok:327 return True328 329 try_counter = try_counter + 1330 331 # Restore the old value and backtrack332 # (need to delete to prevent "ghost" variables that do not exist under333 # certain configurations)334 config[varname] = old_value335 if old_value is None:336 del config[varname]337 338 return random_choices(config, rules, start_index + 1)339 340 258 341 259 ## Get default value from a rule. … … 445 363 def create_output(mkname, mcname, config, rules): 446 364 "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()) 450 367 451 368 sys.stderr.write("Fetching current revision identifier ... ") … … 506 423 outmc.write('#define REVISION %s\n' % revision) 507 424 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_unix512 425 513 426 outmk.write('TIMESTAMP = %s\n' % timestamp) … … 636 549 return 0 637 550 return 1 638 639 # Random mode640 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 2645 if not infer_verify_choices(config, rules):646 sys.stderr.write("Internal error: random configuration not consistent.\n")647 return 2648 preprocess_config(config, rules)649 create_output(MAKEFILE, MACROS, config, rules)650 651 return 0652 551 653 552 screen = xtui.screen_init()
Note:
See TracChangeset
for help on using the changeset viewer.