summaryrefslogtreecommitdiff
path: root/morphlib/bins.py
diff options
context:
space:
mode:
authorJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-12-17 14:16:53 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-02-21 15:06:18 +0000
commit6d901c09f611b3cb98d6b88bf99810782610c617 (patch)
tree23bc641a9aa0cd2be4ec0e79ec03e7aae100ad13 /morphlib/bins.py
parente2a99f9bb762e61053cd3ee788390d0b2bb5e72d (diff)
downloadmorph-6d901c09f611b3cb98d6b88bf99810782610c617.tar.gz
Add ExtractedTarball class and method to extract/mount an artifact
ExtractedTarball is more or less the equivalent to MountableImage for artifacts that are not mountable images. So in order to inspect root file system tarballs, ExtractedTarball can be used, for disk images, MountableImage can be used. The morphlib.bins.call_in_artifact_directory() method combines these two classes and provides a way to extract/mount an artifact and call a callback with the temporary directory / mount point as its first argument. Using this, a plugin that runs a command relative to an artifact's root directory can be written easily.
Diffstat (limited to 'morphlib/bins.py')
-rw-r--r--morphlib/bins.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/morphlib/bins.py b/morphlib/bins.py
index 622aa165..ba5f713f 100644
--- a/morphlib/bins.py
+++ b/morphlib/bins.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2011-2012 Codethink Limited
+# Copyright (C) 2011-2013 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
@@ -21,6 +21,7 @@ Binaries are chunks, strata, and system images.
'''
+import cliapp
import logging
import os
import re
@@ -29,6 +30,11 @@ import stat
import shutil
import tarfile
+import morphlib
+
+from morphlib.extractedtarball import ExtractedTarball
+from morphlib.mountableimage import MountableImage
+
# Work around http://bugs.python.org/issue16477
def safe_makefile(self, tarinfo, targetpath):
@@ -211,3 +217,24 @@ def unpack_binary_from_file(f, dirname): # pragma: no cover
def unpack_binary(filename, dirname):
with open(filename, "rb") as f:
unpack_binary_from_file(f, dirname)
+
+
+class ArtifactNotMountableError(cliapp.AppException): # pragma: no cover
+
+ def __init__(self, filename):
+ cliapp.AppException.__init__(
+ self, 'Artifact %s cannot be extracted or mounted' % filename)
+
+
+def call_in_artifact_directory(app, filename, callback): # pragma: no cover
+ '''Call a function in a directory the artifact is extracted/mounted in.'''
+
+ try:
+ with ExtractedTarball(app, filename) as dirname:
+ callback(dirname)
+ except tarfile.TarError:
+ try:
+ with MountableImage(app, filename) as dirname:
+ callback(dirname)
+ except (IOError, OSError):
+ raise ArtifactNotMountableError(filename)