summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-04-12 13:28:40 +0100
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-04-12 13:55:09 +0100
commitf0753bb71d4be104bcedcd8899e368948df7ae1a (patch)
tree0a51ba540613fa12248d69495c5e83e36af48060
parent412ead40e1e8671f84e28d15aad72c11a962c7a1 (diff)
downloadmorph-f0753bb71d4be104bcedcd8899e368948df7ae1a.tar.gz
Add a class to manage the staging area for builds
-rw-r--r--morphlib/__init__.py1
-rw-r--r--morphlib/stagingarea.py60
-rw-r--r--morphlib/stagingarea_tests.py75
3 files changed, 136 insertions, 0 deletions
diff --git a/morphlib/__init__.py b/morphlib/__init__.py
index 6c74082c..e6040af1 100644
--- a/morphlib/__init__.py
+++ b/morphlib/__init__.py
@@ -40,6 +40,7 @@ import savefile
import source
import sourcemanager
import sourcepool
+import stagingarea
import stopwatch
import tempdir
import util
diff --git a/morphlib/stagingarea.py b/morphlib/stagingarea.py
new file mode 100644
index 00000000..126d7d91
--- /dev/null
+++ b/morphlib/stagingarea.py
@@ -0,0 +1,60 @@
+# Copyright (C) 2012 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 shutil
+import tarfile
+
+
+class StagingArea(object):
+
+ '''Represent the staging area for building software.
+
+ The build dependencies of what will be built will be installed in the
+ staging area. The staging area may be a dedicated part of the
+ filesystem, used with chroot, or it can be the actual root of the
+ filesystem, which is needed when bootstrap building Baserock. The
+ caller chooses this by providing the root directory of the staging
+ area when the object is created. The directory must already exist.
+
+ The staging area can also install build artifacts.
+
+ '''
+
+ def __init__(self, dirname):
+ self.dirname = dirname
+
+ def install_artifact(self, handle):
+ '''Install a build artifact into the staging area.
+
+ We access the artifact via an open file handle. For now, we assume
+ the artifact is a tarball.
+
+ '''
+
+ tf = tarfile.TarFile(fileobj=handle)
+ tf.extractall(path=self.dirname)
+
+ def remove(self):
+ '''Remove the entire staging area.
+
+ Do not expect anything with the staging area to work after this
+ method is called. Be careful about calling this method if
+ the filesystem root directory was given as the dirname.
+
+ '''
+
+ shutil.rmtree(self.dirname)
+
diff --git a/morphlib/stagingarea_tests.py b/morphlib/stagingarea_tests.py
new file mode 100644
index 00000000..c6b17610
--- /dev/null
+++ b/morphlib/stagingarea_tests.py
@@ -0,0 +1,75 @@
+# Copyright (C) 2012 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 os
+import shutil
+import tarfile
+import tempfile
+import unittest
+
+import morphlib
+
+
+class StagingAreaTests(unittest.TestCase):
+
+ def setUp(self):
+ self.tempdir = tempfile.mkdtemp()
+ self.staging = os.path.join(self.tempdir, 'staging')
+ self.sa = morphlib.stagingarea.StagingArea(self.staging)
+
+ def tearDown(self):
+ shutil.rmtree(self.tempdir)
+
+ def create_chunk(self):
+ chunkdir = os.path.join(self.tempdir, 'chunk')
+ os.mkdir(chunkdir)
+ with open(os.path.join(chunkdir, 'file.txt'), 'w'):
+ pass
+ chunk_tar = os.path.join(self.tempdir, 'chunk.tar')
+ tf = tarfile.TarFile(name=chunk_tar, mode='w')
+ tf.add(chunkdir, arcname='.')
+ tf.close()
+
+ return chunk_tar
+
+ def list_tree(self, root):
+ files = []
+ for dirname, subdirs, basenames in os.walk(root):
+ paths = [os.path.join(dirname, x) for x in basenames]
+ for x in [dirname] + sorted(paths):
+ files.append(x[len(root):] or '/')
+ return files
+
+ def test_remembers_specified_directory(self):
+ self.assertEqual(self.sa.dirname, self.staging)
+
+ def test_accepts_root_directory(self):
+ sa = morphlib.stagingarea.StagingArea('/')
+ self.assertEqual(sa.dirname, '/')
+
+ def test_installs_artifact(self):
+ chunk_tar = self.create_chunk()
+ with open(chunk_tar, 'rb') as f:
+ self.sa.install_artifact(f)
+ self.assertEqual(self.list_tree(self.staging), ['/', '/file.txt'])
+
+ def test_removes_everything(self):
+ chunk_tar = self.create_chunk()
+ with open(chunk_tar, 'rb') as f:
+ self.sa.install_artifact(f)
+ self.sa.remove()
+ self.assertFalse(os.path.exists(self.staging))
+