summaryrefslogtreecommitdiff
path: root/morphlib
diff options
context:
space:
mode:
Diffstat (limited to 'morphlib')
-rw-r--r--morphlib/bins.py26
-rw-r--r--morphlib/builder2.py47
2 files changed, 48 insertions, 25 deletions
diff --git a/morphlib/bins.py b/morphlib/bins.py
index 71483172..622aa165 100644
--- a/morphlib/bins.py
+++ b/morphlib/bins.py
@@ -26,9 +26,24 @@ import os
import re
import errno
import stat
+import shutil
import tarfile
+# Work around http://bugs.python.org/issue16477
+def safe_makefile(self, tarinfo, targetpath):
+ '''Create a file, closing correctly in case of exception'''
+
+ source = self.extractfile(tarinfo)
+ try:
+ with open(targetpath, "wb") as target:
+ shutil.copyfileobj(source, target)
+ finally:
+ source.close()
+
+tarfile.TarFile.makefile = safe_makefile
+
+
def create_chunk(rootdir, f, regexps, dump_memory_profile=None):
'''Create a chunk from the contents of a directory.
@@ -187,11 +202,12 @@ def unpack_binary_from_file(f, dirname): # pragma: no cover
tf.makedev = monkey_patcher(tf.makedev)
tf.makelink = monkey_patcher(tf.makelink)
- tf.extractall(path=dirname)
- tf.close
+ try:
+ tf.extractall(path=dirname)
+ finally:
+ tf.close()
def unpack_binary(filename, dirname):
- f = open(filename, "rb")
- unpack_binary_from_file(f, dirname)
- f.close()
+ with open(filename, "rb") as f:
+ unpack_binary_from_file(f, dirname)
diff --git a/morphlib/builder2.py b/morphlib/builder2.py
index 0de0ebff..674cbb17 100644
--- a/morphlib/builder2.py
+++ b/morphlib/builder2.py
@@ -15,6 +15,7 @@
import datetime
+import errno
import json
import logging
import os
@@ -488,6 +489,24 @@ class SystemKindBuilder(BuilderBase): # pragma: no cover
'''
+ def unpack_one_stratum(self, stratum_artifact, target):
+ '''Unpack a single stratum into a target directory'''
+
+ cache = self.local_artifact_cache
+ with cache.get(stratum_artifact) as stratum_file:
+ artifact_list = json.load(stratum_file)
+ for chunk in (ArtifactCacheReference(a) for a in artifact_list):
+ self.app.status(msg='Unpacking chunk %(basename)s',
+ basename=chunk.basename(), chatty=True)
+ with cache.get(chunk) as chunk_file:
+ morphlib.bins.unpack_binary_from_file(chunk_file, target)
+
+ target_metadata = os.path.join(
+ target, 'baserock', '%s.meta' % stratum_artifact.name)
+ with cache.get_artifact_metadata(stratum_artifact, 'meta') as meta_src:
+ with morphlib.savefile.SaveFile(target_metadata, 'w') as meta_dst:
+ shutil.copyfileobj(meta_src, meta_dst)
+
def unpack_strata(self, path):
'''Unpack strata into a directory.'''
@@ -523,22 +542,7 @@ class SystemKindBuilder(BuilderBase): # pragma: no cover
# unpack it from the local artifact cache
for stratum_artifact in self.artifact.dependencies:
- f = self.local_artifact_cache.get(stratum_artifact)
- for chunk in (ArtifactCacheReference(a) for a in json.load(f)):
- self.app.status(msg='Unpacking chunk %(basename)s',
- basename=chunk.basename(), chatty=True)
- chunk_handle = self.local_artifact_cache.get(chunk)
- morphlib.bins.unpack_binary_from_file(chunk_handle, path)
- chunk_handle.close()
- f.close()
- meta = self.local_artifact_cache.get_artifact_metadata(
- stratum_artifact, 'meta')
- dst = morphlib.savefile.SaveFile(
- os.path.join(path, 'baserock',
- '%s.meta' % stratum_artifact.name), 'w')
- shutil.copyfileobj(meta, dst)
- dst.close()
- meta.close()
+ self.unpack_one_stratum(stratum_artifact, path)
ldconfig(self.app.runcmd, path)
@@ -706,15 +710,20 @@ class DiskImageBuilder(SystemKindBuilder): # pragma: no cover
mount_point)
self._install_bootloader(mount_point)
self.copy_kernel_into_artifact_cache(factory_path)
- self._unmount(mount_point)
except BaseException, e:
logging.error(traceback.format_exc())
self.app.status(msg='Error while building system',
error=True)
self._unmount(mount_point)
self._undo_device_mapping(image_name)
- raise
+ if type(e) is IOError and e.errno==errno.ENOSPC:
+ raise cliapp.AppException(
+ 'Ran out of space on %s disk image. Please '
+ 'increase the system\'s disk-size.' % rootfs_name)
+ else:
+ raise
+ self._unmount(mount_point)
self._undo_device_mapping(image_name)
self.app.status(msg='Compressing disk image',
@@ -819,5 +828,3 @@ class DiskImageBuilder(SystemKindBuilder): # pragma: no cover
filename=image_name, chatty=True)
with self.build_watch('undo-device-mapper'):
morphlib.fsutils.undo_device_mapping(self.app.runcmd, image_name)
-
-