-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.py
183 lines (161 loc) · 6.78 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os
import re
from dlrn.utils import import_class
_config_options = None
def _default_datadir():
return os.path.realpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../data"))
def _default_scriptsdir():
return os.path.realpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../scripts"))
def _default_templatedir():
return os.path.join(
os.path.dirname(os.path.abspath(__file__)), "templates")
DLRN_CORE_CONFIG = {
'DEFAULT': {
'tags': {},
'datadir': {'default': _default_datadir()},
'gerrit': {},
'maxretries': {'type': 'int', 'default': 3},
'baseurl': {},
'smtpserver': {},
'distro': {},
'source': {},
'target': {},
'reponame': {},
'rsyncdest': {'default': ''},
'rsyncport': {'default': 22},
'scriptsdir': {'default': _default_scriptsdir()},
'configdir': {},
'templatedir': {'default': _default_templatedir()},
'project_name': {'default': 'RDO'},
'pkginfo_driver': {'default': 'dlrn.drivers.rdoinfo.RdoInfoDriver'},
'build_driver': {'default': 'dlrn.drivers.mockdriver.MockBuildDriver'},
'workers': {'type': 'int', 'default': 1},
'gerrit_topic': {'default': 'rdo-FTBFS'},
'database_connection': {'default': 'sqlite:///commits.sqlite'},
'fallback_to_master': {'type': 'boolean', 'default': True},
'nonfallback_branches': {'type': 'list',
'default': ['^master$', '^rpm-master$']},
'release_numbering': {'default': '0.date.hash'},
'release_minor': {'default': '0'},
'custom_preprocess': {'type': 'list'},
'include_srpm_in_repo': {'type': 'boolean', 'default': True},
'keep_changelog': {'type': 'boolean', 'default': False},
'allow_force_rechecks': {'type': 'boolean', 'default': False},
'use_components': {'type': 'boolean', 'default': False},
'deps_url': {'default': ''},
}
}
def setup_logging(debug=False, filename=None):
logging.basicConfig(
level=logging.DEBUG if debug else logging.INFO,
filename=filename,
format='%(asctime)s %(levelname)s:%(name)s:%(message)s')
# Silence sh module logs
logging.getLogger("sh.command").setLevel(logging.CRITICAL)
class ConfigOptions(object):
def __init__(self, cp, overrides=None):
self.parse_overrides(cp, overrides)
self.parse_config(DLRN_CORE_CONFIG, cp)
# dynamic directory defaults
if not self.configdir:
self.configdir = self.scriptsdir
if not self.templatedir:
self.templatedir = \
os.path.join(os.path.dirname(os.path.realpath(__file__)),
"templates")
# handling for optional sections, driver-based
drivers = [
self.pkginfo_driver,
self.build_driver,
]
for d in drivers:
# import driver specific config options
driver = import_class(d)
try:
driver_cfg = driver.DRIVER_CONFIG
except AttributeError:
driver_cfg = None
if driver_cfg:
self.parse_config(driver_cfg, cp)
global _config_options
_config_options = self
def parse_overrides(self, config_parser, overrides):
if overrides is None:
return
# Now check for any potential overrides
for rule in overrides:
rule_match = re.match(r'^(\w+)\.(\w+)=(\w+)$', rule)
if rule_match is not None:
section = rule_match.group(1)
key = rule_match.group(2)
value = rule_match.group(3)
if section == 'DEFAULT' or config_parser.has_section(section):
if config_parser.has_option(section, key):
logging.info("Overriding option %s.%s with value %s" %
(section, key, value))
config_parser.set(section, key, value)
else:
logging.error("Option %s.%s is not present in the"
"configuration file." % (section, key))
raise RuntimeError("Unknown config option %s.%s" %
(section, key))
def parse_config(self, config_rules, config_parser):
for section, rules in config_rules.items():
if section == 'DEFAULT' or config_parser.has_section(section):
for option, rule in rules.items():
ini_option = rule.get('name', option)
if config_parser.has_option(section, ini_option):
_type = rule.get('type', '')
if _type == 'list' or _type == 'str':
cp_get_method_name = 'get'
else:
cp_get_method_name = 'get' + _type
cp_get_method = getattr(config_parser,
cp_get_method_name)
val = cp_get_method(section, ini_option)
if _type == 'list':
# comma separated list
val = val.split(',')
setattr(self, option, val)
else:
self.set_default(option, rule)
else:
# section is missing, fill in defaults
for option, rule in rules.items():
self.set_default(option, rule)
def set_default(self, option, rule):
if rule.get('ignore_missing'):
# ignore_missing prevents setting default value
return
if 'default' in rule:
val = rule['default']
else:
defaults = {
'boolean': False,
'int': 0,
'str': '',
'list': [],
}
val = None
_type = rule.get('type')
if _type and _type in defaults:
val = defaults[_type]
setattr(self, option, val)
def getConfigOptions():
return _config_options