[Treepkg-commits] r95 - in trunk: bin test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Jun 23 15:21:08 CEST 2008


Author: bh
Date: 2008-06-23 15:21:08 +0200 (Mon, 23 Jun 2008)
New Revision: 95

Modified:
   trunk/bin/initpbuilder.py
   trunk/test/test_builder.py
   trunk/treepkg/builder.py
Log:
Move the pbuilder initialization code from bin/initpbuilder.py to
treepkg/builder.py to make it work again and add some tests.


Modified: trunk/bin/initpbuilder.py
===================================================================
--- trunk/bin/initpbuilder.py	2008-06-20 15:51:24 UTC (rev 94)
+++ trunk/bin/initpbuilder.py	2008-06-23 13:21:08 UTC (rev 95)
@@ -14,83 +14,13 @@
 """
 
 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():
     parser = create_parser()
     parser.set_defaults(distribution="etch")
@@ -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()

Modified: trunk/test/test_builder.py
===================================================================
--- trunk/test/test_builder.py	2008-06-20 15:51:24 UTC (rev 94)
+++ trunk/test/test_builder.py	2008-06-23 13:21:08 UTC (rev 95)
@@ -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

Modified: trunk/treepkg/builder.py
===================================================================
--- trunk/treepkg/builder.py	2008-06-20 15:51:24 UTC (rev 94)
+++ trunk/treepkg/builder.py	2008-06-23 13:21:08 UTC (rev 95)
@@ -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



More information about the Treepkg-commits mailing list