[Treepkg-commits] r46 - in trunk: test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed May 21 18:47:34 CEST 2008


Author: bh
Date: 2008-05-21 18:47:34 +0200 (Wed, 21 May 2008)
New Revision: 46

Modified:
   trunk/test/test_builder.py
   trunk/treepkg/builder.py
Log:
Add the PBuilder.add_binaries_to_extra_pkg method.  It allows automatic
installation of extra packages that should be available in the pbuilder
chroot.  Also add corresponding tests.


Modified: trunk/test/test_builder.py
===================================================================
--- trunk/test/test_builder.py	2008-05-21 16:46:16 UTC (rev 45)
+++ trunk/test/test_builder.py	2008-05-21 16:47:34 UTC (rev 46)
@@ -12,6 +12,7 @@
 import unittest
 
 from treepkg.builder import PBuilder
+from treepkg.run import call
 
 from filesupport import FileTestMixin
 
@@ -22,9 +23,8 @@
 open(sys.argv[1], 'w').write(repr(sys.argv[2:]))
 """
 
+class PBuilderTests(unittest.TestCase, FileTestMixin):
 
-class TestPBuilder(unittest.TestCase, FileTestMixin):
-
     def setUp(self):
         self.dump_command_line = self.create_temp_file("dump_command_line.py",
                                                        dump_command_line_py)
@@ -35,6 +35,9 @@
     def check_command_line(self, args):
         self.checkFileContents(self.command_line_file, repr(args))
 
+
+class TestPBuilder(PBuilderTests):
+
     def test_build(self):
         """Tests the PBuilder.build method.
         The test checks whether the build method creates the binary_dir
@@ -80,3 +83,93 @@
                                  '--bindmounts', '/home/builder/foo',
                                  '--bindmounts', '/home/builder/treepkg',
                                  'my_script'])
+
+
+class TestPBuilderWithBinaryPackage(PBuilderTests):
+
+    minimal_package = [
+        ("debian",
+         [("control", """\
+Source: minimal
+Section: utils
+Priority: optional
+Maintainer: Bernhard Herzog <bh at intevation.de>
+Standards-Version: 3.7.2.2
+Build-Depends: debhelper
+
+Package: minimal
+Architecture: any
+Depends: ${shlibs:Depends}
+Description: Minimal package for test purposes
+"""),
+          ("rules", 0755, """\
+#!/usr/bin/make -f
+
+build: build-stamp
+build-stamp:
+	dh_testdir
+	touch build-stamp
+
+clean:
+	dh_testdir
+	dh_testroot
+	rm -f build-stamp
+	dh_clean
+
+# Build architecture-dependent files here.
+binary-arch:
+	dh_testdir
+	dh_testroot
+	dh_installdocs README
+	dh_installchangelogs
+	dh_fixperms
+	dh_installdeb
+	dh_gencontrol
+	dh_md5sums
+	dh_builddeb
+
+binary: binary-arch
+.PHONY: build build-stamp clean binary-arch binary
+"""),
+          ("changelog", """\
+minimal (1.0-1) unstable; urgency=low
+
+  * Newly packaged
+
+ -- Bernhard Herzog <bh at intevation.de>  Wed, 21 May 2008 16:10:29 +0200
+""")]),
+        ("README", """\
+This is a minimal debian package for test purposes
+"""),
+        ]
+
+    pbuilder_files = [("pbuilderrc", ""),
+                      ("extra-pkg", [])]
+
+    def setUp(self):
+        PBuilderTests.setUp(self)
+        self.temp_base_dir = self.create_temp_dir(self.id())
+        self.minimal_packge_dir = os.path.join(self.temp_base_dir,
+                                               "minimal-1.0")
+        self.create_files(self.minimal_packge_dir, self.minimal_package)
+        call(["dpkg-buildpackage", "-rfakeroot", "-b", "-uc"],
+             cwd=self.minimal_packge_dir, suppress_output=True)
+        self.minimal_package_deb = os.path.join(self.temp_base_dir,
+                                                "minimal_1.0-1_i386.deb")
+        self.pbuilder_basedir = os.path.join(self.temp_base_dir, "pbuilder")
+        self.create_files(self.pbuilder_basedir, self.pbuilder_files)
+        self.extra_pkg_dir = os.path.join(self.pbuilder_basedir, "extra-pkg")
+        self.pbuilderrc = os.path.join(self.pbuilder_basedir, "pbuilderrc")
+
+    def test_add_binaries_to_extra_pkg(self):
+        """Tests the PBuilder.add_binaries_to_extra_pkg method"""
+        builder = PBuilder(self.pbuilderrc, self.root_command)
+        # sanity check: the extra-pkg directory should be empty now
+        self.assertEquals(os.listdir(self.extra_pkg_dir), [])
+
+        builder.add_binaries_to_extra_pkg([self.minimal_package_deb])
+
+        self.assertEquals(sorted(os.listdir(self.extra_pkg_dir)),
+                          ["Packages", "minimal_1.0-1_i386.deb"])
+        self.check_command_line(['/usr/sbin/pbuilder', 'update',
+                                 '--configfile', self.pbuilderrc])

Modified: trunk/treepkg/builder.py
===================================================================
--- trunk/treepkg/builder.py	2008-05-21 16:46:16 UTC (rev 45)
+++ trunk/treepkg/builder.py	2008-05-21 16:47:34 UTC (rev 46)
@@ -8,6 +8,7 @@
 """Build binary packages from source packages"""
 
 import os
+import shutil
 import logging
 
 import util
@@ -50,6 +51,31 @@
             if os.path.splitext(filename)[1] not in (".deb", ".changes"):
                 os.remove(os.path.join(binary_dir, filename))
 
+    def add_binaries_to_extra_pkg(self, filenames):
+        """Adds binary packages to the extra-pkg directory.
+        The filenames parameter should be sequence of absolute
+        filenames.  The files named will be copied to the extra-pkg
+        directory which is assumed to reside in the same directory as
+        the pbuilderrc.  Afterwards, the method generates a Packages
+        file in the directory and runs pbuilder update.  All of this
+        assumes that pbuilder was set up the way bin/initpbuilder.py
+        does.
+        """
+        extrapkg_dir = os.path.join(os.path.dirname(self.pbuilderrc),
+                                    "extra-pkg")
+        for filename in filenames:
+            logging.info("Copying %s into %s", filename, extrapkg_dir)
+            shutil.copy(filename, extrapkg_dir)
+        logging.info("Running apt-ftparchive in %s", extrapkg_dir)
+        run.call(cmdexpand("apt-ftparchive packages ."),
+                 stdout=open(os.path.join(extrapkg_dir, "Packages"), "w"),
+                 cwd=extrapkg_dir)
+        logging.info("Running pbuilder update for %s", self.pbuilderrc)
+        run.call(cmdexpand("@rootcmd /usr/sbin/pbuilder update"
+                           " --configfile $pbuilderrc",
+                           rootcmd=self.root_cmd, pbuilderrc=self.pbuilderrc),
+                 suppress_output=True)
+
     def run_script(self, script, logfile, bindmounts=()):
         """Execute a script in pbuilder's chroot environment
         Parameters:



More information about the Treepkg-commits mailing list