changeset 170:bfcb2bbf9a52

Move the pbuilder initialization code from bin/initpbuilder.py to treepkg/builder.py to make it work again and add some tests.
author Bernhard Herzog <bh@intevation.de>
date Mon, 23 Jun 2008 13:21:08 +0000
parents 261b75d7b972
children c0ea6cbb0fd2
files bin/initpbuilder.py test/test_builder.py treepkg/builder.py
diffstat 3 files changed, 168 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/bin/initpbuilder.py	Fri Jun 20 15:51:24 2008 +0000
+++ b/bin/initpbuilder.py	Mon Jun 23 13:21:08 2008 +0000
@@ -14,81 +14,11 @@
 """
 
 import sys
-import os
-from optparse import OptionParser
 
 import treepkgcmd
 from treepkg.options import create_parser
 from treepkg.packager import create_package_track, PackagerGroup
 from treepkg.readconfig import read_config
-from treepkg.util import ensure_directory, writefile
-from treepkg.run import call
-from treepkg.cmdexpand import cmdexpand
-
-
-pbuilderrc_template = '''\
-# This file was automatically generated by initpbuilder.py.
-# for the possible settings see "man pbuilderrc"
-
-BASETGZ=%(basedir)s/base.tgz
-BUILDPLACE=%(builddir)s
-USEPROC=yes
-USEDEVPTS=yes
-BUILDRESULT=%(resultdir)s
-DISTRIBUTION=%(distribution)s
-APTCACHE=%(basedir)s/aptcache
-APTCACHEHARDLINK=yes
-REMOVEPACKAGES=lilo
-MIRRORSITE="%(mirrorsite)s"
-OTHERMIRROR="%(othermirror)s"
-BINDMOUNTS="%(extra-pkgdir)s"
-PKGNAME_LOGFILE=yes
-'''
-
-
-def init_pbuilder(pbuilderrc, distribution, mirrorsite, extramirrors, root_cmd):
-    if not os.path.isabs(pbuilderrc):
-        print >>sys.stderr, "pbuilderrc must be an absolute filename"
-        sys.exit(1)
-
-    if os.path.exists(pbuilderrc):
-        print >>sys.stderr, "pbuilderrc %r already exists." % pbuilderrc
-        sys.exit(1)
-
-    basedir = os.path.dirname(pbuilderrc)
-    replacements = dict(basedir=basedir,
-                        distribution=distribution,
-                        mirrorsite=mirrorsite)
-
-    # create the pbuilder directories.  basedir is created implicitly by
-    # creating its subdirectories.
-    for subdir in ["base", "build", "result", "aptcache", "extra-pkg"]:
-        directory = os.path.join(basedir, subdir)
-        replacements[subdir + "dir"] = directory
-        print "creating directory:", repr(directory)
-        ensure_directory(directory)
-
-    # build OTHERMIRROR value.  We always include the extra-pkg dir.
-    othermirror = "deb file://%(extra-pkgdir)s ./" % replacements
-    if extramirrors:
-        othermirror += " | " + extramirrors
-    replacements["othermirror"] = othermirror
-
-    # create the pbuilderrcfile
-    print "creating pbuilderrc:", repr(pbuilderrc)
-    writefile(pbuilderrc, pbuilderrc_template % replacements)
-
-    # turn the extra-pkg directory into a proper deb archive
-    print "turning the extra-pkg dir into a debian archive"
-    extra_pkgdir = replacements["extra-pkgdir"]
-    call(cmdexpand("apt-ftparchive packages ."),
-         stdout=open(os.path.join(extra_pkgdir, "Packages"), "w"),
-         cwd=extra_pkgdir)
-
-    # create the base.tgz chroot
-    print "running pbuilder create"
-    call(cmdexpand("@root_cmd pbuilder create --configfile $pbuilderrc",
-                   **locals()))
 
 
 def parse_commandline():
@@ -118,10 +48,8 @@
                            for opts in packager_opts],
                           **treepkg_opts)
     track = group.get_package_tracks()[0]
-    init_pbuilder(track.pbuilderrc,
-                  distribution=options.distribution,
-                  mirrorsite=options.mirrorsite,
-                  extramirrors=options.othermirror,
-                  root_cmd=track.root_cmd)
+    track.builder.init_pbuilder(distribution=options.distribution,
+                                mirrorsite=options.mirrorsite,
+                                extramirrors=options.othermirror)
 
 main()
--- a/test/test_builder.py	Fri Jun 20 15:51:24 2008 +0000
+++ b/test/test_builder.py	Mon Jun 23 13:21:08 2008 +0000
@@ -10,6 +10,7 @@
 import sys
 import os
 import unittest
+import StringIO
 
 from treepkg.builder import PBuilder
 from treepkg.run import call
@@ -42,6 +43,102 @@
 
 class TestPBuilder(PBuilderTests):
 
+    def test_init_pbuilder(self):
+        """Tests the PBuilder.init_pbuilder method."""
+        basedir = self.create_temp_dir(self.id())
+        pbuilderrc = os.path.join(basedir, "pbuilderrc")
+        builder = PBuilder(pbuilderrc, self.root_command)
+        old_stdout = sys.stdout
+        sys.stdout = captured_stdout = StringIO.StringIO()
+        try:
+            builder.init_pbuilder(distribution="etch",
+                                  mirrorsite="http://example.com/debian",
+                                  extramirrors=None)
+        finally:
+            sys.stdout = old_stdout
+
+        # check whether the necessary directories were created
+        missing = [dirname for dirname in ["base", "build", "result",
+                                           "aptcache", "extra-pkg"]
+                   if not os.path.isdir(os.path.join(basedir, dirname))]
+        if missing:
+            self.fail("init_pbuilder did not create these directories: %s"
+                      % " ".join(missing))
+
+        # check the pbuilderrc.  This test is a little too strict
+        # because it checks the exact contents of the file.  Instread it
+        # should normalize the contents in some way and check that.
+        pbuilderrc_contents = (
+            "# This file was automatically generated by initpbuilder.py.\n"
+            "# for the possible settings see \"man pbuilderrc\"\n"
+            "\n"
+            "BASETGZ=%(basedir)s/base/base.tgz\n"
+            "BUILDPLACE=%(basedir)s/build\n"
+            "USEPROC=yes\n"
+            "USEDEVPTS=yes\n"
+            "BUILDRESULT=%(basedir)s/result\n"
+            "DISTRIBUTION=etch\n"
+            "APTCACHE=%(basedir)s/base/aptcache\n"
+            "APTCACHEHARDLINK=yes\n"
+            "REMOVEPACKAGES=lilo\n"
+            "MIRRORSITE=\"http://example.com/debian\"\n"
+            "OTHERMIRROR=\"deb file://%(basedir)s/extra-pkg ./\"\n"
+            "BINDMOUNTS=\"%(basedir)s/extra-pkg\"\n"
+            "PKGNAME_LOGFILE=yes\n" % locals())
+        self.checkFileContents(pbuilderrc, pbuilderrc_contents)
+
+        # The Packages file is empty for now.
+        self.checkFileContents(os.path.join(basedir, "extra-pkg", "Packages"),
+                               "")
+        # check the text written to stdout.  This test is a little too
+        # strict because it checks the exact output.
+        self.assertEquals(captured_stdout.getvalue(),
+                          "creating directory: '%(basedir_repr)s/base'\n"
+                          "creating directory: '%(basedir_repr)s/build'\n"
+                          "creating directory: '%(basedir_repr)s/result'\n"
+                          "creating directory: '%(basedir_repr)s/aptcache'\n"
+                          "creating directory: '%(basedir_repr)s/extra-pkg'\n"
+                          "creating pbuilderrc: '%(basedir_repr)s/pbuilderrc'\n"
+                          "turning the extra-pkg dir into a debian archive\n"
+                          "running pbuilder create\n"
+                          % dict(basedir_repr=repr(basedir)[1:-1]))
+
+    def test_init_pbuilder_run_twice(self):
+        """Tests whether PBuilder.init_pbuilder prints an error when run twice.
+        """
+        basedir = self.create_temp_dir(self.id())
+
+        # run it once
+        pbuilderrc = os.path.join(basedir, "pbuilderrc")
+        builder = PBuilder(pbuilderrc, self.root_command)
+        old_stdout = sys.stdout
+        sys.stdout = captured_stdout = StringIO.StringIO()
+        try:
+            builder.init_pbuilder(distribution="etch",
+                                  mirrorsite="http://example.com/debian",
+                                  extramirrors=None)
+        finally:
+            sys.stdout = old_stdout
+
+        # running it again should not modify anything in the directory
+        # (which we don't check currently), it should print an error
+        # message and exit with exit code 1.
+        old_stderr = sys.stderr
+        sys.stderr = captured_stderr = StringIO.StringIO()
+        try:
+            try:
+                builder.init_pbuilder(distribution="etch",
+                                      mirrorsite="http://example.com/debian",
+                                      extramirrors=None)
+            except SystemExit, exc:
+                self.assertEquals(exc.code, 1)
+        finally:
+            sys.stderr = old_stderr
+        self.assertEquals("pbuilderrc %r already exists.\n"
+                          % os.path.join(basedir, "pbuilderrc"),
+                          captured_stderr.getvalue())
+
+
     def test_build(self):
         """Tests the PBuilder.build method.
         The test checks whether the build method creates the binary_dir
