Example #1
0
def choose_profile(root, fname, screen, config):
	options = []
	opt2path = {}
	cnt = 0
	
	# Look for profiles
	for name in sorted_dir(root):
		path = os.path.join(root, name)
		canon = os.path.join(path, fname)
		
		if os.path.isdir(path) and os.path.exists(canon) and os.path.isfile(canon):
			subprofile = False
			
			# Look for subprofiles
			for subname in sorted_dir(path):
				subpath = os.path.join(path, subname)
				subcanon = os.path.join(subpath, fname)
				
				if os.path.isdir(subpath) and os.path.exists(subcanon) and os.path.isfile(subcanon):
					subprofile = True
					options.append("%s (%s)" % (name, subname))
					opt2path[cnt] = [name, subname]
					cnt += 1
			
			if not subprofile:
				options.append(name)
				opt2path[cnt] = [name]
				cnt += 1
	
	(button, value) = xtui.choice_window(screen, 'Load preconfigured defaults', 'Choose configuration profile', options, None)
	
	if button == 'cancel':
		return None
	
	return opt2path[value]
Example #2
0
def subchoice(screen, name, choices, default):
	"Return choice of choices"
	
	maxkey = 0
	for key, val in choices:
		length = len(key)
		if (length > maxkey):
			maxkey = length
	
	options = []
	position = None
	cnt = 0
	for key, val in choices:
		if (default) and (key == default):
			position = cnt
		
		options.append(" %-*s  %s " % (maxkey, key, val))
		cnt += 1
	
	(button, value) = xtui.choice_window(screen, name, 'Choose value', options, position)
	
	if button == 'cancel':
		return None
	
	return choices[value][0]
Example #3
0
def subchoice(screen, name, choices, default):
    "Return choice of choices"

    maxkey = 0
    for key, val in choices:
        length = len(key)
        if (length > maxkey):
            maxkey = length

    options = []
    position = None
    cnt = 0
    for key, val in choices:
        if (default) and (key == default):
            position = cnt

        options.append(" %-*s  %s " % (maxkey, key, val))
        cnt += 1

    (button, value) = xtui.choice_window(screen, name, 'Choose value', options,
                                         position)

    if button == 'cancel':
        return None

    return choices[value][0]
Example #4
0
def choose_profile(root, fname, screen, config):
    options = []
    opt2path = {}
    cnt = 0

    # Look for profiles
    for name in sorted_dir(root):
        path = os.path.join(root, name)
        canon = os.path.join(path, fname)

        if os.path.isdir(path) and os.path.exists(canon) and os.path.isfile(
                canon):
            subprofile = False

            # Look for subprofiles
            for subname in sorted_dir(path):
                subpath = os.path.join(path, subname)
                subcanon = os.path.join(subpath, fname)

                if os.path.isdir(subpath) and os.path.exists(
                        subcanon) and os.path.isfile(subcanon):
                    subprofile = True
                    options.append("%s (%s)" % (name, subname))
                    opt2path[cnt] = [name, subname]
                    cnt += 1

            if not subprofile:
                options.append(name)
                opt2path[cnt] = [name]
                cnt += 1

    (button, value) = xtui.choice_window(screen, 'Load preconfigured defaults',
                                         'Choose configuration profile',
                                         options, None)

    if button == 'cancel':
        return None

    return opt2path[value]
Example #5
0
def main():
	profile = None
	config = {}
	rules = []
	
	# Parse rules file
	parse_rules(RULES_FILE, rules)
	
	# Input configuration file can be specified on command line
	# otherwise configuration from previous run is used.
	if len(sys.argv) >= 4:
		profile = parse_profile_name(sys.argv[3])
		read_presets(profile, config)
	elif os.path.exists(MAKEFILE):
		read_config(MAKEFILE, config)
	
	# Default mode: check values and regenerate configuration files
	if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'):
		if (infer_verify_choices(config, rules)):
			preprocess_config(config, rules)
			create_output(MAKEFILE, MACROS, config, rules)
			return 0
	
	# Hands-off mode: check values and regenerate configuration files,
	# but no interactive fallback
	if (len(sys.argv) >= 3) and (sys.argv[2] == 'hands-off'):
		# We deliberately test sys.argv >= 4 because we do not want
		# to read implicitly any possible previous run configuration
		if len(sys.argv) < 4:
			sys.stderr.write("Configuration error: No presets specified\n")
			return 2
		
		if (infer_verify_choices(config, rules)):
			preprocess_config(config, rules)
			create_output(MAKEFILE, MACROS, config, rules)
			return 0
		
		sys.stderr.write("Configuration error: The presets are ambiguous\n")
		return 1
	
	# Check mode: only check configuration
	if (len(sys.argv) >= 3) and (sys.argv[2] == 'check'):
		if infer_verify_choices(config, rules):
			return 0
		return 1
	
	screen = xtui.screen_init()
	try:
		selname = None
		position = None
		while True:
			
			# Cancel out all values which have to be deduced
			for varname, vartype, name, choices, cond in rules:
				if (vartype == 'y') and (varname in config) and (config[varname] == '*'):
					config[varname] = None
			
			options = []
			opt2row = {}
			cnt = 1
			
			options.append("  --- Load preconfigured defaults ... ")
			
			for rule in rules:
				varname, vartype, name, choices, cond = rule
				
				if cond and (not check_condition(cond, config, rules)):
					continue
				
				if varname == selname:
					position = cnt
				
				if not varname in config:
					value = None
				else:
					value = config[varname]
				
				if not validate_rule_value(rule, value):
					value = None
				
				default = get_default_rule(rule)
				
				#
				# If we don't have a value but we do have
				# a default, use it.
				#
				if value == None and default != None:
					value = default
					config[varname] = default
				
				option = get_rule_option(rule, value)
				if option != None:
					options.append(option)
				else:
					continue
				
				opt2row[cnt] = (varname, vartype, name, choices)
				
				cnt += 1
			
			if (position != None) and (position >= len(options)):
				position = None
			
			(button, value) = xtui.choice_window(screen, 'HelenOS configuration', 'Choose configuration option', options, position)
			
			if button == 'cancel':
				return 'Configuration canceled'
			
			if button == 'done':
				if (infer_verify_choices(config, rules)):
					break
				else:
					xtui.error_dialog(screen, 'Error', 'Some options have still undefined values. These options are marked with the "?" sign.')
					continue
			
			if value == 0:
				profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config)
				if profile != None:
					read_presets(profile, config)
				position = 1
				continue
			
			position = None
			if not value in opt2row:
				raise RuntimeError("Error selecting value: %s" % value)
			
			(selname, seltype, name, choices) = opt2row[value]
			
			if not selname in config:
				value = None
			else:
				value = config[selname]
			
			if seltype == 'choice':
				config[selname] = subchoice(screen, name, choices, value)
			elif (seltype == 'y/n') or (seltype == 'n/y'):
				if config[selname] == 'y':
					config[selname] = 'n'
				else:
					config[selname] = 'y'
	finally:
		xtui.screen_done(screen)
	
	preprocess_config(config, rules)
	create_output(MAKEFILE, MACROS, config, rules)
	return 0
