Example #1
0
    def write_ini_config_file(self, command, data):
        """
        Write the new command configuration in the plugin configuration file
        """
        # read the config file
        config = ConfigParser()
        config.read(command.plugin.config.fileName)

        # if there is no commands section
        if not config.has_section('commands'):
            raise NoSectionError(
                'could not find <commands> section in plugin <%s> config file'
                % data['plugin_name'])

        # remove the old entry
        found = False
        for temp in config.options('commands'):
            search = temp.split('-')[0]
            if search == command.command:
                config.remove_option('commands', temp)
                found = True

        # set the new command option value
        config.set('commands', data['command_name'], data['command_level'])
        self.debug('%s command <%s> in plugin <%s> config file',
                   'updated' if found else 'created new entry for',
                   command.command, data['plugin_name'])

        # write the updated configuration file
        with open(command.plugin.config.fileName, 'wb') as configfile:
            config.write(configfile)
Example #2
0
def fixConfig():

    conf = ConfigParser()
    conf_name = ['conf.ini'] 
    for i in range(1, 40):
        conf_name.append('conf' + str(i) + '.ini')

    for each in conf_name:
        conf.read(each)
        sections = conf.sections()
        #if each == 'conf1.ini':
        #    pdb.set_trace()
        
        conf.remove_option('slave', 'parsers')
        conf.remove_option('slave', 'sources')
        sections = conf.sections()

        sections.remove('slave')
        sections.remove('proxy')
        sections.remove('master')

        for ele in sections:
            try:
                res = dict(conf.items(ele))
                conf.remove_section(ele)
                if res['source'] == 'britishairRound':
                    res['source'] = 'britishairRoundFlight'
                conf.add_section(res['source'])
                conf.set(res['source'], 'class_name', res['class_name'])
                conf.set(res['source'], 'file_path', res['file_path'].split('../')[1])
                conf.write(open(each, 'w'))
            except Exception, e:
                print str(e)
                continue
Example #3
0
    def parse_image_build_config(self, config_file_name):

        # Logic taken from koji.cli.koji.handle_image_build.
        # Unable to re-use koji's code because "cli" is not
        # a package of koji and this logic is intermingled
        # with CLI specific instructions.

        args = []
        opts = {}

        config = ConfigParser()
        config.readfp(self.get_default_image_build_conf())
        config.read(config_file_name)

        if self.architectures:
            config.set('image-build', 'arches', ','.join(self.architectures))
        elif self.architecture:
            config.set('image-build', 'arches', self.architecture)
        # else just use what was provided by the user in image-build.conf

        config_str = StringIO()
        config.write(config_str)
        self.log.debug('Image Build Config: \n%s', config_str.getvalue())

        image_name = None

        section = 'image-build'
        for option in ('name', 'version', 'arches', 'target', 'install_tree'):
            value = config.get(section, option)
            if not value:
                raise ValueError('{} cannot be empty'.format(option))
            if option == 'arches':
                value = [arch for arch in value.split(',') if arch]
            elif option == 'name':
                image_name = value
            args.append(value)
            config.remove_option(section, option)

        for option, value in config.items(section):
            if option in ('repo', 'format'):
                value = [v for v in value.split(',') if v]
            elif option in ('disk_size'):
                value = int(value)
            opts[option] = value

        section = 'ova-options'
        if config.has_section(section):
            ova = []
            for k, v in config.items(section):
                ova.append('{}={}'.format(k, v))
            opts['ova_option'] = ova

        section = 'factory-parameters'
        if config.has_section(section):
            factory = []
            for option, value in config.items(section):
                factory.append((option, value))
            opts['factory_parameter'] = factory

        return image_name, args, {'opts': opts}
Example #4
0
def delete_value(section, name):
    config = ConfigParser()
    if isfile(CONFIG_FILE):
        config.read(CONFIG_FILE)
        config.remove_option(section, name)
        with open(CONFIG_FILE, 'wb') as configfile:
            config.write(configfile)
class ConfigFileMgr(object):
    def __init__(self):
        self.cluster_name = configuration().get_cluster_name()
        self.config_file_path = '/etc/ceph/' + self.cluster_name + '.conf'
        self.config = ConfigParser()
        self.config.read(self.config_file_path)

    def print_debug(self):
        out = ""
        self.config.read(self.config_file_path)
        for section in self.config.sections():
            out += "[%s] \n" % section
            for options in self.config.options(section):
                out += "%s = %s \n" % (options,
                                       self.config.get(section, options))
            out += "\n"
        print out

    def add_option(self, section, option, value):
        self.config.set(section, option, value)

    def remove_option(self, section, option):
        if self.config.has_option(section, option):
            self.config.remove_option(section, option)

    def save_config_file(self):
        with open(self.config_file_path, 'w+') as configfile:
            self.config.write(configfile)
Example #6
0
 def remove_option(self, section, option):
     """Removes the option from ConfigParser as well as
     the secure storage backend
     """
     if self.is_secure_option(section, option) and self.keyring_available:
         s_option = "%s%s" % (section, option)
         self._unsaved[s_option] = ('delete', None)
     ConfigParser.remove_option(self, section, option)
Example #7
0
def getVerticaDBConfig(vDbName='',
                       vMetaFile='/opt/vertica/config/admintools.conf'):
    """ get configurations of Vertica database
  Arguments:
    vDbName: string
      Vertica database name, if ignored, fist database in vMetaFile(/opt/vertica/config/admintools.conf) will be choosed.
    vMetaFile string
      Vertica database meta file
  Returns:
    vDbName: string
    catPath: string
      catalog pararent path
    nodeNames: list of nodename
    hostIPs: list of IP
  """

    # get configurations(path, node, host) of Vertica database
    try:
        configdict = ConfigParser()
        if not os.path.exists(vMetaFile):
            raise StandardError("Vertica database meta file [%s] not exists!" %
                                vMetaFile)
        configdict.read(vMetaFile)

        pat = re.compile("^Database:%s" % vDbName)
        sections = [d for d in configdict.sections() if pat.match(d)]
        if len(sections) == 0:
            raise StandardError(
                "No Vertica database [%s] is defined in meta file [%s] not exists!"
                % (vDbName, vMetaFile))
        section = sections[0]
        if vDbName == '':
            vDbName = section.split(":")[1]
        catPath = configdict.get(section, 'path')
        if len(catPath) == 0:
            raise StandardError(
                "No [path] property of Vertica database [%s] defined in meta file [%s]!"
                % (vDbName, vMetaFile))
        nodeNames = configdict.get(section, 'nodes').split(',')
        if len(nodeNames) == 0 or len(nodeNames[0]) == 0:
            raise StandardError(
                "No [nodes] property of Vertica database [%s] defined in meta file [%s]!"
                % (vDbName, vMetaFile))
        if configdict.has_option(section, 'host'):
            configdict.remove_option(section, 'host')
        hostIPs = [configdict.get('Nodes', n).split(',')[0] for n in nodeNames]
        if len(hostIPs) != len(nodeNames):
            raise StandardError(
                "Nodes section of Vertica database [%s] defined in meta file [%s] is not correct!"
                % (vDbName, vMetaFile))

        return vDbName, catPath, nodeNames, hostIPs
    except Exception, e:
        raise StandardError(
            "'%s' when getting configurations of Vertica database [%s] in meta file [%s]."
            % (str(e), vDbName, vMetaFile))
