summaryrefslogtreecommitdiff
path: root/morphlib/morphology.py
blob: d31a000c10947764288735054bda496f2571a4d9 (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
# Copyright (C) 2011  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; either 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import json
import logging


class SchemaError(Exception):

    pass


class Morphology(object):

    '''Represent a morphology: description of how to build binaries.'''
    
    def __init__(self, fp, baseurl=None):
        self._fp = fp
        self._baseurl = baseurl or ''
        self._load()

    def _load(self):
        logging.debug('Loading morphology %s' % self._fp.name)
        self._dict = json.load(self._fp)

        if 'name' not in self._dict:
            raise self._error('must contain "name"')
            
        if not self.name:
            raise self._error('"name" must not be empty')

        if 'kind' not in self._dict:
            raise self._error('must contain "kind"')

        if self.kind == 'chunk':
            self._validate_chunk()
            self.source['repo'] = self._join_with_baseurl(self.source['repo'])
        elif self.kind == 'stratum':
            self._validate_stratum()
            for source in self.sources.itervalues():
                source['repo'] = self._join_with_baseurl(source['repo'])
        else:
            raise self._error('kind must be chunk or stratum, not %s' %
                                self.kind)

        self.filename = self._fp.name

    def _validate_chunk(self):
        valid_toplevel_keys = ['name', 'kind', 'source', 'configure-commands',
                               'build-commands', 'test-commands',
                               'install-commands']

        if 'source' not in self._dict:
            raise self._error('chunks must have "source" field')

        if type(self.source) != dict:
            raise self._error('"source" must be a dictionary')

        if len(self.source) == 0:
            raise self._error('"source" must not be empty')
            
        if 'repo' not in self.source:
            raise self._error('"source" must contain "repo"')
            
        if not self.source['repo']:
            raise self._error('"source" must contain non-empty "repo"')
            
        if 'ref' not in self.source:
            raise self._error('"source" must contain "ref"')
            
        if not self.source['ref']:
            raise self._error('"source" must contain non-empty "ref"')

        for key in self.source.keys():
            if key not in ('repo', 'ref'):
                raise self._error('unknown key "%s" in "source"' % key)

        cmdlists = [
            (self.configure_commands, 'configure-commands'),
            (self.build_commands, 'build-commands'),
            (self.test_commands, 'test-commands'),
            (self.install_commands, 'install-commands'),
        ]
        for value, name in cmdlists:
            if type(value) != list:
                raise self._error('"%s" must be a list' % name)
            for x in value:
                if type(x) != unicode:
                    raise self._error('"%s" must contain strings' % name)

        for key in self._dict.keys():
            if key not in valid_toplevel_keys:
                raise self._error('unknown key "%s"' % key)

    def _validate_stratum(self):
        valid_toplevel_keys = ['name', 'kind', 'sources']

        if 'sources' not in self._dict:
            raise self._error('stratum must contain "sources"')

        if type(self.sources) != dict:
            raise self._error('"sources" must be a dict')

        if len(self.sources) == 0:
            raise self._error('"sources" must not be empty')

        for name, source in self.sources.iteritems():
            if type(source) != dict:
                raise self._error('"sources" must contain dicts')
            if 'repo' not in source:
                raise self._error('sources must have "repo"')
            if type(source['repo']) != unicode:
                raise self._error('"repo" must be a string')
            if not source['repo']:
                raise self._error('"repo" must be a non-empty string')
            if 'ref' not in source:
                raise self._error('sources must have "ref"')
            if type(source['ref']) != unicode:
                raise self._error('"ref" must be a string')
            if not source['ref']:
                raise self._error('"ref" must be a non-empty string')
            for key in source:
                if key not in ('repo', 'ref'):
                    raise self._error('unknown key "%s" in sources' % key)

        for key in self._dict.keys():
            if key not in valid_toplevel_keys:
                raise self._error('unknown key "%s"' % key)

    @property
    def name(self):
        return self._dict['name']

    @property
    def kind(self):
        return self._dict['kind']

    @property
    def source(self):
        return self._dict['source']

    @property
    def sources(self):
        return self._dict['sources']

    @property
    def configure_commands(self):
        return self._dict.get('configure-commands', [])

    @property
    def build_commands(self):
        return self._dict.get('build-commands', [])

    @property
    def test_commands(self):
        return self._dict.get('test-commands', [])

    @property
    def install_commands(self):
        return self._dict.get('install-commands', [])

    @property
    def manifest(self):
        if self.kind == 'chunk':
            return [(self.source['repo'], self.source['ref'])]
        else:
            return [(source['repo'], source['ref'])
                     for source in self.sources.itervalues()]

    def _join_with_baseurl(self, url):
        is_relative = (':' not in url or
                       '/' not in url or
                       url.find('/') < url.find(':'))
        if is_relative:
            if not url.endswith('/'):
                url += '/'
            return self._baseurl + url
        else:
            return url

    def _error(self, msg):
        return SchemaError('Morphology %s: %s' % (self._fp.name, msg))