# HG changeset patch # User Bernhard Herzog # Date 1211388454 0 # Node ID 890bb70920d66680d3fb146c2782544aaf14f974 # Parent 007d7f2aa18479afe17d1456735f339d8f038a55 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. diff -r 007d7f2aa184 -r 890bb70920d6 test/test_builder.py --- a/test/test_builder.py Wed May 21 16:46:16 2008 +0000 +++ b/test/test_builder.py Wed May 21 16:47:34 2008 +0000 @@ -12,6 +12,7 @@ import unittest from treepkg.builder import PBuilder +from treepkg.run import call from filesupport import FileTestMixin @@ -22,8 +23,7 @@ open(sys.argv[1], 'w').write(repr(sys.argv[2:])) """ - -class TestPBuilder(unittest.TestCase, FileTestMixin): +class PBuilderTests(unittest.TestCase, FileTestMixin): def setUp(self): self.dump_command_line = self.create_temp_file("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 +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 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]) diff -r 007d7f2aa184 -r 890bb70920d6 treepkg/builder.py --- a/treepkg/builder.py Wed May 21 16:46:16 2008 +0000 +++ b/treepkg/builder.py Wed May 21 16:47:34 2008 +0000 @@ -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: