示例#1
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info('Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = dict()
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value) 
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value 
        
        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error('Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
示例#2
0
def save_env_option_setting_file(location, option_settings):
    log.info(u'Try loading option settings file from {0}'.format(location))
#    old_option_settings = load_env_option_setting_file(location, quiet = True)
    
    log.info(u'Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.iteritems()):
                if not _is_whitelisted_option(namespace, option):
#                    and (namespace not in old_option_settings \
#                         or option not in old_option_settings[namespace]):
                    continue
                
                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                # Add option setting to new file
                if value is None:
                    value = u''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error(u'Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
def save_env_option_setting_file(location, option_settings):
    log.info(u'Try loading option settings file from {0}'.format(location))
#    old_option_settings = load_env_option_setting_file(location, quiet = True)
    
    log.info(u'Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.iteritems()):
                if not _is_whitelisted_option(namespace, option):
#                    and (namespace not in old_option_settings \
#                         or option not in old_option_settings[namespace]):
                    continue
                
                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                # Add option setting to new file
                if value is None:
                    value = u''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error(u'Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
示例#4
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info(u'Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = []
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                cos = ConfigurationOptionSetting()
                cos._namespace = misc.to_unicode(section)
                cos._option_name = misc.to_unicode(option)
                cos._value = misc.to_unicode(value)
                option_settings.append(cos) 
        
        log.debug(u'Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error(u'Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return []
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
示例#5
0
def save_env_option_setting_file(location, option_settings):
    log.info(u'Writing environment option settings to file at "{0}".'.format(
        location))
    try:
        parser = SectionedConfigParser()

        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue

            for option, value in sorted(options.iteritems()):

                if namespace.startswith(OptionSettingContainerPrefix):
                    pass
                elif namespace.startswith(
                        OptionSettingApplicationEnvironment.Namespace):
                    if option in OptionSettingApplicationEnvironment.IgnoreOptionNames:
                        continue
                    else:
                        pass
                # Skip if option setting is on in local option setting list
                elif namespace not in LocalOptionSettings \
                    or option not in LocalOptionSettings[namespace]:
                    continue

                if not parser.has_section(namespace):
                    parser.add_section(namespace)

                if value is None:
                    value = u''
                parser.set(namespace, option, value)

        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        set_access_permission(location, True)
    except BaseException as ex:
        log.error(
            u'Failed to save environment option setting file, because: "{0}"'.
            format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))
        raise
示例#6
0
def save_env_option_setting_file(location, option_settings):
    log.info('Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.items()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.items()):

                if namespace.startswith(OptionSettingContainerPrefix):
                    pass
                elif namespace.startswith(OptionSettingApplicationEnvironment.Namespace):
                    if option in OptionSettingApplicationEnvironment.IgnoreOptionNames:
                        continue
                    else:
                        pass
                # Skip if option setting is on in local option setting list
                elif namespace not in LocalOptionSettings \
                    or option not in LocalOptionSettings[namespace]:
                    continue

                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                if value is None:
                    value = ''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug('Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error('Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
        
示例#7
0
def load_env_option_setting_file(location, option_settings=None, quiet=False):
    log.info('Reading environment option settings from file at "{0}".'.format(
        location))

    if option_settings is None:
        option_settings = dict()

    try:
        parser = SectionedConfigParser()
        parser.read(location)

        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value)
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value

        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        check_access_permission(location, True)
        return option_settings

    except BaseException as ex:
        log.error(
            'Failed to load environment option setting file, because: "{0}"'.
            format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(
                OptionSettingFileErrorMessage.ReadError.format(location))
            raise
示例#8
0
def save_eb_config_file(location, parameter_pool, quiet = False):
    log.info(u'Writing EB configuration to file: "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        parser.add_section(EbConfigFile.RootSectionName)    # Make root section the first

        # add known session
        for section_name, (condition, keys) in EbConfigFile.KnownSections.iteritems():
            if parameter_pool.has(condition) and parameter_pool.get_value(condition):
                log.debug(u'Create section "{0}" in config file.'.format(section_name))
                parser.add_section(section_name)
                for key in sorted(keys):
                    if parameter_pool.has(key):
                        value = parameter_pool.get_value(key)
                        if key in ConfigFileParameters:
                            _, to_file = ConfigFileParameters[key]
                            if to_file is not None:
                                value = to_file(value)
                        parser.set(section_name, key, value)

        # add branch mapping sections
        if parameter_pool.has(ParameterName.BranchMapping)\
            and len(parameter_pool.get_value(ParameterName.BranchMapping)) > 0:
            log.debug(u'Create section "{0}" in config file.'.format(EbConfigFile.BranchSectionName))
            parser.add_section(EbConfigFile.BranchSectionName)
            branch_map = parameter_pool.get_value(ParameterName.BranchMapping)
            for key in sorted(branch_map.keys()):
                parser.set(EbConfigFile.BranchSectionName, key, branch_map[key])

        # add branch environment sections
        if parameter_pool.has(ParameterName.Branches)\
            and len(parameter_pool.get_value(ParameterName.Branches)) > 0:
            branches = parameter_pool.get_value(ParameterName.Branches)
            for branch_name in sorted(branches.keys()):
                section_name = EbConfigFile.BranchSectionPrefix + branch_name
                log.debug(u'Create section "{0}" in config file.'.format(section_name))
                parser.add_section(section_name)
                branch = branches[branch_name]
                for key in sorted(EbConfigFile.BranchSectionKeys):
                    if key in branch:
                        value = branch[key]
                        if key in ConfigFileParameters:
                            _, to_file = ConfigFileParameters[key]
                            if to_file is not None:
                                value = to_file(value)
                        parser.set(section_name, key, value)
            
        # add else
        if parameter_pool.has(ParameterName.ConfigFileExtra): 
            extra_config = parameter_pool.get_value(ParameterName.ConfigFileExtra) 
            for section, pairs in sorted(extra_config.iteritems(), key=operator.itemgetter(0)):
                if not parser.has_section(section):
                    log.debug(u'Create section "{0}" in config file.'.format(section))
                    parser.add_section(section)
                for key, value in pairs.iteritems():
                    parser.set(section, key, value)

        parser.write(location)
        log.info(u'Finished writing EB configuration file.')
        
    except BaseException as ex:
        log.error(u'Failed to save EB configuration file, because: "{0}"'.format(ex))
        prompt.error(ConfigFileErrorMessage.WriteError.format(location))        
        raise
示例#9
0
def load_eb_config_file(location, parameter_pool, quiet = False):
    log.info(u'Reading EB configuration from file: "{0}"'.format(location))
    try:
        try: 
            parser = SectionedConfigParser()
            parser.read(location)

            log.debug(u'Found a sectioned config file.')
            extra_config = dict()
            extra_config[EbConfigFile.RootSectionName] = dict()
            branches = dict()
            
            for section in parser.sections():
                log.debug(u'Reading section "{0}" from config file.'.format(section))                
                # Known sections
                if section in EbConfigFile.KnownSections.keys(): 
                    for key, value in parser.items(section):
                        if ConfigFileParameters.has_key(key):
                            from_file, _ = ConfigFileParameters[key]
                            if from_file:
                                value = from_file(value)
                            parameter_pool.put(Parameter(key, value, ParameterSource.ConfigFile))
                        else:
                            extra_config[EbConfigFile.RootSectionName][key] = value
                            
                elif section == EbConfigFile.BranchSectionName:
                    #branch section
                    branch_mapping = dict()
                    for key, value in parser.items(section):
                        branch_mapping[key] = value
                    parameter_pool.put(Parameter(ParameterName.BranchMapping, 
                                                 branch_mapping, 
                                                 ParameterSource.ConfigFile))
                    
                elif section.startswith(EbConfigFile.BranchSectionPrefix):
                    #branch environment session
                    parsed = section.split(EbConfigFile.SectionNameDelimiter)
                    if len(parsed) != 2 or misc.is_blank_string(parsed[1]):
                        continue    # skip if no environment name
                    branch_name = parsed[1]
                    branches[branch_name] = dict()
                    for key, value in parser.items(section):
                        if ConfigFileParameters.has_key(key):
                            from_file, _ = ConfigFileParameters[key]
                            if from_file:
                                value = from_file(value)
                            branches[branch_name][key] = value
                        else:
                            if not extra_config.has_key(section):
                                extra_config[section] = dict()
                            extra_config[section][key] = value
                
                else:
                    # unknown sections
                    new_section = dict()
                    for key, value in parser.items(section):
                        new_section[key] = value 
                    extra_config[section] = new_section

            parameter_pool.put(Parameter(ParameterName.ConfigFileExtra, 
                                         extra_config, 
                                         ParameterSource.ConfigFile))
            parameter_pool.put(Parameter(ParameterName.Branches, 
                                         branches, 
                                         ParameterSource.ConfigFile))     
            
        except MissingSectionHeaderError as ex:
            # old format: sectionless config file
            log.debug(u'Found a section-less config file.')
            nosect_parser = NoSectionConfigParser()
            nosect_parser.read(location)

            for name, (from_file, _) in ConfigFileParameters.iteritems():
                if nosect_parser.has_option(name):
                    value = nosect_parser.get(name)
                    if from_file is not None:
                        value = from_file(value)
                    parameter_pool.put(Parameter(name, value, ParameterSource.ConfigFile))
        
        # Add original parameter infos
        for name, ori_name in EbConfigFile.BranchResetParameters.iteritems():
            if parameter_pool.has(name):
                parameter_pool.put(Parameter(ori_name, 
                                             parameter_pool.get_value(name), 
                                             ParameterSource.ConfigFile))

        log.info(u'Finished reading from EB configuration file.')
   
    except BaseException as ex:
        log.error(u'Failed to parse EB configuration from file, because: "{0}"'.format(ex))
        if not quiet:
            if (isinstance(ex, OSError) or isinstance(ex, IOError)) and\
                ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                raise EBConfigFileNotExistError(ex)
            else:
                msg = ConfigFileErrorMessage.ReadError.format(location)
                prompt.error(msg)
                raise EBConfigFileNotExistError(msg)
        else:    
            pass # if failed, just skip