Example #8
0
    def remove_option(self, section, key):
        if section != 'randoms':
            ConfigParser.remove_option(self, section, key)

        elif section in self.command_line and key in self.command_line[section]:
            del self.command_line[section][key]
            if not self.command_line[section]:
                del self.command_line[section]
        else:
            if key in self.randoms:
                del self.randoms[key]
            elif key == 'all':
                self.randoms = {}
    def update_auth_files(self, repositories):
        for repo in repositories:
            writers = repo.maintainers() | repo.writers()
            writers = expand_user_set(self.env, writers)
            readers = expand_user_set(self.env, writers | repo.readers())

            hgrc_path = os.path.join(repo.directory, '.hg/hgrc')

            hgrc = ConfigParser()
            hgrc.read(hgrc_path)

            options = ('deny_read', 'deny_push', 'allow_read', 'allow_push',
                       'allow_write')
            if hgrc.has_section('web'):
                for option in options:
                    if hgrc.has_option('web', option):
                        hgrc.remove_option('web', option)
            else:
                hgrc.add_section('web')

            if repo.description:
                hgrc.set('web', 'description', repo.description)

            def apply_user_list(users, action):
                if not users:
                    hgrc.set('web', 'deny_' + action, '*')
                    return
                if 'anonymous' in users:
                    return
                if 'authenticated' in users:
                    hgrc.set('web', 'deny_' + action, 'anonymous')
                    return
                hgrc.set('web', 'allow_' + action, ', '.join(sorted(users)))

            apply_user_list(readers, 'read')
            if repo.maintainers():
                apply_user_list(writers, 'write')
            else:
                apply_user_list(writers, 'push')

            with open(hgrc_path, 'wb') as hgrc_file:
                hgrc.write(hgrc_file)
            try:
                modes = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP
                os.chmod(hgrc_path, modes)
            except:
                pass
Example #10
0
 def remove_option(self, section, option):
     try:
         res=ConfigParser.remove_option(self, section, option)
         if res and self.saveimmediately:
             self.save()
         return res
     except NoSectionError:
         return 0
Example #11
0
def remove_option(section, option):

    path = CONFIGURATION_PATH
    path = os.path.expanduser(path)
    if not os.path.exists(path):
        raise Exception("File '{}' does not exist.".format(path))
    parser = ConfigParser()
    parser.read(path)
    if not parser.has_section(section):
        raise Exception("Section '{}' does not exist.".format(section))
    if not parser.has_option(section, option):
        raise Exception("Option '{}' in section '{}' does not exist.".format(option, section))
    parser.remove_option(section, option)
    f = open(path, 'wb')
    parser.write(f)
    f.close()

    return
def goglib_tags_create(game_name, tags, tags_file):

    parser = ConfigParser()
    parser.read(tags_file)
    if 'goglib tags' in parser.sections():
        exists = True
    else:
        exists = False

    file = open(tags_file, 'w')
    if exists == False:
        parser.add_section('goglib tags')
    if tags == '':
        parser.remove_option('goglib tags', game_name)
    else:
        parser.set('goglib tags', game_name, str(tags))
    parser.write(file)
    file.close()
Example #13
0
    def cb_checkbutton_own_mapper(self, button):

        self.own_dosbox_mapperfile = button.get_active()
        self.button_remap_keys.set_visible(button.get_active())

        game_dir = self.install_dir + '/' + self.game_name
        dosbox_conf = game_dir + '/dosbox.conf'

        if button.get_active() == True:

            dosbox_keymap_path = game_dir + '/dosbox_keymap'

            if not os.path.exists(dosbox_conf):
                open(dosbox_conf, 'a').close()

            config_parser = ConfigParser()
            config_parser.read(dosbox_conf)

            if not config_parser.has_section('sdl'):
                config_parser.add_section('sdl')

            config_parser.set('sdl', 'mapperfile', str(dosbox_keymap_path))

            new_config_file = open(dosbox_conf, 'w')
            config_parser.write(new_config_file)
            new_config_file.close()

        else:

            if os.path.exists(dosbox_conf):

                config_parser = ConfigParser()
                config_parser.read(dosbox_conf)

                if (config_parser.has_section('sdl')) and \
                        config_parser.has_option('sdl', 'mapperfile'):

                    config_parser.remove_option('sdl', 'mapperfile')

                if (len(config_parser.sections()) == 1) and \
                        (len(config_parser.options('sdl')) == 0):

                    os.system('rm ' + dosbox_conf)
Example #14
0
class ChirpConfig(object):
    def __init__(self, basepath, name="chirp.config"):
        self.__basepath = basepath
        self.__name = name

        self._default_section = "global"

        self.__config = ConfigParser()

        cfg = os.path.join(basepath, name)
        if os.path.exists(cfg):
            self.__config.read(cfg)

    def save(self):
        cfg = os.path.join(self.__basepath, self.__name)
        cfg_file = open(cfg, "w")
        self.__config.write(cfg_file)
        cfg_file.close()

    def get(self, key, section, raw=False):
        if not self.__config.has_section(section):
            return None

        if not self.__config.has_option(section, key):
            return None

        return self.__config.get(section, key, raw=raw)

    def set(self, key, value, section):
        if not self.__config.has_section(section):
            self.__config.add_section(section)

        self.__config.set(section, key, value)

    def is_defined(self, key, section):
        return self.__config.has_option(section, key)

    def remove_option(self, section, key):
        self.__config.remove_option(section, key)

        if not self.__config.items(section):
            self.__config.remove_section(section)
Example #15
0
class Config(object):
    """
    Config class to read and write options
    """
    def __init__(self, file_name=None):
        self.file_name = file_name or 'config.cfg'

        self.config = ConfigParser()
        self.config.read(['config.example.cfg', self.file_name])

    def set(self, section, option, value):
        if not self.config.has_section(section):
            self.config.add_section(section)

        self.config.set(section, option, value)

    def remove_option(self, section, option, value):
        if self.config.has_section(section):
            self.config.remove_option(section)

        if len(self.items(section)) == 0:
            self.remove_section(section)

    def remove_section(self, section):
        if self.config.has_section(section):
            self.config.remove_section(section)

    def get(self, section, option, raw=False):
        return self.config.get(section, option, raw)

    def items(self, section):
        return self.config.items(section)

    def has(self, section, option=None):
        if option:
            return self.config.has_option(section, option)

        return self.config.has_section(section)

    def write(self):
        with open(self.file_name, 'wb') as config_file:
            self.config.write(config_file)
Example #16
0
    def reset(self, name):
        # If custom conf file is not set then it is only read only configs
        if self.custom_conf_file is None:
            raise GconfNotConfigurable()

        # If a config can not be modified
        if name != "all" and not self._is_configurable(name):
            raise GconfNotConfigurable()

        cnf = ConfigParser()
        with open(self.custom_conf_file) as f:
            cnf.readfp(f)

            # Nothing to Reset, Not configured
            if name != "all":
                if not cnf.has_option("vars", name):
                    return True

                # Remove option from custom conf file
                cnf.remove_option("vars", name)
            else:
                # Remove and add empty section, do not disturb if config file
                # already has any other section
                try:
                    cnf.remove_section("vars")
                except NoSectionError:
                    pass

                cnf.add_section("vars")

        with open(self.tmp_conf_file, "w") as fw:
            cnf.write(fw)

        os.rename(self.tmp_conf_file, self.custom_conf_file)

        self.reload()

        return True
Example #17
0
def SetConfigs( configs):
    """
    Method used to set configuration directives in INI file kb_config
    Takes as a parameter a dictionary of config directives within the authentication section
    that need to be set/unset. If there is a dictionary entry where the value is None
    then the config setting will be deleted
    """
    global kb_config,authdata,tokenenv,AuthSvcHost,RolesSvcHost
    global RoleSvcURL,nexusconfig,conf

    conf = ConfigParser()
    if os.path.exists( kb_config):
        conf.read(kb_config)
    if not conf.has_section('authentication'):
        conf.add_section('authentication')
    for key in configs.keys():
        if configs[key] is not None:
            conf.set('authentication',key, configs[key])
        else:
            conf.remove_option('authentication',key)
    with open(kb_config, 'wb') as configfile:
        conf.write(configfile)
    LoadConfig()
