summaryrefslogtreecommitdiff
path: root/morphlib/artifactresolver.py
blob: 929dae57b16b1af3d597c5f64a786bae01e90de2 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright (C) 2012  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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import cliapp
import collections

import morphlib


class MutualDependencyError(cliapp.AppException):

    def __init__(self, a, b):
        cliapp.AppException.__init__(
            self, 'Cyclic dependency between %s and %s detected' % (a, b))


class DependencyOrderError(cliapp.AppException):

    def __init__(self, stratum, chunk, dependency_name):
        cliapp.AppException.__init__(
            self, 'In stratum %s, chunk %s references its dependency %s '
            'before it is defined' %
            (stratum.source, chunk, dependency_name))


class DependencyFormatError(cliapp.AppException):

    def __init__(self, stratum, chunk):
        cliapp.AppException.__init__(
            self, 'In stratum %s, chunk %s uses an invalid '
            'build-depends format' % (stratum.source, chunk))


class UndefinedChunkArtifactError(cliapp.AppException):

    '''Exception raised when non-existent artifacts are referenced.

    Usually, this will only occur when a stratum refers to a chunk
    artifact that is not defined in a chunk.

    '''

    def __init__(self, parent, reference):
        cliapp.AppException.__init__(
            self, 'Undefined chunk artifact "%s" referenced in '
            'stratum %s' % (reference, parent))


class ArtifactResolver(object):

    '''Resolves sources into artifacts that would be build from the sources.

    This class takes a CacheKeyComputer and a SourcePool, analyses the
    sources and their dependencies and creates a list of artifacts
    (represented by Artifact objects) that are involved in building the
    sources in the pool.

    '''

    def __init__(self):
        self._cached_artifacts = None
        self._added_artifacts = None
        self._source_pool = None

    def resolve_artifacts(self, source_pool):
        self._source_pool = source_pool
        self._cached_artifacts = {}
        self._added_artifacts = set()

        artifacts = self._resolve_artifacts_recursively()
        # TODO perform cycle detection, e.g. based on:
        # http://stackoverflow.com/questions/546655/finding-all-cycles-in-graph
        return artifacts

    def _resolve_artifacts_recursively(self):
        artifacts = []

        queue = self._create_initial_queue()
        while queue:
            source = queue.popleft()

            if source.morphology['kind'] == 'system':
                systems = [self._get_artifact(source, a)
                           for a in source.morphology.builds_artifacts]

                if any(a not in self._added_artifacts for a in systems):
                    artifacts.extend(systems)
                    self._added_artifacts.update(systems)

                resolved_artifacts = self._resolve_system_dependencies(
                    systems, source, queue)

                for artifact in resolved_artifacts:
                    if not artifact in self._added_artifacts:
                        artifacts.append(artifact)
                        self._added_artifacts.add(artifact)
            elif source.morphology['kind'] == 'stratum':
                assert len(source.morphology.builds_artifacts) == 1
                artifact = self._get_artifact(
                    source, source.morphology.builds_artifacts[0])

                if not artifact in self._added_artifacts:
                    artifacts.append(artifact)
                    self._added_artifacts.add(artifact)

                resolved_artifacts = self._resolve_stratum_dependencies(
                    artifact, queue)

                for artifact in resolved_artifacts:
                    if not artifact in self._added_artifacts:
                        artifacts.append(artifact)
                        self._added_artifacts.add(artifact)
            elif source.morphology['kind'] == 'chunk':
                names = source.morphology.builds_artifacts
                for name in names:
                    artifact = self._get_artifact(source, name)
                    if not artifact in self._added_artifacts:
                        artifacts.append(artifact)
                        self._added_artifacts.add(artifact)

        return artifacts

    def _create_initial_queue(self):
        if all([x.morphology['kind'] == 'chunk' for x in self._source_pool]):
            return collections.deque(self._source_pool)
        else:
            sources = [x for x in self._source_pool
                       if x.morphology['kind'] != 'chunk']
            return collections.deque(sources)

    def _get_artifact(self, source, name):
        info = (source, name)
        if info in self._cached_artifacts:
            return self._cached_artifacts[info]
        else:
            artifact = morphlib.artifact.Artifact(info[0], info[1])
            self._cached_artifacts[info] = artifact
            return artifact

    def _resolve_system_dependencies(self, systems, source, queue):
        artifacts = []

        for stratum_name in source.morphology['strata']:
            stratum_source = self._source_pool.lookup(
                source.repo_name,
                source.original_ref,
                '%s.morph' % stratum_name)

            stratum = self._get_artifact(stratum_source, stratum_name)

            for system in systems:
                system.add_dependency(stratum)
            queue.append(stratum_source)

            artifacts.append(stratum)

        return artifacts

    def _resolve_stratum_dependencies(self, stratum, queue):
        artifacts = []

        strata = []

        if stratum.source.morphology['build-depends']:
            for stratum_name in stratum.source.morphology['build-depends']:
                other_source = self._source_pool.lookup(
                    stratum.source.repo_name,
                    stratum.source.original_ref,
                    '%s.morph' % stratum_name)

                other_stratum = self._get_artifact(other_source, stratum_name)

                strata.append(other_stratum)

                artifacts.append(other_stratum)

                if other_stratum.depends_on(stratum):
                    raise MutualDependencyError(stratum, other_stratum)

                stratum.add_dependency(other_stratum)
                queue.append(other_source)

        chunk_artifacts = []
        processed_artifacts = []
        name_to_processed_artifact = {}

        for info in stratum.source.morphology['sources']:
            chunk_source = self._source_pool.lookup(
                info['repo'],
                info['ref'],
                '%s.morph' % info['morph'])

            possible_names = chunk_source.morphology.builds_artifacts
            if not info['name'] in possible_names:
                raise UndefinedChunkArtifactError(stratum.source, info['name'])

            chunk_artifact = self._get_artifact(chunk_source, info['name'])
            chunk_artifacts.append(chunk_artifact)

            artifacts.append(chunk_artifact)

            stratum.add_dependency(chunk_artifact)

            for other_stratum in strata:
                chunk_artifact.add_dependency(other_stratum)

            build_depends = info.get('build-depends', None)

            if build_depends is None:
                for earlier_artifact in processed_artifacts:
                    if earlier_artifact is chunk_artifact:
                        continue
                    if earlier_artifact.depends_on(chunk_artifact):
                        raise MutualDependencyError(
                            chunk_artifact, earlier_artifact)
                    chunk_artifact.add_dependency(earlier_artifact)
            elif isinstance(build_depends, list):
                for name in build_depends:
                    other_artifact = name_to_processed_artifact.get(name, None)
                    if other_artifact is chunk_artifact:
                        continue
                    if other_artifact:
                        chunk_artifact.add_dependency(other_artifact)
                    else:
                        raise DependencyOrderError(
                            stratum, info['name'], name)
            else:
                raise DependencyFormatError(stratum, info['name'])
            processed_artifacts.append(chunk_artifact)
            name_to_processed_artifact[info['name']] = chunk_artifact

        return artifacts