def _run_changed(ucr, changed, msg=""):
    # type: (ConfigRegistry, Dict[str, Tuple[Optional[str], Optional[str]]], str) -> None
    """
	Run handlers for changed UCR variables.

	:param ucr: UCR instance.
	:param changed: Mapping from UCR variable name to 2-tuple (old-value, new-value).
	:param msg: Message to be printed when change is shadowed by higher layer. Must contain 2 `%s` placeholders for `key` and `scope-name`.
	"""
    visible = {}  # type: Dict[str, Tuple[Optional[str], Optional[str]]]
    for key, (old_value, new_value) in changed.items():
        replog(ucr, key, old_value, new_value)

        reg = ucr.scope
        while old_value is None and reg > 0:
            reg -= 1
            old_value = ucr._registry[reg].get(key)

        reg, new_value = ucr.get(key, (0, None), getscope=True)
        if reg > ucr.scope:
            if msg:
                print(msg % (key, SCOPE[reg]), file=sys.stderr)
        else:
            visible[key] = (old_value, new_value)

    handlers = ConfigHandlers()
    handlers.load()
    handlers(list(visible), (ucr, visible))
Example #2
0
def handler_commit(args, opts=dict()):
	"""Commit all registered templated files."""
	ucr = ConfigRegistry()
	ucr.load()

	handlers = ConfigHandlers()
	handlers.load()
	handlers.commit(ucr, args)
Example #3
0
def handler_unregister(args, opts=dict()):
	"""Unregister old info file."""
	ucr = ConfigRegistry()
	ucr.load()

	handlers = ConfigHandlers()
	cur = handlers.update()  # cache must be current
	obsolete = handlers.unregister(args[0], ucr)
	handlers.update_divert(cur - obsolete)
Example #4
0
def _run_changed(ucr, changed, msg=None):
    for key, (old_value, new_value) in changed.iteritems():
        replog(ucr, key, old_value, new_value)
        if msg:
            scope, _value = ucr.get(key, (0, None), getscope=True)
            if scope > ucr.scope:
                print >> sys.stderr, msg % (key, SCOPE[scope])

    handlers = ConfigHandlers()
    handlers.load()
    handlers(changed.keys(), (ucr, changed))
Example #5
0
def handler_update(args, opts=dict()):
	# type: (List[str], Dict[str, Any]) -> None
	"""
	Update handlers.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
	handlers = ConfigHandlers()
	cur = handlers.update()
	handlers.update_divert(cur)
Example #6
0
def handler_register(args, opts=dict()):
	"""Register new info file."""
	ucr = ConfigRegistry()
	ucr.load()

	handlers = ConfigHandlers()
	handlers.update()  # cache must be current
	# Bug #21263: by forcing an update here, the new .info file is already
	# incorporated. Calling register for multifiles will increment the
	# def_count a second time, which is not nice, but uncritical, since the
	# diversion is (re-)done when >= 1.
	handlers.register(args[0], ucr)
def handler_update(args, opts=dict()):
    # type: (List[str], Dict[str, Any]) -> None
    """
	Update handlers.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
    handlers = ConfigHandlers()
    cur = handlers.update()
    handlers.update_divert(cur)

    ucr = ConfigRegistry()
    _register_variable_default_values(ucr)
Example #8
0
def _run_changed(ucr, changed, msg=None):
	# type: (ConfigRegistry, Dict[str, Tuple[Optional[str], Optional[str]]], str) -> None
	"""
	Run handlers for changes UCR variables.

	:param ucr: UCR instance.
	:param changed: Mapping from UCR variable name to 2-tuple (old-value, new-value).
	"""
	for key, (old_value, new_value) in changed.items():
		replog(ucr, key, old_value, new_value)
		if msg:
			scope, _value = ucr.get(key, (0, None), getscope=True)
			if scope > ucr.scope:
				print(msg % (key, SCOPE[scope]), file=sys.stderr)

	handlers = ConfigHandlers()
	handlers.load()
	handlers(list(changed.keys()), (ucr, changed))
