summaryrefslogtreecommitdiff
path: root/morphlib/util.py
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 /morphlib/util.py
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
Diffstat (limited to 'morphlib/util.py')
-rw-r--r--morphlib/util.py22
1 files changed, 22 insertions, 0 deletions
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]