Ejemplo n.º 1
0
def test_env3():
    if six.PY2:
        os.environ["UNICODE_ENV_VAR"] = UNICODE.encode("utf8")
    else:
        os.environ["UNICODE_ENV_VAR"] = UNICODE
    x = OpinionatedConfigParser()
    x.read_dict(TEST_DICT1)
    assert x.get("section1", "key2") == UNICODE
    del os.environ["UNICODE_ENV_VAR"]
def test_read_dict_without_inheritance2():
    x = OpinionatedConfigParser(configuration_name="foo_foo_foo_foo")
    x.read_dict(TEST_DICT1)
    assert x.get("section1", "key1") == "value2"
    assert x.get("section1", "key2") == "value4"
    assert x.get("section1", "key3", fallback=None) is None
    assert x.get("section1", "key4", fallback=None) is None
    assert x.get("section2", "key1", fallback=None) is None
    assert x.get("section2", "key3") == "value5"
    assert x.get("section2", "key4", fallback=None) is None
def test_read_dict_with_inheritance2():
    x = OpinionatedConfigParser(configuration_name="foo_bar_1",
                                default_section="_common")
    x.read_dict(TEST_DICT1)
    assert x.get("section1", "key1") == "value6"
    assert x.get("section1", "key2") == "value4"
    assert x.get("section1", "key3") == "value7"
    assert x.get("section1", "key4") == "value8"
    assert x.get("section2", "key1") == "value6"
    assert x.get("section2", "key3") == "value5"
    assert x.get("section1", "key4") == "value8"
    assert x.get("_common", "key3", fallback=None) is None
Ejemplo n.º 4
0
def make_env_var_dict(
    configuration_name,
    prefix,
    paths,
    legacy_env=False,
    legacy_file_inclusion=False,
    resolve=False,
    legacy_file_inclusion_directory=None,
    generation_time=False,
    ignore_keys_starting_with="_",
):
    if prefix is None:
        prefix = os.environ.get("MFMODULE", None)
        if prefix is None:
            print(
                "ERROR: can't determine PREFIX, use --prefix option",
                file=sys.stderr,
            )
            sys.exit(1)
    if configuration_name is None:
        configuration_name = os.environ.get("MFCONFIG", None)
        if configuration_name is None:
            configuration_name = "GENERIC"
    configuration_name = configuration_name.lower()
    parser = OpinionatedConfigParser(configuration_name=configuration_name)
    paths = [x for x in paths if os.path.isfile(x)]
    parser.read(paths)
    env_var_dict = {}
    for section in parser.sections():
        for option in parser.options(section):
            if ignore_keys_starting_with and \
                    option.strip().startswith(ignore_keys_starting_with):
                continue
            val = parser.get(section, option)
            if legacy_env:
                val = _legacy_env(val)
            if legacy_file_inclusion:
                lfid = legacy_file_inclusion_directory
                val = _legacy_file(val, lfid)
            if resolve:
                if not parser.has_option(section, "%s_ip" % option):
                    if (option.lower().endswith("_hostname")
                            or option.lower() == "hostname"):
                        add_resolved_ip_var(env_var_dict, prefix, section,
                                            option, val)
                    elif (option.lower().endswith("_hostnames")
                          or option.lower() == "hostnames"):
                        add_resolved_ips_var(env_var_dict, prefix, section,
                                             option, val)
            add_env_var(env_var_dict, prefix, section, option, val)
    if generation_time:
        add_env_var(env_var_dict, prefix, "conf", "generation_time",
                    str(int(time.time())))
    return env_var_dict
Ejemplo n.º 5
0
 def read(self, path, section_prefix="switch_rules*"):
     self.log = self.log.bind(path=path)
     result = RulesSet()
     x = OpinionatedConfigParser(delimiters=("=",), comment_prefixes=("#",))
     x.optionxform = str
     x.read([path])
     rules_block = None
     for section in x.sections():
         if fnmatch.fnmatch(section, "%s:*:*" % section_prefix):
             tempo = section.split(':')
             typ = tempo[1]
             params = ':'.join(tempo[2:]).split(',')
             rules_block = self.rules_block_factory(typ, params)
             result.add_rules_block(rules_block)
         elif fnmatch.fnmatch(section, "%s:*" % section_prefix):
             tempo = section.split(':')
             typ = tempo[1]
             rules_block = self.rules_block_factory(typ)
             result.add_rules_block(rules_block)
         else:
             continue
         for option in x.options(section):
             val = x.get(section, option)
             actions = [y.strip() for y in val.split(',')]
             pattern = option.replace('@@@@@@', '=').replace('~~~~~~', '#')
             switch_rule = SwitchRule(pattern)
             for action in actions:
                 tempo2 = action.split('/')
                 if len(tempo2) == 2:
                     if tempo2[1].endswith('*'):
                         act = self.action_factory("hardlink", tempo2[0],
                                                   tempo2[1][:-1])
                     else:
                         act = self.action_factory("copy", tempo2[0],
                                                   tempo2[1])
                 else:
                     self.log.error("bad action [%s] for section [%s] "
                                    "and pattern: %s" % (action, section,
                                                         option))
                     raise BadSyntax()
                 switch_rule.add_action(act)
             rules_block.add_switch_rule(switch_rule)
     return result