Example #9
0
def handler_unset(args, opts=dict()):
    """
	Unset config registry variables in args.
	"""
    current_scope = ConfigRegistry.NORMAL
    reg = None
    if opts.get('ldap-policy', False):
        current_scope = ConfigRegistry.LDAP
        reg = ConfigRegistry(write_registry=current_scope)
    elif opts.get('force', False):
        current_scope = ConfigRegistry.FORCED
        reg = ConfigRegistry(write_registry=current_scope)
    elif opts.get('schedule', False):
        current_scope = ConfigRegistry.SCHEDULE
        reg = ConfigRegistry(write_registry=current_scope)
    else:
        reg = ConfigRegistry()
    reg.lock()
    try:
        reg.load()

        handlers = ConfigHandlers()
        handlers.load()

        changed = {}
        for arg in args:
            if reg.has_key(arg, write_registry_only=True):
                oldvalue = reg[arg]
                print 'Unsetting %s' % arg
                del reg[arg]
                changed[arg] = (oldvalue, '')
                k = reg.get(arg, None, getscope=True)
                replog('unset', current_scope, reg, arg, oldvalue)
                if k and k[0] > current_scope:
                    print >> sys.stderr, \
                      'W: %s is still set in scope "%s"' % \
                      (arg, SCOPE[k[0]])
            else:
                msg = "W: The config registry variable '%s' does not exist"
                print >> sys.stderr, msg % (arg, )
        reg.save()
    finally:
        reg.unlock()
    handlers(changed.keys(), (reg, changed))
Example #10
0
def handler_unset(args, opts=dict()):
	"""
	Unset config registry variables in args.
	"""
	current_scope = ConfigRegistry.NORMAL
	reg = None
	if opts.get('ldap-policy', False):
		current_scope = ConfigRegistry.LDAP
		reg = ConfigRegistry(write_registry=current_scope)
	elif opts.get('force', False):
		current_scope = ConfigRegistry.FORCED
		reg = ConfigRegistry(write_registry=current_scope)
	elif opts.get('schedule', False):
		current_scope = ConfigRegistry.SCHEDULE
		reg = ConfigRegistry(write_registry=current_scope)
	else:
		reg = ConfigRegistry()
	reg.lock()
	try:
		reg.load()

		handlers = ConfigHandlers()
		handlers.load()

		changed = {}
		for arg in args:
			if reg.has_key(arg, write_registry_only=True):
				oldvalue = reg[arg]
				print 'Unsetting %s' % arg
				del reg[arg]
				changed[arg] = (oldvalue, '')
				k = reg.get(arg, None, getscope=True)
				replog('unset', current_scope, reg, arg, oldvalue)
				if k and k[0] > current_scope:
					print >> sys.stderr, \
							'W: %s is still set in scope "%s"' % \
							(arg, SCOPE[k[0]])
			else:
				msg = "W: The config registry variable '%s' does not exist"
				print >> sys.stderr, msg % (arg,)
		reg.save()
	finally:
		reg.unlock()
	handlers(changed.keys(), (reg, changed))
Example #11
0
def handler_commit(args, opts=dict()):
    """Commit all registered templated files."""
    ucr = ConfigRegistry()
    ucr.load()

    handlers = ConfigHandlers()
    handlers.load()
    handlers.commit(ucr, args)
Example #12
0
def handler_unregister(args, opts=dict()):
    """Unregister old info file."""
    ucr = ConfigRegistry()
    ucr.load()

    handlers = ConfigHandlers()
    cur = handlers.update()  # cache must be current
    obsolete = handlers.unregister(args[0], ucr)
    handlers.update_divert(cur - obsolete)
Example #13
0
def handler_register(args, opts=dict()):
    """Register new info file."""
    ucr = ConfigRegistry()
    ucr.load()

    handlers = ConfigHandlers()
    handlers.update()  # cache must be current
    # Bug #21263: by forcing an update here, the new .info file is already
    # incorporated. Calling register for multifiles will increment the
    # def_count a second time, which is not nice, but uncritical, since the
    # diversion is (re-)done when >= 1.
    handlers.register(args[0], ucr)
Example #14
0
def handler_commit(args, opts=dict()):
	# type: (List[str], Dict[str, Any]) -> None
	"""
	Commit all registered templated files.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
	ucr = ConfigRegistry()
	ucr.load()

	handlers = ConfigHandlers()
	handlers.load()
	handlers.commit(ucr, args)
Example #15
0
def handler_unregister(args, opts=dict()):
	# type: (List[str], Dict[str, Any]) -> None
	"""
	Unregister old `.info` file.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
	ucr = ConfigRegistry()
	ucr.load()

	handlers = ConfigHandlers()
	cur = handlers.update()  # cache must be current
	obsolete = handlers.unregister(args[0], ucr)
	handlers.update_divert(cur - obsolete)
