summaryrefslogtreecommitdiff
path: root/distbuild/serialise.py
blob: a27a526b35ba0b3363d8b0dcb41494119e1575ee (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
# distbuild/serialise.py -- (de)serialise Artifact object graphs
#
# Copyright (C) 2012, 2014  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 json
import yaml

import morphlib
import logging


def serialise_artifact(artifact):
    '''Serialise an Artifact object and its dependencies into string form.'''

    def encode_morphology(morphology):
        result = {}
        for key in morphology.keys():
            result[key] = morphology[key]
        return result
    
    def encode_source(source):
        source_dic = {
            'name': source.name,
            'repo': None,
            'repo_name': source.repo_name,
            'original_ref': source.original_ref,
            'sha1': source.sha1,
            'tree': source.tree,
            'morphology': id(source.morphology),
            'filename': source.filename,

            'artifact_ids': [id(artifact) for (_, artifact)
                in source.artifacts.iteritems()],
            'cache_id': source.cache_id,
            'cache_key': source.cache_key,
            'dependencies': [id(d)
                for d in source.dependencies],
        }

        if source.morphology['kind'] == 'chunk':
            source_dic['build_mode'] = source.build_mode
            source_dic['prefix'] = source.prefix
        return source_dic

    def encode_artifact(a):
        if artifact.source.morphology['kind'] == 'system': # pragma: no cover
            arch = artifact.source.morphology['arch']
        else:
            arch = artifact.arch

        return {
            'source_id': id(a.source),
            'name': a.name,
            'arch': arch
        }

    encoded_artifacts = {}
    encoded_sources = {}
    encoded_morphologies = {}

    for a in artifact.walk():
        if id(a.source) not in encoded_sources:
            for (_, sa) in a.source.artifacts.iteritems():
                if id(sa) not in encoded_artifacts:
                    encoded_artifacts[id(sa)] = encode_artifact(sa)
            encoded_morphologies[id(a.source.morphology)] = \
                encode_morphology(a.source.morphology)
            encoded_sources[id(a.source)] = encode_source(a.source)

        if id(a) not in encoded_artifacts: # pragma: no cover
            encoded_artifacts[id(a)] = encode_artifact(a)

    content = {
        'sources': encoded_sources,
        'artifacts': encoded_artifacts,
        'morphologies': encoded_morphologies,
        'root_artifact': id(artifact),
        'default_split_rules': {
            'chunk': morphlib.artifactsplitrule.DEFAULT_CHUNK_RULES,
            'stratum': morphlib.artifactsplitrule.DEFAULT_STRATUM_RULES,
        },
    }
    return json.dumps(yaml.dump(content))


def deserialise_artifact(encoded):
    '''Re-construct the Artifact object (and dependencies).
    
    The argument should be a string returned by ``serialise_artifact``.
    The reconstructed Artifact objects will be sufficiently like the
    originals that they can be used as a build graph, and other such
    purposes, by Morph.
    
    '''

    def decode_morphology(le_dict):
        '''Convert a dict into something that kinda acts like a Morphology.
        
        As it happens, we don't need the full Morphology so we cheat.
        Cheating is good.
        
        '''
        
        return morphlib.morphology.Morphology(le_dict)

    def decode_source(le_dict, morphology, split_rules):
        '''Convert a dict into a Source object.'''

        source = morphlib.source.Source(le_dict['name'],
                                        le_dict['repo_name'],
                                        le_dict['original_ref'],
                                        le_dict['sha1'],
                                        le_dict['tree'],
                                        morphology,
                                        le_dict['filename'],
                                        split_rules)

        if morphology['kind'] == 'chunk':
            source.build_mode = le_dict['build_mode']
            source.prefix = le_dict['prefix']
        source.cache_id =  le_dict['cache_id']
        source.cache_key = le_dict['cache_key']
        return source
        
    def decode_artifact(artifact_dict, source):
        '''Convert dict into an Artifact object.
        
        Do not set dependencies, that will be dealt with later.
        
        '''

        artifact = morphlib.artifact.Artifact(source, artifact_dict['name'])
        artifact.arch = artifact_dict['arch']
        artifact.source = source

        return artifact

    le_dicts = yaml.load(json.loads(encoded))
    artifacts_dict = le_dicts['artifacts']
    sources_dict = le_dicts['sources']
    morphologies_dict = le_dicts['morphologies']
    root_artifact = le_dicts['root_artifact']

    artifact_ids = ([root_artifact] + artifacts_dict.keys())

    artifacts = {}
    sources = {}
    morphologies = {id: decode_morphology(d)
                    for (id, d) in morphologies_dict.iteritems()}

    for source_id, source_dict in sources_dict.iteritems():
        morphology = morphologies[source_dict['morphology']]
        kind = morphology['kind']
        ruler = getattr(morphlib.artifactsplitrule, 'unify_%s_matches' % kind)
        if kind in ('chunk', 'stratum'):
            rules = ruler(morphology, le_dicts['default_split_rules'][kind])
        else: # pragma: no cover
            rules = ruler(morphology)
        sources[source_id] = decode_source(source_dict, morphology, rules)

        # clear the source artifacts that get automatically generated
        # we want to add the ones that were sent to us
        sources[source_id].artifacts = {}
        source_artifacts = source_dict['artifact_ids']

        for artifact_id in source_artifacts:
            if artifact_id not in artifacts:
                artifact_dict = artifacts_dict[artifact_id]
                artifact = decode_artifact(artifact_dict, sources[source_id])

                artifacts[artifact_id] = artifact

            key = artifacts[artifact_id].name
            sources[source_id].artifacts[key] = artifacts[artifact_id]

    # now add the dependencies
    for source_id, source_dict in sources_dict.iteritems():
        source = sources[source_id]
        source.dependencies = [artifacts[aid]
                               for aid in source_dict['dependencies']]

    return artifacts[root_artifact]