Example #18
0
class Config(object):

    def __init__(self, inifile='data/config.ini'):
        self.inifile = inifile
        self.cfg = ConfigParser()

        with FileLock(inifile):
            if os.path.exists(inifile):
                self.cfg.read(inifile)

            # initialize configurations
            default_configs = {
                'server': {
                    'ip': '*',
                    'port': '8888',
                    'lastcheckupdate': 0,
                    'updateinfo': ''
                },
                'auth': {
                    'username': '******',
                    'password': '',         # empty password never validated
                    'passwordcheck': 'on',
                    'accesskey': '',        # empty access key never validated
                    'accesskeyenable': 'off',
                },
                'runtime': {
                    'mode': '',             # format: demo | dev | prod
                    'loginlock': 'off',
                    'loginfails': 0,
                    'loginlockexpire': 0,
                },
                'file': {
                    'lastdir': '/root',
                    'lastfile': '',
                },
                'time': {
                    'timezone': '' # format: timezone = Asia/Shanghai
                },
                'ecs': {
                    'accounts': ''
                },
                'inpanel': {
                    'Instance Name': 'Access key'
                }
            }
            needupdate = False
            for sec, secdata in default_configs.items():
                if not self.cfg.has_section(sec):
                    self.cfg.add_section(sec)
                    needupdate = True
                for opt, val in secdata.items():
                    if not self.cfg.has_option(sec, opt):
                        self.cfg.set(sec, opt, val)
                        needupdate = True

            # update ini file
            if needupdate:
                self.update(False)

    def update(self, lock=True):
        if lock:
            flock = FileLock(self.inifile)
            flock.acquire()

        try:
            inifp = open(self.inifile, 'w')
            self.cfg.write(inifp)
            inifp.close()
            if lock:
                flock.release()
            return True
        except:
            if lock:
                flock.release()
            return False

    def has_option(self, section, option):
        return self.cfg.has_option(section, option)

    def get(self, section, option):
        return self.cfg.get(section, option)

    def getboolean(self, section, option):
        return self.cfg.getboolean(section, option)

    def getint(self, section, option):
        return self.cfg.getint(section, option)

    def has_section(self, section):
        return self.cfg.has_section(section)

    def add_section(self, section):
        return self.cfg.add_section(section)

    def remove_option(self, section, option):
        return self.cfg.remove_option(section, option)

    def set(self, section, option, value):
        try:
            self.cfg.set(section, option, value)
        except:
            return False
        return self.update()
Example #19
0
class PlatformUserConfig:
    def __init__(self, config_file):
        self.log = logger.get_logger('PlatformUserConfig')
        self.parser = ConfigParser()
        self.filename = config_file

    def update_redirect(self, domain, api_url):
        self.parser.read(self.filename)
        self.log.info('setting domain={0}, api_url={1}'.format(
            domain, api_url))

        self.__set('redirect', 'domain', domain)
        self.__set('redirect', 'api_url', api_url)
        self.__save()

    def get_redirect_domain(self):
        self.parser.read(self.filename)
        if self.parser.has_section('redirect') and self.parser.has_option(
                'redirect', 'domain'):
            return self.parser.get('redirect', 'domain')
        return 'syncloud.it'

    def get_redirect_api_url(self):
        self.parser.read(self.filename)
        if self.parser.has_section('redirect') and self.parser.has_option(
                'redirect', 'api_url'):
            return self.parser.get('redirect', 'api_url')
        return 'http://api.syncloud.it'

    def set_user_update_token(self, user_update_token):
        self.parser.read(self.filename)
        self.__set('redirect', 'user_update_token', user_update_token)
        self.__save()

    def get_user_update_token(self):
        self.parser.read(self.filename)
        return self.parser.get('redirect', 'user_update_token')

    def set_user_email(self, user_email):
        self.parser.read(self.filename)
        self.__set('redirect', 'user_email', user_email)
        self.__save()

    def get_user_email(self):
        self.parser.read(self.filename)
        return self.parser.get('redirect', 'user_email')

    def set_custom_domain(self, custom_domain):
        self.parser.read(self.filename)
        self.__set('platform', 'custom_domain', custom_domain)
        self.__save()

    def set_activated(self):
        self.parser.read(self.filename)
        self.__set('platform', 'activated', True)
        self.__save()

    def is_activated(self):

        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'activated'):
            return False
        return self.parser.getboolean('platform', 'activated')

    def get_custom_domain(self):
        self.parser.read(self.filename)
        if self.parser.has_option('platform', 'custom_domain'):
            return self.parser.get('platform', 'custom_domain')
        return None

    def get_user_domain(self):
        self.parser.read(self.filename)
        if self.parser.has_option('platform', 'user_domain'):
            return self.parser.get('platform', 'user_domain')
        return None

    def get_domain_update_token(self):
        self.parser.read(self.filename)
        if self.parser.has_option('platform', 'domain_update_token'):
            return self.parser.get('platform', 'domain_update_token')
        return None

    def update_domain(self, user_domain, domain_update_token):
        self.parser.read(self.filename)
        self.log.info(
            'saving user_domain = {0}, domain_update_token = {0}'.format(
                user_domain, domain_update_token))
        self.__set('platform', 'user_domain', user_domain)
        self.__set('platform', 'domain_update_token', domain_update_token)
        self.__save()

    def get_external_access(self):
        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'external_access'):
            return False
        return self.parser.getboolean('platform', 'external_access')

    def is_redirect_enabled(self):
        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'redirect_enabled'):
            return True
        return self.parser.getboolean('platform', 'redirect_enabled')

    def set_redirect_enabled(self, enabled):
        self.parser.read(self.filename)
        self.__set('platform', 'redirect_enabled', enabled)
        self.__save()

    def update_device_access(self, upnp_enabled, external_access, public_ip,
                             manual_certificate_port, manual_access_port):
        self.parser.read(self.filename)
        self.__set('platform', 'external_access', external_access)
        self.__set('platform', 'upnp', upnp_enabled)
        self.__set('platform', 'public_ip', public_ip)
        self.__set('platform', 'manual_certificate_port',
                   manual_certificate_port)
        self.__set('platform', 'manual_access_port', manual_access_port)
        self.__save()

    def get_upnp(self):
        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'upnp'):
            return True
        return self.parser.getboolean('platform', 'upnp')

    def get_public_ip(self):
        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'public_ip'):
            return None
        return self.parser.get('platform', 'public_ip')

    def get_manual_certificate_port(self):
        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'manual_certificate_port'):
            return None
        return self.parser.get('platform', 'manual_certificate_port')

    def get_manual_access_port(self):
        self.parser.read(self.filename)
        if not self.parser.has_option('platform', 'manual_access_port'):
            return None
        return self.parser.get('platform', 'manual_access_port')

    def __set(self, section, key, value):
        if not self.parser.has_section(section):
            self.parser.add_section(section)
        if value is None:
            self.parser.remove_option(section, key)
        else:
            self.parser.set(section, key, value)

    def __save(self):
        with open(self.filename, 'wb') as f:
            self.parser.write(f)
Example #20
0
class Configuration(object):
    """
    Thin layer over `ConfigParser` from the Python standard library.
    
    In addition to providing some convenience methods, the class remembers
    the last modification time of the configuration file, and reparses it
    when the file has changed.
    """
    def __init__(self, filename=None):
        self._sections = {}
        self.filename = filename
        self.parser = ConfigParser()
        self._lastmtime = 0
        self._lastsitemtime = 0
        self.parse_if_needed()

    def __contains__(self, name):
        """
        Return whether the configuration contains a section of the given name.
        """
        return self.parser.has_section(name)

    def __getitem__(self, name):
        """
        Return the configuration section with the specified name.
        """
        if name not in self._sections:
            self._sections[name] = Section(self, name)
        return self._sections[name]

    def get(self, section, name, default=None):
        """
        Return the value of the specified option.
        """
        return self[section].get(name, default)

    def getbool(self, section, name, default=None):
        """
        Return the specified option as boolean value.
        
        If the value of the option is one of "yes", "true",  "on", or "1", this
        method will return `True`, otherwise `False`.
        """
        return self[section].getbool(name, default)

    def getint(self, section, name, default=None):
        """
        Return the value of the specified option as integer.
        
        If the specified option can not be converted to an integer, a
        `ConfigurationError` exception is raised.
        """
        return self[section].getint(name, default)

    def getlist(self, section, name, default=None, sep=',', keep_empty=False):
        """
        Return a list of values that have been specified as a single
        comma-separated option.
        
        A different separator can be specified using the `sep` parameter. If
        the `keep_empty` parameter is set to `True`, empty elements are
        included in the list.
        """
        return self[section].getlist(name, default, sep, keep_empty)

    def set(self, section, name, value):
        """
        Change a configuration value.
        
        These changes are not persistent unless saved with `save()`.
        """
        self[section].set(name, value)

    def defaults(self):
        """
        Returns a dictionary of the default configuration values.
        """
        defaults = {}
        for (section, name), option in Option.registry.items():
            defaults.setdefault(section, {})[name] = option.default
        return defaults

    def options(self, section):
        """
        Return a list of `(name, value)` tuples for every option in the
        specified section.
        
        This includes options that have default values that haven't been
        overridden.
        """
        return self[section].options()

    def remove(self, section, name):
        """
        Remove the specified option.
        """
        if self.parser.has_section(section):
            self.parser.remove_option(section, name)

    delete = remove

    def sections(self):
        """
        Return a list of section names.
        """
        return sorted(set(self.parser.sections() + self.parser.sections()))

    def save(self):
        """
        Write the configuration options to the primary file.
        """
        # Only save options that differ from the defaults
        sections = []
        for section in self.sections():
            options = []
            for option in self[section]:
                current = self.parser.has_option(section, option) and \
                          self.parser.get(section, option)
                if current is not False:
                    options.append((option, current))
            if options:
                sections.append((section, sorted(options)))
        # skip saving if no filename is given
        if not self.filename:
            return
        fileobj = file(self.filename, 'w')
        try:
            print >> fileobj, '# -*- coding: utf-8 -*-'
            print >> fileobj
            for section, options in sections:
                print >> fileobj, '[%s]' % section
                for key, val in options:
                    if key in self[section].overridden:
                        print >> fileobj, '# %s = <set in global seishub.ini>' \
                                        % key
                    else:
                        val = val.replace(CRLF, '\n').replace('\n', '\n ')
                        print >> fileobj, '%s = %s' % \
                                        (key, toUnicode(val).encode('utf-8'))
                print >> fileobj
        finally:
            fileobj.close()

    def parse_if_needed(self):
        if not self.filename or not os.path.isfile(self.filename):
            return
        modtime = os.path.getmtime(self.filename)
        if modtime > self._lastmtime:
            self.parser.read(self.filename)
            self._lastmtime = modtime

    def has_site_option(self, section, name):
        return self.parser.has_option(section, name)
