3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 Read sections from a build configuration file and check that
17 all required values have been given.
23 from rpmbuilder.baseerror import RpmbuilderError
26 class Configfilereader(object):
28 """ Reading and processing of user given configuration file """
30 def __init__(self, configfile):
31 self.logger = logging.getLogger(__name__)
32 self.configfile = configfile
33 self.configuration = self.readconfig(configfile)
35 def readconfig(self, configfile):
36 """ Configuration file reading """
37 conf = ConfigParser.ConfigParser()
39 with open(configfile) as filep:
42 raise ConfigError("Failed to open configuration file %s" % configfile)
44 self.__validate_section_names(conf)
47 def get_bool(self, section, option, mandatory=False, defaultvalue=False):
48 """ Get boolean values from configuration. In case of problems do raise
49 or just return default value """
51 return self.configuration.getboolean(section, option)
52 except ConfigParser.NoSectionError:
53 raise ConfigError("Could not find required [%s] section in configuration" % section)
54 except ConfigParser.NoOptionError:
56 raise ConfigError("Could not find option %s from [%s] section" % option, section)
62 def get_string(self, section, option, mandatory=False, defaultvalue=None):
63 """ Return the requested value from the given section. In case of problems
64 do raise or just return default value"""
66 return self.configuration.get(section, option)
67 except ConfigParser.NoSectionError:
68 raise ConfigError("Could not find required [%s] section in configuration" % section)
69 except ConfigParser.NoOptionError:
71 raise ConfigError("Could not find option %s from [%s] section" % option, section)
77 def get_sections(self):
78 """ List all sections from the configuration """
80 return self.configuration.sections()
84 def __validate_section_names(self, configuration):
85 """ Loop through all section names and do validation """
86 for section in configuration.sections():
87 self.__validate_section_name(section)
89 def __validate_section_name(self, name):
90 """ Check that section contains characters that
91 do not cause problems for directory names """
92 if not re.match('^[A-Za-z0-9-]+$', name):
93 self.logger.critical("Configuration of [%s] has problems.", name,
94 "Section name can has illegal characters"
95 "Use only alphanumeric and dashes")
96 raise ConfigError("Section %s name contains illegal characters" % name)
99 class ConfigError(RpmbuilderError):
101 """ Exception for configuration file content problems """