fix building of forked upstream packages
[ta/rpmbuilder.git] / rpmbuilder / configfile.py
1 # Copyright 2019 Nokia
2 #
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 """
16 Read sections from a build configuration file and check that
17 all required values have been given.
18 """
19 import ConfigParser
20 import logging
21 import re
22
23 from rpmbuilder.baseerror import RpmbuilderError
24
25
26 class Configfilereader(object):
27
28     """ Reading and processing of user given configuration file """
29
30     def __init__(self, configfile):
31         self.logger = logging.getLogger(__name__)
32         self.configfile = configfile
33         self.configuration = self.readconfig(configfile)
34
35     def readconfig(self, configfile):
36         """ Configuration file reading """
37         conf = ConfigParser.ConfigParser()
38         try:
39             with open(configfile) as filep:
40                 conf.readfp(filep)
41         except IOError:
42             raise ConfigError("Failed to open configuration file %s" % configfile)
43
44         self.__validate_section_names(conf)
45         return conf
46
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 """
50         try:
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:
55             if mandatory:
56                 raise ConfigError("Could not find option %s from [%s] section" % option, section)
57             else:
58                 return defaultvalue
59         except:
60             raise
61
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"""
65         try:
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:
70             if mandatory:
71                 raise ConfigError("Could not find option %s from [%s] section" % option, section)
72             else:
73                 return defaultvalue
74         except:
75             raise
76
77     def get_sections(self):
78         """ List all sections from the configuration """
79         try:
80             return self.configuration.sections()
81         except:
82             raise
83
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)
88
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)
97
98
99 class ConfigError(RpmbuilderError):
100
101     """ Exception for configuration file content problems """
102     pass