summaryrefslogtreecommitdiff
path: root/morphlib/extractedtarball.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/extractedtarball.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/extractedtarball.py')
-rw-r--r--morphlib/extractedtarball.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/morphlib/extractedtarball.py b/morphlib/extractedtarball.py
new file mode 100644
index 00000000..e435b1ef
--- /dev/null
+++ b/morphlib/extractedtarball.py
@@ -0,0 +1,61 @@
+# Copyright (C) 2012-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
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+import cliapp
+import gzip
+import os
+import tempfile
+import shutil
+
+import morphlib
+
+
+class ExtractedTarball(object): # pragma: no cover
+
+ '''Tarball extracted in a temporary directory.
+
+ This can be used e.g. to inspect the contents of a rootfs tarball.
+
+ '''
+ def __init__(self, app, tarball):
+ self.app = app
+ self.tarball = tarball
+
+ def setup(self):
+ self.app.status(msg='Preparing tarball %(tarball)s',
+ tarball=os.path.basename(self.tarball), chatty=True)
+ self.app.status(msg=' Extracting...', chatty=True)
+ self.tempdir = tempfile.mkdtemp(dir=self.app.settings['tempdir'])
+ try:
+ morphlib.bins.unpack_binary(self.tarball, self.tempdir)
+ except:
+ shutil.rmtree(self.tempdir)
+ raise
+ return self.tempdir
+
+ def cleanup(self):
+ self.app.status(msg='Cleanup extracted tarball %(tarball)s',
+ tarball=os.path.basename(self.tarball), chatty=True)
+ try:
+ shutil.rmtree(self.tempdir)
+ except:
+ pass
+
+ def __enter__(self):
+ return self.setup()
+
+ def __exit__(self, exctype, excvalue, exctraceback):
+ self.cleanup()