summaryrefslogtreecommitdiff
path: root/pkg/configs.py
blob: 5b499db5a758f95f37d34b443c5de67b83852f63 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# SPDX-License-Identifier: BSD-2-Clause
"""
RSB Deployment Configurations
"""

#
# Copyright 2022 Chris Johns (chris@contemporary.software)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

import re
import os
import sys

from waflib import Context

path = 'config'


def init(ctx):
    pass


def options(opt):
    opt.add_option('--builds',
                   default=None,
                   dest='builds',
                   help='Regex filter of buildset to build')


def configure(conf):
    if conf.options.builds is not None:
        try:
            bf = re.compile(conf.options.builds)
            build_filter = conf.options.builds
            bf = 'yes'
        except:
            conf.fatal('Builds filter regex invalid')
    else:
        build_filter = None
        bf = 'no'
    conf.env.BUILD_FILTER = build_filter
    conf.msg('Buildset filter', bf, color='GREEN')


def get_config_parser():
    try:
        import configparser
        config = configparser.ConfigParser(strict=False, interpolation=None)
    except:
        # python2
        import ConfigParser as configparser
        config = configparser.ConfigParser(raw=True)
    return config


def get_config_error():
    try:
        import configparser
    except:
        # python2
        import ConfigParser as configparser
    return configparser.Error


def config_path(config):
    return os.path.join(path, config + '.bset')


def config_dir(config):
    return os.path.dirname(config_path(config))


def add_wscript_fun(ctx, fun_name, fun_func):
    node = ctx.path.find_node(Context.WSCRIPT_FILE)
    if node:
        wscript_module = Context.load_module(node.abspath())
        setattr(wscript_module, fun_name, fun_func)


def configs_ini_load(bld, inis, configs):

    def parse_types(bld, value):
        vs = value.lower().strip().split(' ')
        if len(vs) == 1:
            if vs[0] == 'true':
                return True
            if vs[0] == 'false':
                return False
            return None
        if vs[0] == 'version':
            if len(vs) != 3:
                return None
            try:
                rev = int(bld.env.RSB_VERSION)
            except:
                bld.fatal('cannot convert RSB version to a number: ' +
                          bld.env.RSB_VERSION)
            try:
                val = int(vs[2])
            except:
                return None
            if vs[1] == '==':
                return rev == val
            if vs[1] == '!=':
                return rev != val
            if vs[1] == '>':
                return rev > val
            if vs[1] == '<':
                return rev < val
            if vs[1] == '>=':
                return rev >= val
            if vs[1] == '<=':
                return rev <= val
            return None
        return None

    for ini in inis:
        config = get_config_parser()
        try:
            config.read(ini)
        except pkg.configs.get_config_error() as ce:
            bld.fatal('configs ini parse error: ' + str(ce))
        ini_dir = os.path.dirname(os.path.abspath(ini))
        for d in config.defaults():
            i = (d, config.defaults()[d])
            val = parse_types(bld, i[1])
            if val is None:
                bld.fatal('invalid configs item in ' + ini + ': defaults: ' +
                          i[0] + ' = ' + i[1])
            for c in configs:
                if ini_dir == os.path.abspath(config_dir(c['buildset'])):
                    c[d] = val
        for section in config.sections():
            for c in configs:
                if section == os.path.basename(c['buildset']):
                    try:
                        items = config.items(section)
                    except pkg.configs.get_config_error() as ce:
                        bld.fatal('configs ini parse error: ' + str(ce))
                    for i in items:
                        val = parse_types(bld, i[1])
                        if val is None:
                            bld.fatal('invalid configs item in ' + ini + ': ' +
                                      section + ': ' + i[0] + ' = ' + i[1])
                        c[i[0]] = val


def find_buildsets(bld):
    discovered = []
    inis = []
    for root, dirs, files in os.walk(path):
        base = root[len('config') + 1:]
        for f in files:
            r, e = os.path.splitext(f)
            if e == '.bset':
                discovered += [os.path.join(base, r)]
            elif f == 'configs.ini':
                inis += [os.path.join(root, f)]
    bs = [{
        'buildset': b,
        'enabled': True,
        'good': True,
        'dry-run': False
    } for b in discovered]
    if len(bld.env.BUILD_FILTER) > 0:
        bf = re.compile(bld.env.BUILD_FILTER)
        bs = [b for b in bs if bf.match(b['buildset'])]
    configs_ini_load(bld, inis, bs)
    return sorted([b for b in bs if b['enabled']],
                  key=lambda bs: bs['buildset'])


def buildset(bld, build, dry_run):
    name = os.path.basename(build['buildset'])
    log = bld.path.get_bld().find_or_declare(build['buildset'] + '.txt')
    config = config_path(build['buildset'])
    bset = bld.path.find_resource(config)
    if buildset is None:
        bld.fatal('buildset not found: ' + build['buildset'])
    tardir = bld.path.make_node('tar')
    tar = tardir.make_node(os.path.basename(build['buildset']) + '.tar.bz2')
    cmd = bld.env.RSB_SET_BUILDER
    opts = [
        '--prefix=' + bld.env.PREFIX, '--bset-tar-file', '--trace',
        '--log=' + str(log.path_from(bld.path))
    ]
    opts_extra = []
    if bld.env.NO_INSTALL:
        opts_extra += ['--no-install']
    if build['dry-run'] or dry_run:
        opts_extra += ['--dry-run']
    run_opts = opts + opts_extra + [build['buildset']]
    pkg_opts = opts + ['--no-install', build['buildset']]
    return {
        'name': name,
        'config': config,
        'buildset': bset,
        'log': log,
        'tardir': tardir,
        'tar': tar,
        'dry-run': build['dry-run'] or dry_run,
        'cmd': cmd,
        'opts': opts,
        'opts-extra': opts_extra,
        'run-opts': run_opts,
        'pkg-opts': pkg_opts
    }