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
|
# distbuild/serialise.py -- (de)serialise Artifact object graphs
#
# Copyright (C) 2012, 2014, 2015 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 logging
import yaml
import morphlib
class ArtifactReference(object):
'''Container for some basic information about an artifact.'''
def __init__(self, basename, encoded):
self._basename = basename
self._dict = encoded
def __getattr__(self, name):
if not name.startswith('_'):
return self._dict.get(name)
else:
super(ArtifactReference, self).__getattr(name)
def __setattr__(self, name, val):
if not name.startswith('_'):
self._dict[name] = val
else:
super(ArtifactReference, self).__setattr__(name, val)
def __repr__(self):
return 'REF: %s' % self.basename()
def basename(self):
return self._basename
def serialise_artifact(artifact, repo, ref):
'''Serialise an Artifact object and its dependencies into string form.'''
def encode_artifact(a):
if artifact.source.morphology['kind'] == 'system': # pragma: no cover
arch = artifact.source.morphology['arch']
else:
arch = artifact.arch
a_dict = {
'root_filename': artifact.source.filename,
'arch': arch,
'cache_key': a.source.cache_key,
'basename': a.basename(),
'filename': a.source.filename,
'name': a.name,
'kind': a.source.morphology['kind'],
'repo': repo,
'ref': ref,
'dependencies': [],
'source_repo': a.source.repo_name,
'source_ref': a.source.sha1,
'source_artifacts': []
}
for dep in a.source.dependencies:
a_dict['dependencies'].append(dep.basename())
for other in a.source.artifacts:
a_dict['source_artifacts'].append(other)
return a_dict
def encode_artifact_reference(a):
a_dict = dict(a._dict)
a_dict['dependencies'] = [dep.basename() for dep in a.dependencies]
a_dict['source_artifacts'] = [str(sa) for sa in a.source_artifacts]
return a_dict
def handle_artifact_reference(a):
if a.basename() in encoded_artifacts:
return
visited_artifacts[a.basename()] = a
for dep in a.dependencies:
handle_artifact_reference(dep)
encoded_artifacts[a.basename()] = encode_artifact_reference(a)
encoded_artifacts = {}
visited_artifacts = {}
if isinstance(artifact, morphlib.artifact.Artifact):
for a in artifact.walk():
if a.basename() not in encoded_artifacts: # pragma: no cover
encoded_artifacts[a.basename()] = encode_artifact(a)
visited_artifacts[artifact.basename()] = artifact
elif isinstance(artifact, ArtifactReference):
encoded_artifacts[artifact.basename()] = encode_artifact_reference(artifact)
content = {
'root-artifact': artifact.basename(),
'artifacts': encoded_artifacts
}
logging.debug('SERIALISE: dumping content')
ret = json.dumps(yaml.dump(content))
logging.debug('SERIALISE: dumped')
return ret
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_artifact(artifact_dict):
'''Convert dict into an Artifact object.
Do not set dependencies, that will be dealt with later.
'''
artifact = ArtifactReference(artifact_dict)
return artifact
content = yaml.load(json.loads(encoded))
root = content['root-artifact']
encoded_artifacts = content['artifacts']
artifacts = {}
# decode artifacts
for basename, artifact_dict in encoded_artifacts.iteritems():
artifact = ArtifactReference(basename, artifact_dict)
artifacts[basename] = artifact
# add dependencies
for basename, a_dict in encoded_artifacts.iteritems():
artifact = artifacts[basename]
artifact.dependencies = [artifacts.get(dep)
for dep in a_dict['dependencies']]
return artifacts[root]
|