From d338f525f8f5edd4b9c1e0dbf90e97e6c46ba2e7 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Wed, 15 Apr 2015 14:45:50 +0000 Subject: 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 --- morphlib/builder.py | 25 ++++--------------------- morphlib/plugins/deploy_plugin.py | 3 +-- morphlib/util.py | 22 ++++++++++++++++++++++ 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 9c2782e6..1631e7b9 100644 --- a/morphlib/plugins/deploy_plugin.py +++ b/morphlib/plugins/deploy_plugin.py @@ -564,8 +564,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] -- cgit v1.2.1