Example #6
0
def main():
    profile = None
    config = {}
    rules = []

    # Parse rules file
    parse_rules(RULES_FILE, rules)

    if len(sys.argv) > ARGPOS_CHOICE:
        choice = sys.argv[ARGPOS_CHOICE]
    else:
        choice = None

    if len(sys.argv) > ARGPOS_PRESET:
        preset = sys.argv[ARGPOS_PRESET]
    else:
        preset = None

    # Input configuration file can be specified on command line
    # otherwise configuration from previous run is used.
    if preset is not None:
        profile = parse_profile_name(preset)
        read_presets(profile, config)
    elif os.path.exists(MAKEFILE):
        read_config(MAKEFILE, config)

    # Default mode: check values and regenerate configuration files
    if choice == 'default':
        if (infer_verify_choices(config, rules)):
            preprocess_config(config, rules)
            create_output(MAKEFILE, MACROS, config, rules)
            return 0

    # Hands-off mode: check values and regenerate configuration files,
    # but no interactive fallback
    if choice == 'hands-off':
        # We deliberately test this because we do not want
        # to read implicitly any possible previous run configuration
        if preset is None:
            sys.stderr.write("Configuration error: No presets specified\n")
            return 2

        if (infer_verify_choices(config, rules)):
            preprocess_config(config, rules)
            create_output(MAKEFILE, MACROS, config, rules)
            return 0

        sys.stderr.write("Configuration error: The presets are ambiguous\n")
        return 1

    # Check mode: only check configuration
    if choice == 'check':
        if infer_verify_choices(config, rules):
            return 0
        return 1

    # Random mode
    if choice == 'random':
        ok = random_choices(config, rules, 0)
        if not ok:
            sys.stderr.write(
                "Internal error: unable to generate random config.\n")
            return 2
        if not infer_verify_choices(config, rules):
            sys.stderr.write(
                "Internal error: random configuration not consistent.\n")
            return 2
        preprocess_config(config, rules)
        create_output(MAKEFILE, MACROS, config, rules)

        return 0

    screen = xtui.screen_init()
    try:
        selname = None
        position = None
        while True:

            # Cancel out all values which have to be deduced
            for varname, vartype, name, choices, cond in rules:
                if (vartype == 'y') and (varname
                                         in config) and (config[varname]
                                                         == '*'):
                    config[varname] = None

            options = []
            opt2row = {}
            cnt = 1

            options.append("  --- Load preconfigured defaults ... ")

            for rule in rules:
                varname, vartype, name, choices, cond = rule

                if cond and not cond.evaluate(config):
                    continue

                if varname == selname:
                    position = cnt

                if not varname in config:
                    value = None
                else:
                    value = config[varname]

                if not validate_rule_value(rule, value):
                    value = None

                default = get_default_rule(rule)

                #
                # If we don't have a value but we do have
                # a default, use it.
                #
                if value == None and default != None:
                    value = default
                    config[varname] = default

                option = get_rule_option(rule, value)
                if option != None:
                    options.append(option)
                else:
                    continue

                opt2row[cnt] = (varname, vartype, name, choices)

                cnt += 1

            if (position != None) and (position >= len(options)):
                position = None

            (button,
             value) = xtui.choice_window(screen, 'HelenOS configuration',
                                         'Choose configuration option',
                                         options, position)

            if button == 'cancel':
                return 'Configuration canceled'

            if button == 'done':
                if (infer_verify_choices(config, rules)):
                    break
                else:
                    xtui.error_dialog(
                        screen, 'Error',
                        'Some options have still undefined values. These options are marked with the "?" sign.'
                    )
                    continue

            if value == 0:
                profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config)
                if profile != None:
                    read_presets(profile, config)
                position = 1
                continue

            position = None
            if not value in opt2row:
                raise RuntimeError("Error selecting value: %s" % value)

            (selname, seltype, name, choices) = opt2row[value]

            if not selname in config:
                value = None
            else:
                value = config[selname]

            if seltype == 'choice':
                config[selname] = subchoice(screen, name, choices, value)
            elif (seltype == 'y/n') or (seltype == 'n/y'):
                if config[selname] == 'y':
                    config[selname] = 'n'
                else:
                    config[selname] = 'y'
    finally:
        xtui.screen_done(screen)

    preprocess_config(config, rules)
    create_output(MAKEFILE, MACROS, config, rules)
    return 0