Example #21
0
class HGWeb:
    """
    Global config management class
    """
    def __init__(self, path, create=False):
        """ @path: string path to hgweb.config file """
        if not os.path.exists(path):
            if not create:
                raise ValueError(_("Can't load config file"))
            else:
                try:
                    f = open(path, "w")
                    f.close()
                except IOError:
                    raise ValueError(_("Can't create config file"))
        self._file_name = path
        self._parser = ConfigParser()
        self._parser.read(path)

    def get_paths_and_collections(self):
        """finds all paths from section [paths] and paths with appended /** from section [collection]"""
        ret_val = self.get_paths() + self.get_collections()

        return ret_val

    def get_paths(self):
        """ finds all paths from section [paths] """
        if self._parser.has_section("paths"):
            return self._parser.items("paths")
        else:
            return []

    def get_collections(self):
        if self._parser.has_section("collections"):
            collections = self._parser.items("collections")
            # todo: this exposes collections as paths with /** suffix. simple support of collections.
            #collections = [ (name, path + "/**") for name, path in collections]
            return collections
        else:
            return []

    def get_groups(self):
        paths = self.get_paths()
        groups = self.get_collections()
        for path in paths:
            if path[1].endswith("*"):
                groups.append(path)
        groups = sorted(groups)
        return groups

    def get_path(self, key):
        try:
            return self._parser.get("paths", key)
        except (NoOptionError, NoSectionError):
            return None

    def get_collection(self, key):
        try:
            return self._parser.get("collections", key)
        except (NoOptionError, NoSectionError):
            return None

    def add_paths(self, key, path):
        if "paths" not in self._parser.sections():
            self._parser.add_section("paths")
        self._parser.set("paths", key, path)
        with open(self._file_name, "w") as f:
            self._parser.write(f)

    def add_collections(self, key, path):
        if "collections" not in self._parser.sections():
            self._parser.add_section("collections")
        self._parser.set("collections", key, path)
        with open(self._file_name, "w") as f:
            self._parser.write(f)

    def del_paths(self, key):
        self._parser.remove_option("paths", key)
        with open(self._file_name, "w") as f:
            self._parser.write(f)

    def del_collections(self, key):
        self._parser.remove_option("collections", key)
        with open(self._file_name, "w") as f:
            self._parser.write(f)

    def get_web(self):
        try:
            return self._parser.items("web")
        except (NoOptionError, NoSectionError):
            return None

    def get_web_key(self, key):
        try:
            return self._parser.get("web", key)
        except (NoOptionError, NoSectionError):
            return None

    def set_web_key(self, key, value):
        if "web" not in self._parser.sections():
            self._parser.add_section("web")
        self._parser.set("web", key, value)
        with open(self._file_name, "w") as f:
            self._parser.write(f)

    def del_web_key(self, key):
        self._parser.remove_option("web", key)
        with open(self._file_name, "w") as f:
            self._parser.write(f)
Example #22
0
class ParserConf(Exception):
    "parser config."

    def __init__(self, confFile):

        if not os.path.isfile(confFile):
            raise CustomException("The file %s does not exist!" % confFile)
        self.confFile = confFile
        self.config = ConfigParser()
        #self.config.readfp(codecs.open(confFile, 'r',"utf_16"))
        self.config.read(confFile)

    def get_sections(self):
        "return all section."

        return self.config.sections()

    def add_section(self, section):
        "create a new section."

        self.config.add_section(section)
        self.config.write(open(self.confFile, "w"))

    def remove_section(self, section):
        "remove a section."

        self.config.remove_section(section)
        self.config.write(open(self.confFile, "w"))

    def get_items(self, section):
        "return section's items."

        try:
            return self.config.items(section)
        except:
            return None

    def get_item(self, section, key):
        "return a item value."

        try:
            return self.config.get(section, key)
        except:
            return None

    def update_item(self, section, key, value):
        "update a item."

        self.config.set(section, key, value)
        self.config.write(open(self.confFile, "w"))

    def create_item(self, section, key, value):
        "create a item."

        self.config.set(section, key, value)
        self.config.write(open(self.confFile, "w"))

    def remove_item(self, section, key):
        "remove a item."

        self.config.remove_option(section, key)
        self.config.write(open(self.confFile, "w"))
Example #23
0
class Config(object):
    """Config class holding all data from configuration files."""

    OFF = 0
    ON = 1
    IGNORE = 2

    def __init__(self, filename=''):
        self._config = ConfigParser()
        self.keys = {}  # {'action_name': 'key', }
        self.ignored = set()
        self.sections = {}  # {section.name: section, }
        self.aliases = {}  # {alias: section|action, }
        self.filename = filename
        self.load(filename)

    def __parse_settings(self):
        """Parse SETTINGS section of the config file"""
        for key, value in self._config.items('SETTINGS'):
            value = value.lower()
            if value in ['1', 'yes', 'on', 'true']:
                setattr(self, key, self.ON)
            elif value == 'ignore':
                setattr(self, key, self.IGNORE)
            elif value:
                setattr(self, key, self.OFF)

    def load(self, filename):
        """Load configuration file"""
        log.debug('Loading configuration file %s' % filename)
        self.filename = filename
        # Load config file (load default first)
        self._config.read([
            os.path.join('/', 'etc', 'pywo', 'pyworc'),
            os.path.join(os.path.dirname(__file__), '..', 'etc', 'pyworc'),
        ])
        if self.filename:
            # If filename provided use it instead of default location in ~/
            self._config.read(self.filename)
        else:
            self._config.read([
                os.path.join(os.path.expanduser('~'), '.config', 'pywo',
                             'pyworc'),
                os.path.join(os.path.expanduser('~'), '.pyworc'),
            ])
        # Get keys settings
        self.keys = dict(self._config.items('KEYS'))
        self._config.remove_section('KEYS')
        # Get aliases
        self.aliases = dict(self._config.items('ALIASES'))
        self.aliases = dict([[alias, name.lower()]
                             for alias, name in self.aliases.items()])
        self._config.remove_section('ALIASES')
        # Parse SETTINGS section
        if self._config.has_option('SETTINGS', 'layout'):
            # Load layout definition
            layout = self._config.get('SETTINGS', 'layout')
            self._config.read([
                os.path.join('/', 'etc', 'pywo', 'layouts', layout),
                os.path.join('/', 'etc', 'pywo', layout),
                os.path.join(os.path.dirname(__file__), '..', 'etc', layout),
                os.path.join(os.path.dirname(__file__), '..', 'etc', 'layouts',
                             layout),
                os.path.join(os.path.expanduser('~'), '.config', 'pywo',
                             'layouts', layout),
                os.path.join(os.path.expanduser('~'), '.config', 'pywo',
                             layout),
                os.path.join(os.path.expanduser('~'), layout)
            ])
            self._config.remove_option('SETTINGS', 'layout')
        self.ignored = set()
        if self._config.has_option('SETTINGS', 'ignore_actions'):
            # Parse ignore_actions setting
            ignored = self._config.get('SETTINGS', 'ignore_actions')
            self.ignored = set(ignored.split(', '))
            self._config.remove_option('SETTINGS', 'ignore_actions')
        if 'grid' in self.ignored:
            self.ignored.update('grid_width', 'grid_height')
        self.__parse_settings()
        self._config.remove_section('SETTINGS')
        # Parse every section
        self.sections = {}
        for section in self._config.sections():
            key = self.keys.pop(section, None)
            try:
                self.sections[section.lower()] = _Section(self, section, key)
            except Exception, exc:
                log.exception('Invalid section %s: %s', (section, exc))
            self._config.remove_section(section)

        log.debug('Loaded configuration file')