Example #16
0
def handler_register(args, opts=dict()):
	# type: (List[str], Dict[str, Any]) -> None
	"""
	Register new `.info` file.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
	ucr = ConfigRegistry()
	ucr.load()

	handlers = ConfigHandlers()
	handlers.update()  # cache must be current
	# Bug #21263: by forcing an update here, the new .info file is already
	# incorporated. Calling register for multifiles will increment the
	# def_count a second time, which is not nice, but uncritical, since the
	# diversion is (re-)done when >= 1.
	handlers.register(args[0], ucr)
Example #17
0
def handler_update(args, opts=dict()):
    """Update handlers."""
    handlers = ConfigHandlers()
    cur = handlers.update()
    handlers.update_divert(cur)
Example #18
0
def handler_set(args, opts=dict(), quiet=False):
    """
	Set config registry variables in args.
	Args is an array of strings 'key=value' or 'key?value'.
	"""
    handlers = ConfigHandlers()
    handlers.load()

    current_scope = ConfigRegistry.NORMAL
    reg = None
    if opts.get('ldap-policy', False):
        current_scope = ConfigRegistry.LDAP
        reg = ConfigRegistry(write_registry=current_scope)
    elif opts.get('force', False):
        current_scope = ConfigRegistry.FORCED
        reg = ConfigRegistry(write_registry=current_scope)
    elif opts.get('schedule', False):
        current_scope = ConfigRegistry.SCHEDULE
        reg = ConfigRegistry(write_registry=current_scope)
    else:
        reg = ConfigRegistry()

    reg.lock()
    try:
        reg.load()

        changed = {}
        for arg in args:
            sep_set = arg.find('=')  # set
            sep_def = arg.find('?')  # set if not already set
            if sep_set == -1 and sep_def == -1:
                print >> sys.stderr, \
                 "W: Missing value for config registry variable '%s'" % \
                 (arg,)
                continue
            else:
                if sep_set > 0 and sep_def == -1:
                    sep = sep_set
                elif sep_def > 0 and sep_set == -1:
                    sep = sep_def
                else:
                    sep = min(sep_set, sep_def)
            key = arg[0:sep]
            value = arg[sep + 1:]
            old = reg.get(key)
            if (old is None or sep == sep_set) and validate_key(key):
                if not quiet:
                    if reg.has_key(key, write_registry_only=True):
                        print 'Setting %s' % key
                    else:
                        print 'Create %s' % key
                    k = reg.get(key, None, getscope=True)
                    if k and k[0] > current_scope:
                        print >> sys.stderr, \
                         'W: %s is overridden by scope "%s"' % \
                         (key, SCOPE[k[0]])
                reg[key] = value
                changed[key] = (old, value)
                replog('set', current_scope, reg, key, old, value)
            else:
                if not quiet:
                    if old is not None:
                        print 'Not updating %s' % key
                    else:
                        print 'Not setting %s' % key

        reg.save()
    finally:
        reg.unlock()

    handlers(changed.keys(), (reg, changed))
Example #19
0
def handler_update(args, opts=dict()):
	"""Update handlers."""
	handlers = ConfigHandlers()
	cur = handlers.update()
	handlers.update_divert(cur)
Example #20
0
def handler_set(args, opts=dict(), quiet=False):
	"""
	Set config registry variables in args.
	Args is an array of strings 'key=value' or 'key?value'.
	"""
	handlers = ConfigHandlers()
	handlers.load()

	current_scope = ConfigRegistry.NORMAL
	reg = None
	if opts.get('ldap-policy', False):
		current_scope = ConfigRegistry.LDAP
		reg = ConfigRegistry(write_registry=current_scope)
	elif opts.get('force', False):
		current_scope = ConfigRegistry.FORCED
		reg = ConfigRegistry(write_registry=current_scope)
	elif opts.get('schedule', False):
		current_scope = ConfigRegistry.SCHEDULE
		reg = ConfigRegistry(write_registry=current_scope)
	else:
		reg = ConfigRegistry()

	reg.lock()
	try:
		reg.load()

		changed = {}
		for arg in args:
			sep_set = arg.find('=')  # set
			sep_def = arg.find('?')  # set if not already set
			if sep_set == -1 and sep_def == -1:
				print >> sys.stderr, \
					"W: Missing value for config registry variable '%s'" % \
					(arg,)
				continue
			else:
				if sep_set > 0 and sep_def == -1:
					sep = sep_set
				elif sep_def > 0 and sep_set == -1:
					sep = sep_def
				else:
					sep = min(sep_set, sep_def)
			key = arg[0:sep]
			value = arg[sep + 1:]
			old = reg.get(key)
			if (old is None or sep == sep_set) and validate_key(key):
				if not quiet:
					if reg.has_key(key, write_registry_only=True):
						print 'Setting %s' % key
					else:
						print 'Create %s' % key
					k = reg.get(key, None, getscope=True)
					if k and k[0] > current_scope:
						print >> sys.stderr, \
							'W: %s is overridden by scope "%s"' % \
							(key, SCOPE[k[0]])
				reg[key] = value
				changed[key] = (old, value)
				replog('set', current_scope, reg, key, old, value)
			else:
				if not quiet:
					if old is not None:
						print 'Not updating %s' % key
					else:
						print 'Not setting %s' % key

		reg.save()
	finally:
		reg.unlock()

	handlers(changed.keys(), (reg, changed))