view treepkg/builder.py @ 119:92116333ef77

Add PBuilder.run_script method and associated tests. The run_script method allows arbitrary scripts to be run in the pbuilder chroot environment
author Bernhard Herzog <bh@intevation.de>
date Wed, 21 May 2008 13:20:36 +0000
parents cea98d4e4a6a
children 890bb70920d6
line wrap: on
line source
# Copyright (C) 2007, 2008 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""Build binary packages from source packages"""

import os
import logging

import util
import run
from cmdexpand import cmdexpand


class PBuilder(object):

    """Represents a way to run and manage a specific pbuilder instance"""

    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
        be used to get root permissions to run pbuilder.  It may be an
        empty list if no command is needed.  It's a list so that
        commands with several shell-words can be used without having to
        worry about quoting.
        """
        self.pbuilderrc = pbuilderrc
        self.root_cmd = root_cmd

    def build(self, dsc_file, binary_dir, logfile):
        """Build a binary packager from a source package
        Parameters:
           dsc_file -- name of the debian .dsc file of the source package
           binary_dir -- name of the directory to receive the binary packages
           logfile -- name of the logfile of the build
        """
        util.ensure_directory(binary_dir)
        run.call(cmdexpand("@rootcmd /usr/sbin/pbuilder build"
                           " --configfile $pbuilderrc"
                           " --logfile $logfile --buildresult $bindir $dsc",
                           rootcmd=self.root_cmd, pbuilderrc=self.pbuilderrc,
                           logfile=logfile, bindir=binary_dir, dsc=dsc_file),
                 suppress_output=True)
        # remove the source package files put into the binary directory
        # by pbuilder
        for filename in os.listdir(binary_dir):
            if os.path.splitext(filename)[1] not in (".deb", ".changes"):
                os.remove(os.path.join(binary_dir, filename))

    def run_script(self, script, logfile, bindmounts=()):
        """Execute a script in pbuilder's chroot environment
        Parameters:
           script -- The filename of the script
           logfile -- name of the logfile of the build
           bindmounts -- Sequence of directory names that should be
                         bind-mounted in the pbuilder chroot
                         environment (optional)
        """
        logging.info("Running pbuilder execute on %s", script)
        args = []
        if logfile:
            args.extend(["--logfile", logfile])
        for mount in bindmounts:
            args.extend(["--bindmounts", mount])

        run.call(cmdexpand("@rootcmd /usr/sbin/pbuilder execute"
                           " --configfile $pbuilderrc @args $script",
                           rootcmd=self.root_cmd, pbuilderrc=self.pbuilderrc,
                           args=args, script=script),
                 suppress_output=False)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)