Example #24
0
def main():
    """
    Parse command line options and run tests with given options
    """
    
    parser = OptionParser()
    
    #Describes the available options.
    parser.add_option(
        '-c', '--test_config', dest='test_config',
        type='string', default='test_cases.cfg',
        help="Set the test configuration file to FILE", metavar="FILE"
        )
        
    parser.add_option(
        '-t', '--target_test', dest='target_test',
        type='string', action='append', default=[],
        help="Set the test to run to TEST", metavar="TEST"
        )
        
    parser.add_option(
        '-o', '--ocd', action='store_true', dest='ocd',
        help="Run APBS in OCD mode"
        )
        
    parser.add_option(
        '-l', '--log_file', dest='log_file', type='string', default='test.log',
        help="Save the test log to FILE.", metavar="FILE"
        )
    
    # Parse the command line and extract option values
    ( options, args ) = parser.parse_args()

    # Messages will go to stdout, log messages will go to the supplied log file
    message_fd = sys.stdout
    logfile_fd = None
    
    # Verify that the log file is writable
    try:
        logfile_fd = open( options.log_file, 'w' )
    except IOError as err:
        parser.error( "Could't open log_file %s: %s" % ( options.log_file, err.strerror ) )
        
    # Set up the logger with the message and log file descriptor
    logger = Logger( message_fd, logfile_fd )

    # Read the test cases file
    config = ConfigParser()
    config.read( options.test_config )

    # Make sure that the apbs binary can be found
    binary = test_binary()
    if binary == '':
        parser.error( "Coulnd't detect an apbs binary in the path or local bin directory" )

    # Get the names of all the test sections to run.
    test_sections = []
    if 'all' in options.target_test or options.target_test == []:
        test_sections = config.sections()
        print "Testing all sections"
    else:
        test_sections = options.target_test

    print "The following sections will be tested: " + ', '.join( test_sections )
    print '=' * 80

    # Run each test that has been requested
    for test_name in test_sections:
    
        print "Running tests for " + test_name + " section"
        
        # Verify that the test is described in the test cases file
        if test_name not in config.sections():
            print "  " + test_name + " section not found in " + options.test_config
            print "  skipping..."
            continue
            
        # As each test runs, remove the value so it can't be run twice
        test_directory = config.get( test_name, 'input_dir' )
        config.remove_option( test_name, 'input_dir' )
        
        # Run the test!
        run_test( binary, config.items( test_name ), test_name, test_directory, logger, options.ocd )
Example #25
0
File: config.py Project: zxfly/trac
 def remove_option(self, section, option):
     section_str = to_utf8(section)
     option_str = to_utf8(option)
     ConfigParser.remove_option(self, section_str, option_str)
Example #26
0
class Config(object):
    def __init__(self, inifile=None, configs=None):
        self.inifile = 'data/config.ini' if inifile is None else inifile
        self.cfg = ConfigParser()

        with FileLock(self.inifile):
            if os.path.exists(self.inifile):
                self.cfg.read(self.inifile)

            # initialize configurations
            if configs is None:
                default_configs = {
                    'server': {
                        'ip': '*',
                        'port': '8888',
                        'forcehttps': 'off',  # force use https
                        'lastcheckupdate': 0,
                        'updateinfo': '',
                        'sslkey':
                        '/usr/local/inpanel/core/certificate/inpanel.key',
                        'sslcrt':
                        '/usr/local/inpanel/core/certificate/inpanel.crt'
                    },
                    'auth': {
                        'username': '******',
                        'password': '',  # empty password never validated
                        'passwordcheck': 'on',
                        'accesskey': '',  # empty access key never validated
                        'accesskeyenable': 'off',
                    },
                    'runtime': {
                        'mode': '',  # format: demo | dev | prod
                        'loginlock': 'off',
                        'loginfails': 0,
                        'loginlockexpire': 0,
                    },
                    'file': {
                        'lastdir': '/root',
                        'lastfile': '',
                    },
                    'time': {
                        'timezone': ''  # format: timezone = Asia/Shanghai
                    },
                    'ecs': {
                        'accounts': ''
                    },
                    'inpanel': {
                        'Instance Name': 'Access key'
                    }
                }
            else:
                default_configs = configs

            needupdate = False
            for sec, secdata in default_configs.items():
                if not self.cfg.has_section(sec):
                    self.cfg.add_section(sec)
                    needupdate = True
                for opt, val in secdata.items():
                    if not self.cfg.has_option(sec, opt):
                        self.cfg.set(sec, opt, val)
                        needupdate = True

            # update ini file
            if needupdate:
                self.update(False)

    def update(self, lock=True):
        if lock:
            flock = FileLock(self.inifile)
            flock.acquire()

        try:
            inifp = open(self.inifile, 'w')
            self.cfg.write(inifp)
            inifp.close()
            if lock:
                flock.release()
            return True
        except:
            if lock:
                flock.release()
            return False

    def has_option(self, section, option):
        return self.cfg.has_option(section, option)

    def remove_option(self, section, option):
        return self.cfg.remove_option(section, option)

    def get(self, section, option):
        return self.cfg.get(section, option)

    def getboolean(self, section, option):
        return self.cfg.getboolean(section, option)

    def getint(self, section, option):
        return self.cfg.getint(section, option)

    def has_section(self, section):
        return self.cfg.has_section(section)

    def add_section(self, section):
        return self.cfg.add_section(section)

    def remove_section(self, section=None):
        if section is None:
            return False
        else:
            return self.cfg.remove_section(section)

    def set(self, section, option, value):
        try:
            self.cfg.set(section, option, value)
        except:
            return False
        return self.update()

    def get_section_list(self):
        '''Return a list of section names, excluding [DEFAULT]'''
        return self.cfg.sections()

    def get_option_list(self, section):
        '''Return a list of option names for the given section name.'''
        return self.cfg.options(section)

    def get_config_list(self):
        '''Return a list of all config for the given config file.'''
        config_list = []
        sections = self.cfg.sections()
        for section in sections:
            sec = {'section': section, 'option': {}}
            options = self.cfg.options(section)
            for key in options:
                sec['option'][key] = self.cfg.get(section, key)
            config_list.append(sec)
        return config_list

    def get_config(self):
        '''Return a dict of all config for the given config file.'''
        config = {}
        for section in self.cfg.sections():
            config[section] = {}
            for item in self.cfg.options(section):
                config[section][item] = self.cfg.get(section, item)
        return config

    def addsection(self, section, data):
        '''add one section'''
        try:
            if not self.cfg.has_section(section):
                self.cfg.add_section(section)
            for option, value in data.items():
                self.cfg.set(section, option, value)
            return self.update(False)
        except:
            return False

    def addsections(self, section):
        '''add some sections'''
        try:
            for sec, data in section.items():
                if not self.cfg.has_section(sec):
                    self.cfg.add_section(sec)
                for option, value in data.items():
                    self.cfg.set(sec, option, value)
            return self.update(False)
        except:
            return False
