~debian-bazaar/debian/sid/breezy-debian/unstable

18.1.39 by James Westby
Place the package under the GPL
1
#    builder.py -- Classes for building packages
95.1.1 by James Westby
Add the start of a test suite.
2
#    Copyright (C) 2006, 2007 James Westby <jw+debian@jameswestby.net>
18.1.39 by James Westby
Place the package under the GPL
3
#    
4
#    This file is part of bzr-builddeb.
5
#
75.1.2 by Jelmer Vernooij
Fix some typos
6
#    bzr-builddeb is free software; you can redistribute it and/or modify
18.1.39 by James Westby
Place the package under the GPL
7
#    it under the terms of the GNU General Public License as published by
8
#    the Free Software Foundation; either version 2 of the License, or
9
#    (at your option) any later version.
10
#
11
#    bzr-builddeb is distributed in the hope that it will be useful,
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#    GNU General Public License for more details.
15
#
16
#    You should have received a copy of the GNU General Public License
17
#    along with bzr-builddeb; if not, write to the Free Software
18
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
18.1.35 by James Westby
Refactored code to use multiple files.
20
750 by Jelmer Vernooij
use relative imports.
21
from __future__ import absolute_import
22
18.1.35 by James Westby
Refactored code to use multiple files.
23
import shutil
178.1.9 by James Westby
Use subprocess for building as it is cleaner and saves on chdirs.
24
import subprocess
18.1.35 by James Westby
Refactored code to use multiple files.
25
import os
26
750 by Jelmer Vernooij
use relative imports.
27
from ...trace import note
327.2.7 by James Westby
Move to using SourceDistiller.
28
750 by Jelmer Vernooij
use relative imports.
29
from .errors import (
30
    NoSourceDirError,
31
    BuildFailedError,
32
    )
33
from .quilt import quilt_push_all
34
from .util import (
678.1.6 by Jelmer Vernooij
Automatically apply patches for 3.0 (quilt) packages in 'bzr bd-do'
35
    get_parent_dir,
36
    subprocess_setup,
37
    FORMAT_3_0_QUILT,
38
    )
266.1.24 by James Westby
Handle result and build dirs being the same better.
39
40
18.1.35 by James Westby
Refactored code to use multiple files.
41
class DebBuild(object):
327.2.7 by James Westby
Move to using SourceDistiller.
42
    """The object that does the building work."""
43
44
    def __init__(self, distiller, target_dir, builder, use_existing=False):
45
        """Create a builder.
46
47
        :param distiller: the SourceDistiller that will get the source to
48
            build.
49
        :param target_dir: the directory in which to do all the work.
50
        :param builder: the build command to use.
51
        :param use_existing: whether to re-use the target_dir if it exists.
52
        """
53
        self.distiller = distiller
54
        self.target_dir = target_dir
55
        self.builder = builder
56
        self.use_existing = use_existing
57
58
    def prepare(self):
59
        """Do any preparatory steps that should be run before the build.
60
61
        It checks that everything is well, and that some needed dirs are
62
        created.
63
        """
327.1.33 by James Westby
Add tests for UpstreamProvider.
64
        parent_dir = get_parent_dir(self.target_dir)
327.2.7 by James Westby
Move to using SourceDistiller.
65
        if parent_dir != '' and not os.path.exists(parent_dir):
66
            os.makedirs(parent_dir)
67
        if os.path.exists(self.target_dir):
68
            if not self.use_existing:
379.1.1 by Jelmer Vernooij
Use bzrlib.trace.note rather than deprecated bzrlib.trace.info.
69
                note("Purging the build dir: %s", self.target_dir)
327.2.7 by James Westby
Move to using SourceDistiller.
70
                shutil.rmtree(self.target_dir)
71
            else:
379.1.1 by Jelmer Vernooij
Use bzrlib.trace.note rather than deprecated bzrlib.trace.info.
72
                note("Not purging build dir as requested: %s",
327.2.7 by James Westby
Move to using SourceDistiller.
73
                        self.target_dir)
74
        else:
75
            if self.use_existing:
76
                raise NoSourceDirError
77
678.1.6 by Jelmer Vernooij
Automatically apply patches for 3.0 (quilt) packages in 'bzr bd-do'
78
    def export(self, apply_quilt_patches=True):
327.2.7 by James Westby
Move to using SourceDistiller.
79
        self.distiller.distill(self.target_dir)
678.1.6 by Jelmer Vernooij
Automatically apply patches for 3.0 (quilt) packages in 'bzr bd-do'
80
        if apply_quilt_patches:
81
            self._apply_quilt_patches()
82
83
    def _apply_quilt_patches(self):
84
        if not os.path.isfile(os.path.join(self.target_dir, "debian/patches/series")):
85
            return
86
        format_path = os.path.join(self.target_dir, "debian/source/format")
87
        if not os.path.isfile(format_path):
88
            return
89
        with file(format_path, 'r') as f:
90
            if f.read().strip() == FORMAT_3_0_QUILT:
91
                quilt_push_all(os.path.abspath(self.target_dir))
327.2.7 by James Westby
Move to using SourceDistiller.
92
93
    def build(self):
94
        """This builds the package using the supplied command."""
379.1.1 by Jelmer Vernooij
Use bzrlib.trace.note rather than deprecated bzrlib.trace.info.
95
        note("Building the package in %s, using %s", self.target_dir,
327.2.7 by James Westby
Move to using SourceDistiller.
96
                self.builder)
354 by James Westby
Also avoid problems with SIGPIPE when running the build command.
97
        proc = subprocess.Popen(self.builder, shell=True, cwd=self.target_dir,
98
                preexec_fn=subprocess_setup)
178.1.33 by James Westby
Look for the upstream tarball in the archives.
99
        proc.wait()
100
        if proc.returncode != 0:
327.2.7 by James Westby
Move to using SourceDistiller.
101
            raise BuildFailedError
102
103
    def clean(self):
104
        """This removes the build directory."""
379.1.1 by Jelmer Vernooij
Use bzrlib.trace.note rather than deprecated bzrlib.trace.info.
105
        note("Cleaning build dir: %s", self.target_dir)
327.2.7 by James Westby
Move to using SourceDistiller.
106
        shutil.rmtree(self.target_dir)