Ejemplo n.º 6
0
def main():
    arg_parser = argparse.ArgumentParser(
        description="Lit le contenu d'un fichier .ini")
    arg_parser.add_argument("path",
                            help="Chemin du fichier de config a visualiser")
    arg_parser.add_argument("section",
                            help="Precise la section a afficher",
                            nargs='?',
                            default=None)
    arg_parser.add_argument("option",
                            help="Precise l'option a afficher",
                            nargs='?',
                            default=None)
    arg_parser.add_argument(
        "config",
        help="Precise la config a utiliser pour l'affichage",
        nargs='?',
        default=None)
    args = arg_parser.parse_args()

    if (args.config):
        parser = OpinionatedConfigParser(configuration_name=args.config)
    else:
        parser = OpinionatedConfigParser()
    data = codecs.open(str(args.path), "r", "utf-8")
    parser.read_file(data)

    if (args.section):
        # Affichage de la valeur d'une option
        if (args.option):
            print_option(parser, args.section, args.option)

        # Affichage du contenu d'une section
        else:
            print_section(parser, args.section)

    # Affichage de l'ensemble des sections du fichier
    else:
        print_file(parser)
Ejemplo n.º 7
0
def build_plugin(plugin_path, plugins_base_dir=None):
    """Build a plugin.

    Args:
        plugin_path (string): the plugin path to build
        plugins_base_dir (string): (optional) the plugin base directory path.
            If not set, the default plugins base directory path is used.

    Raises:
        MFUtilPluginCantBuild: if a error occurs during build

    """
    plugin_path = os.path.abspath(plugin_path)
    plugins_base_dir = _get_plugins_base_dir(plugins_base_dir)
    base = os.path.join(plugins_base_dir, "base")
    pwd = os.getcwd()
    parser = OpinionatedConfigParser()
    with open(os.path.join(plugin_path, "config.ini"), "r") as f:
        config_content = f.read()
    if six.PY2:
        parser.read_string(config_content.decode('utf-8'))
    else:
        parser.read_string(config_content)
    with open(os.path.join(plugin_path, ".layerapi2_label"), "r") as f:
        name = f.read().replace('plugin_', '', 1).split('@')[0]
    version = parser['general']['version']
    summary = parser['general']['summary']
    license = parser['general']['license']
    try:
        packager = parser['general']['packager']
    except Exception:
        packager = parser['general']['maintainer']
    vendor = parser['general']['vendor']
    url = parser['general']['url']
    tmpdir = os.path.join(RUNTIME_HOME, "tmp",
                          "plugin_%s" % get_unique_hexa_identifier())
    mkdir_p_or_die(os.path.join(tmpdir, "BUILD"))
    mkdir_p_or_die(os.path.join(tmpdir, "RPMS"))
    mkdir_p_or_die(os.path.join(tmpdir, "SRPMS"))
    _make_plugin_spec(os.path.join(tmpdir, "specfile.spec"), name, version,
                      summary, license, packager, vendor, url)
    cmd = "source %s/lib/bash_utils.sh ; " % MFEXT_HOME
    cmd = cmd + "layer_load rpm@mfext ; "
    cmd = cmd + 'rpmbuild --define "_topdir %s" --define "pwd %s" ' \
        '--define "prefix %s" --dbpath %s ' \
        '-bb %s/specfile.spec' % (tmpdir, plugin_path, tmpdir,
                                  base, tmpdir)
    x = BashWrapperOrRaise(cmd, MFUtilPluginCantBuild,
                           "can't build plugin %s" % plugin_path)
    tmp = glob.glob(os.path.join(tmpdir, "RPMS", "x86_64", "*.rpm"))
    if len(tmp) == 0:
        raise MFUtilPluginCantBuild("can't find generated plugin" %
                                    plugin_path, bash_wrapper=x)
    plugin_path = tmp[0]
    new_basename = \
        os.path.basename(plugin_path).replace("x86_64.rpm",
                                              "metwork.%s.plugin" %
                                              MFMODULE_LOWERCASE)
    new_plugin_path = os.path.join(pwd, new_basename)
    shutil.move(plugin_path, new_plugin_path)
    shutil.rmtree(tmpdir, True)
    os.chdir(pwd)
    return new_plugin_path
def test_issue8():
    x = OpinionatedConfigParser()
    x.read_dict(TEST_DICT1)
    assert x.get("section3", "issue8[foo]bar") == "foobar"
def test_constructor():
    x = OpinionatedConfigParser()
    assert x is not None
def test_unicode():
    x = OpinionatedConfigParser()
    x.read_dict(TEST_DICT1)
    assert x.get("section1", "key1") == UNICODE
