summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2015-04-15 14:45:50 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2015-04-20 14:07:42 +0000
commitf75cf185b208e1b66bf11b3633c7721b79f16df8 (patch)
tree0288266cc9502898e52407496eb8693e110d6824
parentf42e2af871873653c70715d06d0cf6a9102d1ff7 (diff)
downloadmorph-f75cf185b208e1b66bf11b3633c7721b79f16df8.tar.gz
Consolidate parsing of stratum artifacts in one place
This allows handing parse errors that might occur if the files in the cache have become corrupted, instead of crashing with a traceback. Change-Id: Ibaa372ba6a14e7a04de907cb3a664b92cf61fbf3
-rw-r--r--morphlib/builder.py25
-rw-r--r--morphlib/plugins/deploy_plugin.py3
-rw-r--r--morphlib/util.py22
3 files changed, 27 insertions, 23 deletions
diff --git a/morphlib/builder.py b/morphlib/builder.py
index b0c95bb3..44e26d06 100644
--- a/morphlib/builder.py
+++ b/morphlib/builder.py
@@ -28,7 +28,6 @@ import tempfile
import cliapp
import morphlib
-from morphlib.artifactcachereference import ArtifactCacheReference
from morphlib.util import error_message_for_containerised_commandline
import morphlib.gitversion
@@ -558,29 +557,12 @@ class SystemBuilder(BuilderBase): # pragma: no cover
self.save_build_times()
return self.source.artifacts.itervalues()
- def load_stratum(self, stratum_artifact):
- '''Load a stratum from the local artifact cache.
-
- Returns a list of ArtifactCacheReference instances for the chunks
- contained in the stratum.
-
- '''
- cache = self.local_artifact_cache
- with open(cache.get(stratum_artifact), 'r') as stratum_file:
- try:
- artifact_list = json.load(stratum_file,
- encoding='unicode-escape')
- except ValueError as e:
- raise cliapp.AppException(
- 'Corruption detected: %s while loading %s' %
- (e, cache.artifact_filename(stratum_artifact)))
- return [ArtifactCacheReference(a) for a in artifact_list]
-
def unpack_one_stratum(self, stratum_artifact, target):
'''Unpack a single stratum into a target directory'''
cache = self.local_artifact_cache
- for chunk in self.load_stratum(stratum_artifact):
+ chunks = morphlib.util.get_stratum_contents(cache, stratum_artifact)
+ for chunk in chunks:
self.app.status(msg='Checkout chunk %(basename)s',
basename=chunk.basename(), chatty=True)
cache.get(chunk, target)
@@ -606,7 +588,8 @@ class SystemBuilder(BuilderBase): # pragma: no cover
# download the chunk artifacts if necessary
for stratum_artifact in self.source.dependencies:
- chunks = self.load_stratum(stratum_artifact)
+ chunks = morphlib.util.get_stratum_contents(
+ self.local_artifact_cache, stratum_artifact)
download_depends(chunks,
self.local_artifact_cache,
self.remote_artifact_cache)
diff --git a/morphlib/plugins/deploy_plugin.py b/morphlib/plugins/deploy_plugin.py
index dbca2d10..45c516b1 100644
--- a/morphlib/plugins/deploy_plugin.py
+++ b/morphlib/plugins/deploy_plugin.py
@@ -554,8 +554,7 @@ class DeployPlugin(cliapp.Plugin):
a morphlib.remoteartifactcache.GetError is raised.
"""
- with open(lac.get(artifact), 'r') as stratum:
- chunks = [ArtifactCacheReference(c) for c in json.load(stratum)]
+ chunks = morphlib.util.get_stratum_contents(lac, artifact)
morphlib.builder.download_depends(chunks, lac, rac)
for chunk in chunks:
self.app.status(msg='Checkout chunk %(name)s.',
diff --git a/morphlib/util.py b/morphlib/util.py
index 91880988..8ae95b65 100644
--- a/morphlib/util.py
+++ b/morphlib/util.py
@@ -14,6 +14,7 @@
import contextlib
import itertools
+import json
import os
import pipes
import re
@@ -21,9 +22,11 @@ import subprocess
import textwrap
import sys
+import cliapp
import fs.osfs
import morphlib
+from morphlib.artifactcachereference import ArtifactCacheReference
'''Utility functions for morph.'''
@@ -692,3 +695,22 @@ def write_from_dict(filepath, d, validate=lambda x, y: True): #pragma: no cover
os.fchown(f.fileno(), 0, 0)
os.fchmod(f.fileno(), 0644)
+
+
+def get_stratum_contents(cache, stratum_artifact):
+ '''Load a stratum from a local artifact cache.
+
+ Returns a list of ArtifactCacheReference instances for the chunks
+ contained in the stratum.
+
+ '''
+
+ with open(cache.get(stratum_artifact), 'r') as stratum_file:
+ try:
+ artifact_list = json.load(stratum_file,
+ encoding='unicode-escape')
+ except ValueError as e:
+ raise cliapp.AppException(
+ 'Corruption detected: %s while loading %s' %
+ (e, cache.artifact_filename(stratum_artifact)))
+ return [ArtifactCacheReference(a) for a in artifact_list]