Example #27
0
class config_loader:
    """
    Base class of the configuration parser
    """
    def __init__(self, cfg, tmpdir='/tmp', raise_errors=False):
        """
        Instantiate ConfigParser and provide the file like object that we'll
        use to read configuration data from.
        :param cfg: Where we'll get configuration data. It can be either:
                * A URL containing the file
                * A valid file path inside the filesystem
                * A string containing configuration data
        :param tmpdir: Where we'll dump the temporary conf files.
        :param raise_errors: Whether config value absences will raise
                ValueError exceptions.
        """
        # Base Parser
        self.parser = ConfigParser()
        # Raise errors when lacking values
        self.raise_errors = raise_errors
        # File is already a file like object
        if hasattr(cfg, 'read'):
            self.cfg = cfg
            self.parser.readfp(self.cfg)
        elif isinstance(cfg, types.StringTypes):
            # Config file is a URL. Download it to a temp dir
            if cfg.startswith('http') or cfg.startswith('ftp'):
                self.cfg = path.join(tmpdir, path.basename(cfg))
                utils.urlretrieve(cfg, self.cfg)
                self.parser.read(self.cfg)
            # Config is a valid filesystem path to a file.
            elif path.exists(path.abspath(cfg)):
                if path.isfile(cfg):
                    self.cfg = path.abspath(cfg)
                    self.parser.read(self.cfg)
                else:
                    e_msg = 'Invalid config file path: %s' % cfg
                    raise IOError(e_msg)
            # Config file is just a string, convert it to a python file like
            # object using StringIO
            else:
                self.cfg = StringIO(cfg)
                self.parser.readfp(self.cfg)

    def get(self, section, option, default=None):
        """
        Get the value of a option.

        Section of the config file and the option name.
        You can pass a default value if the option doesn't exist.

        :param section: Configuration file section.
        :param option: Option we're looking after.
        :default: In case the option is not available and raise_errors is set
                to False, return the default.
        """
        if not self.parser.has_option(section, option):
            if self.raise_errors:
                raise ValueError('No value for option %s. Please check your '
                                 'config file "%s".' % (option, self.cfg))
            else:
                return default

        return self.parser.get(section, option)

    def set(self, section, option, value):
        """
        Set an option.

        This change is not persistent unless saved with 'save()'.
        """
        if not self.parser.has_section(section):
            self.parser.add_section(section)
        return self.parser.set(section, option, value)

    def remove(self, section, option):
        """
        Remove an option.
        """
        if self.parser.has_section(section):
            self.parser.remove_option(section, option)

    def save(self):
        """
        Save the configuration file with all modifications
        """
        if not self.cfg:
            return
        fileobj = file(self.cfg, 'w')
        try:
            self.parser.write(fileobj)
        finally:
            fileobj.close()

    def check(self, section):
        """
        Check if the config file has valid values
        """
        if not self.parser.has_section(section):
            return False, "Section not found: %s" % (section)

        options = self.parser.items(section)
        for i in range(options.__len__()):
            param = options[i][0]
            aux = string.split(param, '.')

            if aux.__len__ < 2:
                return False, "Invalid parameter syntax at %s" % (param)

            if not self.check_parameter(aux[0], options[i][1]):
                return False, "Invalid value at %s" % (param)

        return True, None

    def check_parameter(self, param_type, parameter):
        """
        Check if a option has a valid value
        """
        if parameter == '' or parameter is None:
            return False
        elif param_type == "ip" and self.__isipaddress(parameter):
            return True
        elif param_type == "int" and self.__isint(parameter):
            return True
        elif param_type == "float" and self.__isfloat(parameter):
            return True
        elif param_type == "str" and self.__isstr(parameter):
            return True

        return False

    def __isipaddress(self, parameter):
        """
        Verify if the ip address is valid

        :param ip String: IP Address
        :return: True if a valid IP Address or False
        """
        octet1 = "([1-9][0-9]{,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
        octet = "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
        pattern = "^" + octet1 + "\.(" + octet + "\.){2}" + octet + "$"
        if re.match(pattern, parameter) is None:
            return False
        else:
            return True

    def __isint(self, parameter):
        try:
            int(parameter)
        except Exception:
            return False
        return True

    def __isfloat(self, parameter):
        try:
            float(parameter)
        except Exception:
            return False
        return True

    def __isstr(self, parameter):
        try:
            str(parameter)
        except Exception:
            return False
        return True
