summaryrefslogtreecommitdiff
path: root/ybd/defaults.py
blob: a8f218432e474cd88b8a9556c70489fb8c3c8637 (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
#!/usr/bin/env python
# Copyright (C) 2015-2016  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


'''Defaults to be used for older Baserock Definitions.

Definitions version 7 adds a file named DEFAULTS which sets the default
build commands and default split rules for the set of definitions in that
repo.

These definitions shall be used if no DEFAULTS file is present.

'''

import os
import app
import yaml


class Defaults(object):

    def __init__(self):
        defaults = self._load_defaults()
        self.build_steps = defaults.get('build-steps', {})
        self.fields = defaults.get('morph-fields', {})
        self.build_systems = defaults.get('build-systems', {})
        self.split_rules = defaults.get('split-rules', {})
        self.cpus = defaults.get('cpus', {})

    def _load_defaults(self, defaults_file='./DEFAULTS'):
        '''Get defaults, either from a DEFAULTS file, or built-in defaults.'''
        ybd_defaults_file = os.path.join(os.path.dirname(__file__),
                                         app.config['defaults'])
        ybd_defaults = self._load(ybd_defaults_file, ignore_errors=True)

        defaults = self._load(defaults_file, ignore_errors=True)
        if defaults is None:
            return ybd_defaults

        for index in ybd_defaults:
            if defaults.get(index) is None or defaults.get(index) == {}:
                defaults[index] = ybd_defaults[index]

        return defaults

    def _load(self, path, ignore_errors=True):
        try:
            with open(path) as f:
                contents = yaml.safe_load(f)
        except:
            if ignore_errors:
                app.log('DEFAULTS', 'WARNING: problem loading', path)
                return None
            else:
                raise
        contents['path'] = path[2:]
        return contents

    def get_split_rules(self, kind):
        return self.split_rules.get(kind, {})

    def lookup_build_system(self, name, default=None):
        '''Return build system that corresponds to the name.

        If the name does not match any build system, raise ``KeyError``.
        '''

        if name in self.build_systems:
            return self.build_systems[name]
        elif default:
            return default

        raise KeyError("Undefined build-system %s" % name)

    def detect_build_system(self, file_list):
        '''Automatically detect the build system, if possible.'''

        for build_system in sorted(self.build_systems):
            indicators = self.build_systems[build_system].get('indicators', [])
            if any(x in file_list for x in indicators):
                return build_system

        for build_system in sorted(self.build_systems):
            indicators = self.build_systems[build_system].get('indicators', [])
            for indicator in indicators:
                if any(x.endswith(indicator) for x in file_list):
                    return build_system

        return 'manual'