--- a/treepkg/builder.py	Fri Jun 20 15:51:24 2008 +0000
+++ b/treepkg/builder.py	Mon Jun 23 13:21:08 2008 +0000
@@ -7,6 +7,7 @@
 
 """Build binary packages from source packages"""
 
+import sys
 import os
 import shutil
 import logging
@@ -20,6 +21,26 @@
 
     """Represents a way to run and manage a specific pbuilder instance"""
 
+    pbuilderrc_template = '''\
+# This file was automatically generated by initpbuilder.py.
+# for the possible settings see "man pbuilderrc"
+
+BASETGZ=%(basedir)s/base.tgz
+BUILDPLACE=%(builddir)s
+USEPROC=yes
+USEDEVPTS=yes
+BUILDRESULT=%(resultdir)s
+DISTRIBUTION=%(distribution)s
+APTCACHE=%(basedir)s/aptcache
+APTCACHEHARDLINK=yes
+REMOVEPACKAGES=lilo
+MIRRORSITE="%(mirrorsite)s"
+OTHERMIRROR="%(othermirror)s"
+BINDMOUNTS="%(extra-pkgdir)s"
+PKGNAME_LOGFILE=yes
+'''
+
+
     def __init__(self, pbuilderrc, root_cmd):
         """Initialize the PBuilder instance with the configuration file.
         The root_cmd parameter should be a list with a command that can
@@ -31,6 +52,53 @@
         self.pbuilderrc = pbuilderrc
         self.root_cmd = root_cmd
 
+    def init_pbuilder(self, distribution, mirrorsite, extramirrors):
+        """Initializes the pbuilder instance"""
+        if not os.path.isabs(self.pbuilderrc):
+            print >>sys.stderr, "pbuilderrc must be an absolute filename"
+            sys.exit(1)
+
+        if os.path.exists(self.pbuilderrc):
+            print >>sys.stderr, ("pbuilderrc %r already exists."
+                                 % self.pbuilderrc)
+            sys.exit(1)
+
+        basedir = os.path.dirname(self.pbuilderrc)
+        replacements = dict(basedir=basedir,
+                            distribution=distribution,
+                            mirrorsite=mirrorsite)
+
+        # create the pbuilder directories.  basedir is created implicitly by
+        # creating its subdirectories.
+        for subdir in ["base", "build", "result", "aptcache", "extra-pkg"]:
+            directory = os.path.join(basedir, subdir)
+            replacements[subdir + "dir"] = directory
+            print "creating directory:", repr(directory)
+            util.ensure_directory(directory)
+
+        # build OTHERMIRROR value.  We always include the extra-pkg dir.
+        othermirror = "deb file://%(extra-pkgdir)s ./" % replacements
+        if extramirrors:
+            othermirror += " | " + extramirrors
+        replacements["othermirror"] = othermirror
+
+        # create the pbuilderrcfile
+        print "creating pbuilderrc:", repr(self.pbuilderrc)
+        util.writefile(self.pbuilderrc, self.pbuilderrc_template % replacements)
+
+        # turn the extra-pkg directory into a proper deb archive
+        print "turning the extra-pkg dir into a debian archive"
+        extra_pkgdir = replacements["extra-pkgdir"]
+        run.call(cmdexpand("apt-ftparchive packages ."),
+                 stdout=open(os.path.join(extra_pkgdir, "Packages"), "w"),
+                 cwd=extra_pkgdir)
+
+        # create the base.tgz chroot
+        print "running pbuilder create"
+        run.call(cmdexpand("@root_cmd pbuilder create --configfile $pbuilderrc",
+                           root_cmd=self.root_cmd, pbuilderrc=self.pbuilderrc))
+
+
     def build(self, dsc_file, binary_dir, logfile, bindmounts=(),
               extra_packages=(), extra_env=None):
         """Build a binary packager from a source package
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)