Example #28
0
class Configurator:
    class DatabaseNotDefined(Exception):
        def __init__(self, db):
            Exception.__init__(self, "Database %s is not defined." % db)

    instance = None
    last_modified = None
    lock = threading.RLock()

    @staticmethod
    def Instance():
        """
        Callable method to get the singleton instance of this object.
        """
        Configurator.lock.acquire()
        try:
            with FileLock(ADMINTOOLS_CONF):
                if os.path.exists(ADMINTOOLS_CONF):
                    modified_time = time.localtime(
                        os.path.getmtime(ADMINTOOLS_CONF))
                else:
                    modified_time = time.localtime()  # now

                if Configurator.instance is None:
                    Configurator.instance = Configurator(ADMINTOOLS_CONF)
                    Configurator.last_modified = modified_time
                    return Configurator.instance

                # XXX: If True?  Once upon a time this worked. It was
                # accidentally disabled, it seems.  Can we successfully
                # reenable it?
                if True or Configurator.last_modified != modified_time:
                    Configurator.instance = Configurator(ADMINTOOLS_CONF)

            return Configurator.instance
        finally:
            Configurator.lock.release()

    @staticmethod
    def _transform_to_ip(name):
        # 从admintools移除 hostnames. 尝试解析hostnames到IP addresses.
        # 如果不成功,(i.e. 因为 DNS 此时不能工作) 单独留下.
        try:
            return util.resolve_to_one_ip(name)
        except StandardError as err:
            print(
                "WARNING: Unable to resolve hostname %s found in admintools.conf."
                % name,
                file=sys.stderr)
            return name

    def _move_configdict_section(self, from_sec, to_sec):
        """Moves all of the values from (and removes) `from_sec` to `to_sec`

        Any values in `to_sec`, if it exists, will remain.  Values from
        `from_sec` will overwrite any shared keys.  `to_sec` will exist after a
        call here, even if `from_sec` does not.
        """

        if not self.configdict.has_section(to_sec):
            self.configdict.add_section(to_sec)

        if not self.configdict.has_section(from_sec):
            return

        for (key, value) in self.configdict.items(from_sec, raw=True):
            self.configdict.set(to_sec, key, value)

        self.configdict.remove_section(from_sec)

    def __load(self, filename):
        self.configdict.read(filename)

        ###################################################################
        # [Configuration] 进入self.config映射.
        #
        if self.configdict.has_section('Configuration'):
            self.config = dict(self.configdict.items('Configuration'))
        else:
            self.config = {}

        self.config.setdefault('controlmode', "broadcast")

        # last_port 保持了数据库最后使用的基本端口.
        # 新数据库能够被创建,使用下一个有效的端口集合(因此能够并发运行).
        # 对于配置接下来的逻辑将生成last_port,此配置并没有此值(例如. 更新逻辑).
        if "last_port" not in self.config:
            last_port = 0
            for db in self.listDatabases():
                dbport = self.configdict.get("Database:%s" % db, "port")
                if dbport and int(dbport) > last_port:
                    last_port = int(dbport)

            if last_port == 0:
                last_port = 5433

            self.configdict.set("Configuration", "last_port", str(last_port))
            self.config['last_port'] = str(last_port)

        ###################################################################
        # [Cluster] 部分有一个单条目列出主机集合
        #
        self.cluster = self.configdict.get('Cluster', 'hosts').split(',')
        self.cluster = [x.strip() for x in self.cluster if len(x.strip()) > 0]
        self.cluster = [self._transform_to_ip(x) for x in self.cluster]

        ###################################################################
        # [Nodes] 部分曾经是[sites]部分.
        # 每个数据库实例都有一个条目,带有唯一的实例名称.
        # 通常情况下,每个数据库都有节点的一个子集,
        # 如果不使用 --compat21,它们都是'node_xxxx'.
        #
        self._move_configdict_section('sites', 'Nodes')
        # self.sites: 节点名称到"addr,catalog,data"的映射
        self.sites = self.configdict.items('Nodes')

        # 映射site的主机到IP地址
        # 即使site值是畸形的,此部分也能够工作
        def map_site_to_ip(site):
            tmp = site.split(',', 1)
            tmp[0] = self._transform_to_ip(tmp[0])
            return ','.join(tmp)

        self.sites = [(x, map_site_to_ip(y)) for (x, y) in self.sites]
        for (name, data) in self.sites:
            self.configdict.set('Nodes', name, data)

        ###################################################################
        # [Database:*] 部分定义现有的数据库.
        # 迭代数据库擦掉不可用的host选项,可能包含一个主机名.
        #
        for db in self.listDatabases():
            section = "Database:%s" % db
            if self.configdict.has_option(section, 'host'):
                self.configdict.remove_option(section, 'host')

    def __init__(self, filename):
        """
        Constructor method - shouldn't be called.  Use Configurator.Instance()
        instead.
        """
        self.filename = filename
        self.configdict = ConfigParser()

        if filename is not None and os.path.exists(filename):
            self.__load(filename)
        else:
            self.init_defaults()
            # 我们进行更新操作, 对于old icky admin格式进行更好的测试,
            # 同时从这构建配置.
            self._legacy_port()

        # 进入有空主机值的配置文件.
        # 清除空值. 同时也删除重复值.
        self.cluster = [x.strip() for x in self.cluster if len(x.strip()) != 0]
        seen = set()
        uniq_cluster = []
        for x in self.cluster:
            if x not in seen:
                seen.add(x)
                uniq_cluster.append(x)
        self.cluster = uniq_cluster

        self.configdict.set("Cluster", "hosts", ','.join(self.cluster))

    def init_defaults(self):
        # 使用缺省的控制(重新)创建此文件.
        self.config = {}
        self.config['format'] = 3
        self.config['install_opts'] = ""
        self.config[
            'default_base'] = "/home/dbadmin"  #@todo - fix for install variable
        self.config['controlmode'] = "broadcast"
        self.config['controlsubnet'] = "default"
        self.config['spreadlog'] = "False"
        self.config['last_port'] = "5433"

        self.cluster = []
        self.sites = []

        def clear_section(section):
            if self.configdict.has_section(section):
                self.configdict.remove_section(section)
            self.configdict.add_section(section)

        clear_section("Configuration")
        for (k, v) in self.config.iteritems():
            self.configdict.set("Configuration", k, v)

        # 清除集群. only has one value, hosts, which is empty.
        clear_section("Cluster")
        self.configdict.set("Cluster", "hosts", '')

        # 清除节点. Starts empty. (sites = [])
        clear_section("Nodes")

    def set_options(self, opts):
        """
        Records the options used for install_mesadb so the user can look back
        at them.  They should not be pulled and parsed for any reason, since
        they are not reliably written and any sensitive values are scrubbed.
        """

        hide = ["-p", "--dba-user-password", "-P", "--ssh-password"]

        for x in hide:
            if x in opts:
                opts[opts.index(x) + 1] = "*******"

        val = util.shell_repr(opts)
        self.config['install_opts'] = val
        self.configdict.set("Configuration", "install_opts", val)

    def listDatabases(self):
        """ Returns a list of defined database names """
        # 每个数据库定义在 [Database:foo] 部分中.
        # 找到每个部分匹配的部分,同时返回 'foo'.
        pat = re.compile("^Database:")
        sections = [
            d.split(":")[1] for d in self.configdict.sections() if pat.match(d)
        ]
        return sections

    def incluster(self, hostname):
        """ Returns True if the given hostname is in the cluster. """
        return (hostname in self.cluster)

    def isdefined(self, database):
        """ Returns True if the given database is already defined. """
        return (database in self.listDatabases())

    def gethosts(self):
        """ Returns a list of hosts in the cluster (IP address strings) """
        return list(self.cluster)

    def getconfig(self, database):
        """
        return a diction of properties about the initial host
        in the database. Other classes will use this data to
        bootstrap the system.
        """
        if (not self.isdefined(database)):
            raise Configurator.DatabaseNotDefined(database)

        props = {}

        key = "Database:%s" % database

        options = self.configdict.options(key)
        for option in options:
            if option == 'nodes':
                props[option + "_new"] = []
                for n in self.configdict.get(key, option).split(','):
                    props[option + "_new"].append(self.getsiteconfig(n))
            props[option] = self.configdict.get(key, option)
        props['id'] = database

        # upgrade from a daily bug fix
        # just default to never (the same as never setting it!)
        if not 'restartpolicy' in props.keys():
            props['restartpolicy'] = 'never'

        return props

    def addsite(self, nodename, host, catalog_base, data_base):
        """ add a site to the configuration parameters """

        # 从admintools清除主机名称
        assert util.is_ip_address(host), \
                "All sites must be added by IP address (%s)" % host

        c = catalog_base
        d = data_base

        if c.endswith("/"):
            c = c[:len(c) - 1]

        if d.endswith("/"):
            d = d[:len(d) - 1]

        data = "%s,%s,%s" % (host, c, d)
        self.configdict.set("Nodes", nodename, data)
        self.sites.append((nodename, data))

    # getSite
    def getNode(self, node_name, or_none=False):
        rv = self.getNodeMap().get(node_name, None)
        if rv is None and not or_none:
            raise StandardError("Node not found: %s" % node_name)
        return rv

    # sites.
    def getNodeMap(self):
        all_nodes = {}
        for (node_name, data) in self.sites:
            data_list = data.split(',')
            assert len(data_list) == 3, "Error parsing Nodes section"

            all_nodes[node_name] = NodeInfoElement(node_name=node_name,
                                                   host=data_list[0],
                                                   catalog_dir=data_list[1],
                                                   data_dir=data_list[2])

        return all_nodes

    def getNodeList(self):
        return self.getNodeMap().values()

    # 使用getNode(nodename)进行替换
    def getsiteconfig(self, nodename):
        """
            return a dictionary of properties about the named
            node.
        """
        props = {}

        p = self.configdict.get("Nodes", nodename).split(',')
        props['host'] = p[0]
        props['catalog_base'] = p[1]
        props['data_base'] = p[2]
        props['id'] = nodename

        return props

    def setrestart(self, database, restart_policy):
        """
          set the restart policy for a given database
        """
        key = "Database:%s" % database
        self.configdict.set(key, "restartpolicy", restart_policy)

    def setcontrolmode(self, controlmode):
        """
          Set the mode that spread uses (broadcast or pt2pt)
        """
        self.configdict.set('Configuration', 'controlmode', controlmode)

    def setcontrolsubnet(self, controlsubnet):
        """
          Set the subnet that control (spread) traffic goes over
        """
        self.configdict.set('Configuration', 'controlsubnet', controlsubnet)

    def setspreadlogging(self, path):
        self.configdict.set('Configuration', 'spreadlog', path)

    def setlargecluster(self, count):
        csize = len(self.gethosts())
        if (count == 'off'):
            self.configdict.remove_option('Configuration', 'largecluster')
            count = 0
        elif (count == 'default'):
            minc = min(csize, 3)
            count = max(int(math.sqrt(csize)), minc)
        else:
            count = max(1, min(128, csize, int(count)))
        self.configdict.set('Configuration', 'largecluster', str(count))
        return count

    def save(self, nolock=False):
        """
        simple method to force the file to write itself to disk
        this method must be explictly called or else changes will
        not be saved.
        """

        Configurator.lock.acquire()
        try:
            if nolock:
                with open(self.filename, "w") as f:
                    self.configdict.write(f)
            else:
                with FileLock(ADMINTOOLS_CONF):
                    with open(self.filename, "w") as f:
                        self.configdict.write(f)
        finally:
            Configurator.lock.release()

        from mesadb.tools import DBfunctions
        DBfunctions.record("Saved %s -> %s" % (str(self), ADMINTOOLS_CONF))

    def addhost(self, host):
        """Adds a host to the cluster.  Forcibly changes to an IP address."""
        # 在admintools.conf中没有主机名称
        assert util.is_ip_address(host), \
                "All hosts must be added by IP address (%s)" % host
        if not self.incluster(host):
            self.cluster.append(host)
            self.configdict.set("Cluster", "hosts", ",".join(self.cluster))
        return host

    def add(self, database, path, port, sites, restart="ksafe"):
        """
        add a database to the configuration.
        only the initial 'startup' node needs to be
        defined -- we'll ask the catalog for the
        rest of the information on restarts.
        """
        if (not self.isdefined(database)):

            key = "Database:%s" % database
            self.configdict.add_section(key)
            self.configdict.set(key, "restartpolicy", restart)
            self.configdict.set(key, "port", "%s" % port)
            self.configdict.set(key, "path", path)
            self.configdict.set(key, "nodes", ",".join(sites))

    def update(self, database, path=None, port=None, nodes=None):
        """
        update the properties of a database.  you can supply
        a subset of the values, in which case the current values
        will continue to apply.
        """
        if not self.isdefined(database):
            return

        props = self.getconfig(database)

        d = path
        p = port
        n = ""
        if nodes != None:
            n = ",".join(nodes)
        if d == None: d = props['path']
        if p == None: p = props['port']
        if n == None: n = props['nodes']

        key = "Database:%s" % database
        self.configdict.set(key, "port", "%s" % p)
        self.configdict.set(key, "path", d)
        self.configdict.set(key, "nodes", n)

    def remove(self, name, type='database'):
        """
        remove a database or  host from the
        configuration.  defaults to databases
        set type to 'host' to remove a  host.

        WARNING: this only removes the info
        from the configuration file. 
        """

        if (type == 'database' and self.isdefined(name)):
            self.configdict.remove_section("Database:%s" % name)

        if (type == 'host'):
            self.cluster.remove(name)
            self.configdict.set("Cluster", "hosts", ",".join(self.cluster))
        if (type == 'site'):
            self.configdict.remove_option("Nodes", name)

    def remove_node_from_db(self, database, nodename):
        key = "Database:%s" % database
        nodes = self.configdict.get(key, 'nodes').split(',')
        idx = 0
        for n in nodes:
            if n == nodename:
                del (nodes[idx])
            idx += 1
        self.configdict.set(key, 'nodes', ','.join(nodes))
        self.save()

    def __str__(self):
        databases = self.listDatabases()
        return "Configurator(clusterSize=%s, dbs=(%s))" % (len(
            self.gethosts()), ','.join(databases))

    def _legacy_port(self):
        """Load the legacy configuration"""

        print("Upgrading admintools meta data format..", file=sys.stderr)
        print("scanning %s/config/users" % DBinclude.DB_DIR, file=sys.stderr)
        userdirs = []
        try:
            userdirs = os.listdir("%s/config/users" % DBinclude.DB_DIR)
        except:
            pass

        ports = self._load_dict("%s/config/share/portinfo.dat" %
                                DBinclude.DB_DIR)
        if ports is None:
            ports = {}
        self.configdict.set("Configuration", "last_port",
                            ports.get("base", "5433"))

        for dir in userdirs:
            try:
                sites = self._load_dict("%s/config/users/%s/siteinfo.dat" %
                                        (DBinclude.DB_DIR, dir))
                # if siteinfo.dat fails to load, forget about it.
                if sites is None:
                    continue

                for node in sites.values():
                    if not self.incluster(node[1]):
                        self.addhost(node[1])

                    if not self.configdict.has_section("Nodes"):
                        self.configdict.add_section("Nodes")

                    c = node[2]
                    d = node[3]
                    if c.endswith("/"):
                        c = c[:len(c) - 1]

                    if d.endswith("/"):
                        d = d[:len(d) - 1]

                    try:
                        self.configdict.set(
                            "Nodes", node[0], ','.join(
                                [socket.gethostbyname_ex(node[1])[2][0], c,
                                 d]))
                    except:
                        self.configdict.set("Nodes", node[0],
                                            ','.join([node[1], c, d]))

                shutil.move(sites, "%s.old.%s" % (sites, time.time()))
                dbinfo = self._load_dict("%s/config/users/%s/dbinfo.dat" %
                                         (DBinclude.DB_DIR, dir))

                if dbinfo is not None:
                    databases = dbinfo['defined'].keys()
                    for db in databases:
                        if not self.isdefined(db):
                            startinfo = dbinfo['startinfo'][db]
                            try:
                                host = socket.gethostbyname_ex(
                                    startinfo[6][0])[2][0]
                            except:
                                host = startinfo[6][0]
                            path = startinfo[0]
                            port = 5433

                            try:
                                port = ports[1]['assignments'][db]
                            except:
                                pass

                            policy = "never"
                            try:
                                if 'restartpolicy' in dbinfo.keys():
                                    if db in dbinfo['restartpolicy'].keys():
                                        policy = dbinfo['restartpolicy'][db]
                            except:
                                pass

                            self.add(db, path, port, dbinfo['defined'][db],
                                     policy)

                shutil.move(dbinfo, "%s.old.%s" % (dbinfo, time.time()))

            except Exception as e:
                traceback.print_exc()
                print("failed to convert meta-data for %s: %s" % (dir, str(e)),
                      file=sys.stderr)

    def _load_dict(self, fileName):
        try:
            with open(fileName, "r") as f:
                return pickle.load(f)
        except IOError:
            return None