def init():
    global monitors, inactivityTimeout, logger

    if monitors:
        # Init already performed
        return True

    arg_parser = ArgumentParser()
    arg_parser.add_argument('-c', '--config', help='Configuration filename '
                            '( absolute or relative path to a directory under '
                            'module home directory )'
                            )
    args = arg_parser.parse_args()
    homedir = None
    try:
        if args.config is None:
            logger.critical("you must provide a configuration file")
            sys.exit(1)
        if args.config.startswith('~'):
            args.config = os.path.abspath(args.config)

        if args.config.startswith('/'):
            ficconfig = args.config
            if not os.path.exists(ficconfig):
                logger.critical("File %s does not exist or is not accessible" %
                                ficconfig)
                ficconfig = None
        else:
            ficconfig = None
            homedir = os.environ[os.environ['MFMODULE'] + '_HOME']
            for directory in DEFAULT_HOME_CONFIG_DIRS:
                ficconfig = os.path.abspath(homedir)
                ficconfig = os.path.join(ficconfig, directory)
                ficconfig = os.path.join(ficconfig, args.config)
                if os.path.exists(ficconfig):
                    break
                ficconfig = None
            if not ficconfig:
                logger.info("File %s not found under standard configuration"
                            " directories")
                pass
    except KeyError:
        logger.critical("Can't obtain Module home directory")
        ficconfig = None

    if ficconfig and os.path.isdir(ficconfig):
        logger.critical("%s is a directory !" % ficconfig)
        ficconfig = None

    if not ficconfig:
        return False

    parser = OpinionatedConfigParser(default_section="common")
    data = codecs.open(ficconfig, "r", "utf-8")
    try:
        parser.read_file(data)
    except Exception:
        logger.critical('Error Loading config: %s' % ficconfig)
        return False

    Monitor.server = None
    Monitor.port = None
    Monitor.redis = None
    Monitor.unix_socket = None
    Monitor.wm = None

    monitor_inactivitytimeout = None

    monitors = []

    for s in parser._sections:
        mon = Monitor()
        active = False
        key = 'active'
        if parser.has_option(s, key):
            active = parser.getboolean(s, key)
        if not active:
            continue

        key = 'redis_server'
        if not parser.has_option(s, key):
            continue
        server = parser[s][key]
        if Monitor.server is None:
            Monitor.server = server
        elif Monitor.server != server:
            logger.critical('Only 1 redis server Please')
            return False

        key = 'redis_unix_socket'
        if not parser.has_option(s, key):
            continue

        unix_socket = parser[s][key]
        if len(unix_socket) == 0 or unix_socket == "null":
            unix_socket = None
        Monitor.unix_socket = unix_socket

        key = 'redis_port'
        if not parser.has_option(s, key):
            continue

        port = parser.getint(s, key)
        if Monitor.port is None:
            Monitor.port = port
        elif Monitor.port != port:
            logger.critical('Only 1 redis port Please')
            return False

        key = 'monitor_inactivitytimeout'
        if parser.has_option(s, key):
            timeout = parser.getint(s, key)
            if timeout <= 0:  # Standardization
                timeout = -1
            if monitor_inactivitytimeout is None:
                monitor_inactivitytimeout = timeout
                inactivityTimeout = timeout
            elif monitor_inactivitytimeout != timeout:
                logger.critical('Only 1 monitor_inactivitytimeout Please')
                return False

        key = 'directory'
        if not parser.has_option(s, key):
            continue

        mon.dir = parser[s][key]

        key = 'queue'
        if not parser.has_option(s, key):
            continue
        mon.redisqueue = parser[s][key]
        if not mon.redisqueue:
            logger.critical('Invalid redis queue name')
            continue

        q = mon.redisqueue.replace('queue:', '').replace('out_', 'out.')
        q = re.sub(r'stage(\d+)_', r'stage\1.', q)

        key = 'timeout'
        if parser.has_option(s, key):
            mon.redistimeout = parser.getint(s, key)

        key = 'forced_poll'
        if parser.has_option(s, key):
            mon.tpolling = parser.getint(s, key)

        key = 'poll_queue_limit'
        if parser.has_option(s, key):
            mon.pollinglimit = parser.getint(s, key)

        key = 'only'
        if parser.has_option(s, key):
            mon.only = getMask(parser[s][key])

        key = 'ignore'
        cnfgignorestr = None
        if parser.has_option(s, key):
            cnfgignorestr = parser[s][key]
        mon.ignore = getMask(cnfgignorestr, (r'^.*\.working$',))

        monitors.append(mon)

    if not monitors or len(monitors) < 1:
        logger.critical('No directory to scan')
        return False

    return True
Ejemplo n.º 12
0
def test_env2():
    os.environ["ENV_VAR"] = "foo"
    x = OpinionatedConfigParser()
    x.read_dict(TEST_DICT1)
    assert x.get("section1", "key1") == "value1foo"
    del os.environ["ENV_VAR"]
Ejemplo n.º 13
0
def test_env1():
    if "ENV_VAR" in os.environ:
        del os.environ["ENV_VAR"]
    x = OpinionatedConfigParser()
    x.read_dict(TEST_DICT1)
    assert x.get("section1", "key1") == "value1"