-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilder.py
142 lines (118 loc) · 4.69 KB
/
builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# builder.py -- Classes for building packages
# Copyright (C) 2006, 2007 James Westby <jw+debian@jameswestby.net>
#
# This file is part of breezy-debian.
#
# bzr-builddeb is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# bzr-builddeb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bzr-builddeb; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import shutil
import subprocess
import os
import tempfile
from ...errors import BzrError
from ...trace import note
from .hooks import run_hook
from .util import (
get_parent_dir,
subprocess_setup,
find_changes_files,
dget_changes,
)
class ChangesFileMissing(BzrError):
_fmt = "Missing changes file."
class NoSourceDirError(BzrError):
_fmt = ("There is no existing source directory to use. Use "
"--export-only or --dont-purge to get one that can be used")
class BuildFailedError(BzrError):
_fmt = "The build failed."
class DebBuild:
"""The object that does the building work."""
def __init__(self, distiller, target_dir, builder, use_existing=False):
"""Create a builder.
:param distiller: the SourceDistiller that will get the source to
build.
:param target_dir: the directory in which to do all the work.
:param builder: the build command to use.
:param use_existing: whether to re-use the target_dir if it exists.
"""
self.distiller = distiller
self.target_dir = target_dir
self.builder = builder
self.use_existing = use_existing
def prepare(self):
"""Do any preparatory steps that should be run before the build.
It checks that everything is well, and that some needed dirs are
created.
"""
parent_dir = get_parent_dir(self.target_dir)
if parent_dir != '' and not os.path.exists(parent_dir):
os.makedirs(parent_dir)
if os.path.exists(self.target_dir):
if not self.use_existing:
note("Purging the build dir: %s", self.target_dir)
shutil.rmtree(self.target_dir)
else:
note("Not purging build dir as requested: %s",
self.target_dir)
else:
if self.use_existing:
raise NoSourceDirError
def export(self):
self.distiller.distill(self.target_dir)
def before_build(self):
subprocess.check_call(
['dpkg-source', '--before-build', self.target_dir],
preexec_fn=subprocess_setup)
def after_build(self):
subprocess.check_call(
['dpkg-source', '--after-build', self.target_dir],
preexec_fn=subprocess_setup, )
def build(self):
"""This builds the package using the supplied command."""
note("Building the package in %s, using %s", self.target_dir,
self.builder)
proc = subprocess.Popen(
self.builder, shell=True, cwd=self.target_dir,
preexec_fn=subprocess_setup)
proc.wait()
if proc.returncode != 0:
raise BuildFailedError
def clean(self):
"""This removes the build directory."""
note("Cleaning build dir: %s", self.target_dir)
shutil.rmtree(self.target_dir)
def do_build(package_name, version, distiller, local_tree, config,
build_command, target_dir=None):
"""Actually run a build."""
with tempfile.TemporaryDirectory() as bd:
build_source_dir = os.path.join(
bd, package_name + "-" + version.upstream_version)
builder = DebBuild(
distiller, build_source_dir,
build_command,
use_existing=False)
builder.prepare()
run_hook(local_tree, 'pre-export', config)
builder.export()
run_hook(local_tree, 'pre-build', config, wd=build_source_dir)
builder.build()
run_hook(local_tree, 'post-build', config, wd=build_source_dir)
if target_dir is not None:
ret = {}
for kind, entry in find_changes_files(bd, package_name, version):
ret[kind] = dget_changes(entry.path, target_dir)
if not ret:
raise ChangesFileMissing()
return ret