Example #29
0
 def remove_option(self, section, option):
     ConfigParser.remove_option(self, section, option)
     self.__save()
Example #30
0
class Config(object):
    def __init__(self, inifile=None, configs=None):
        if inifile is None:
            return None
        self.inifile = inifile
        self.cfg = ConfigParser()

        with FileLock(self.inifile):
            if exists(self.inifile):
                self.cfg.read(self.inifile)

            # initialize configurations
            default_configs = {} if configs is None else configs
            needupdate = False
            for sec, secdata in default_configs.items():
                if not self.cfg.has_section(sec):
                    self.cfg.add_section(sec)
                    needupdate = True
                for opt, val in secdata.items():
                    if not self.cfg.has_option(sec, opt):
                        self.cfg.set(sec, opt, val)
                        needupdate = True

            # update ini file
            if needupdate:
                self.update(False)

    def update(self, lock=True):
        if lock:
            flock = FileLock(self.inifile)
            flock.acquire()

        try:
            inifp = open(self.inifile, 'w')
            self.cfg.write(inifp)
            inifp.close()
            if lock:
                flock.release()
            return True
        except:
            if lock:
                flock.release()
            return False

    def has_option(self, section, option):
        return self.cfg.has_option(section, option)

    def remove_option(self, section, option):
        return self.cfg.remove_option(section, option)

    def get(self, section, option):
        if self.cfg.has_option(section, option):
            return self.cfg.get(section, option)
        else:
            return None

    def getboolean(self, section, option):
        return self.cfg.getboolean(section, option)

    def getint(self, section, option):
        return self.cfg.getint(section, option)

    def has_section(self, section):
        return self.cfg.has_section(section)

    def add_section(self, section):
        return self.cfg.add_section(section)

    def remove_section(self, section=None):
        if section is None:
            return False
        else:
            return self.cfg.remove_section(section)

    def set(self, section, option, value):
        try:
            self.cfg.set(section, option, value)
        except:
            return False
        return self.update()

    def get_section_list(self):
        '''Return a list of section names, excluding [DEFAULT]'''
        return self.cfg.sections()

    def get_option_list(self, section):
        '''Return a list of option names for the given section name.'''
        return self.cfg.options(section)

    def get_config_list(self):
        '''Return a list of all config for the given config file.'''
        config_list = []
        sections = self.cfg.sections()
        for section in sections:
            sec = {'section': section, 'option': {}}
            options = self.cfg.options(section)
            for key in options:
                sec['option'][key] = self.cfg.get(section, key)
            config_list.append(sec)
        return config_list

    def get_config(self):
        '''Return a dict of all config for the given config file.'''
        config = {}
        for section in self.cfg.sections():
            config[section] = {}
            for item in self.cfg.options(section):
                config[section][item] = self.cfg.get(section, item)
        return config

    def addsection(self, section, data):
        '''add one section'''
        try:
            if not self.cfg.has_section(section):
                self.cfg.add_section(section)
            for option, value in data.items():
                self.cfg.set(section, option, value)
            return self.update(False)
        except:
            return False

    def addsections(self, section):
        '''add some sections'''
        try:
            for sec, data in section.items():
                if not self.cfg.has_section(sec):
                    self.cfg.add_section(sec)
                for option, value in data.items():
                    self.cfg.set(sec, option, value)
            return self.update(False)
        except:
            return False