/
build.py
306 lines (264 loc) · 11.7 KB
/
build.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/true
#
# build.py - part of autospec
# Copyright (C) 2015 Intel Corporation
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# Actually build the package
#
import os
import re
import shutil
import util
def cleanup_req(s: str) -> str:
"""Strip unhelpful strings from requirements."""
if "is wanted" in s:
s = ""
if "should be defined" in s:
s = ""
if "are broken" in s:
s = ""
if "is broken" in s:
s = ""
if s[0:4] == 'for ':
s = s[4:]
s = s.replace(" works as expected", "")
s = s.replace(" and usability", "")
s = s.replace(" usability", "")
s = s.replace(" argument", "")
s = s.replace(" environment variable", "")
s = s.replace(" environment var", "")
s = s.replace(" presence", "")
s = s.replace(" support", "")
s = s.replace(" implementation is broken", "")
s = s.replace(" is broken", "")
s = s.replace(" files can be found", "")
s = s.replace(" can be found", "")
s = s.replace(" is declared", "")
s = s.replace("whether to build ", "")
s = s.replace("whether ", "")
s = s.replace("library containing ", "")
s = s.replace("x86_64-generic-linux-gnu-", "")
s = s.replace("i686-generic-linux-gnu-", "")
s = s.replace("'", "")
s = s.strip()
return s
def check_for_warning_pattern(line):
"""Print warning if a line matches against a warning list."""
warning_patterns = [
"march=native"
]
for pat in warning_patterns:
if pat in line:
util.print_warning("Build log contains: {}".format(pat))
def get_mock_cmd():
"""Set mock command to use sudo as needed."""
# Some distributions (e.g. Fedora) use consolehelper to run mock,
# while others (e.g. Clear Linux) expect the user run it via sudo.
if os.path.basename(os.path.realpath('/usr/bin/mock')) == 'consolehelper':
return '/usr/bin/mock'
return 'sudo /usr/bin/mock'
class Build(object):
"""Manage package builds."""
def __init__(self):
"""Initialize default build settings."""
self.success = 0
self.round = 0
self.must_restart = 0
self.file_restart = 0
self.uniqueext = ''
self.warned_about = set()
self.patch_name_line = re.compile(r'^Patch #[0-9]+ \((.*)\):$')
self.patch_fail_line = re.compile(r'^Skipping patch.$')
def simple_pattern_pkgconfig(self, line, pattern, pkgconfig, conf32, requirements):
"""Check for pkgconfig patterns and restart build as needed."""
pat = re.compile(pattern)
match = pat.search(line)
if match:
self.must_restart += requirements.add_pkgconfig_buildreq(pkgconfig, conf32, cache=True)
def simple_pattern(self, line, pattern, req, requirements):
"""Check for simple patterns and restart the build as needed."""
pat = re.compile(pattern)
match = pat.search(line)
if match:
self.must_restart += requirements.add_buildreq(req, cache=True)
def failed_pattern(self, line, config, requirements, pattern, verbose, buildtool=None):
"""Check against failed patterns to restart build as needed."""
pat = re.compile(pattern)
match = pat.search(line)
if not match:
return
s = match.group(1)
# standard configure cleanups
s = cleanup_req(s)
if s in config.ignored_commands:
return
try:
if not buildtool:
req = config.failed_commands[s]
if req:
self.must_restart += requirements.add_buildreq(req, cache=True)
elif buildtool == 'pkgconfig':
self.must_restart += requirements.add_pkgconfig_buildreq(s, config.config_opts.get('32bit'), cache=True)
elif buildtool == 'R':
if requirements.add_buildreq("R-" + s, cache=True) > 0:
self.must_restart += 1
elif buildtool == 'perl':
s = s.replace('inc::', '')
self.must_restart += requirements.add_buildreq('perl(%s)' % s, cache=True)
elif buildtool == 'pypi':
s = util.translate(s)
if not s:
return
self.must_restart += requirements.add_buildreq(f"pypi({s.lower().replace('-', '_')})", cache=True)
elif buildtool == 'catkin':
self.must_restart += requirements.add_pkgconfig_buildreq(s, config.config_opts.get('32bit'), cache=True)
self.must_restart += requirements.add_buildreq(s, cache=True)
except Exception:
if s.strip() and s not in self.warned_about and s[:2] != '--':
util.print_warning(f"Unknown pattern match: {s}")
self.warned_about.add(s)
def parse_buildroot_log(self, filename, returncode):
"""Handle buildroot log contents."""
if returncode == 0:
return True
self.must_restart = 0
self.file_restart = 0
is_clean = True
util.call("sync")
with util.open_auto(filename, "r") as rootlog:
loglines = rootlog.readlines()
missing_pat = re.compile(r"^.*No matching package to install: '(.*)'$")
for line in loglines:
match = missing_pat.match(line)
if match is not None:
util.print_fatal("Cannot resolve dependency name: {}".format(match.group(1)))
is_clean = False
return is_clean
def parse_build_results(self, filename, returncode, filemanager, config, requirements, content):
"""Handle build log contents."""
requirements.verbose = 1
self.must_restart = 0
self.file_restart = 0
infiles = 0
patch_name = ""
# Flush the build-log to disk, before reading it
util.call("sync")
with util.open_auto(filename, "r") as buildlog:
loglines = buildlog.readlines()
for line in loglines:
if patch_name_match := self.patch_name_line.search(line):
patch_name = patch_name_match.groups()[0]
if patch_name:
if self.patch_fail_line.search(line):
self.must_restart += config.remove_backport_patch(patch_name)
for pat in config.pkgconfig_pats:
self.simple_pattern_pkgconfig(line, *pat, config.config_opts.get('32bit'), requirements)
for pat in config.simple_pats:
self.simple_pattern(line, *pat, requirements)
for pat in config.failed_pats:
self.failed_pattern(line, config, requirements, *pat)
check_for_warning_pattern(line)
# Search for files to add to the %files section.
# * infiles == 0 before we reach the files listing
# * infiles == 1 for the "Installed (but unpackaged) file(s) found" header
# and for the entirety of the files listing
# * infiles == 2 after the files listing has ended
if infiles == 1:
for search in ["RPM build errors", "Childreturncodewas",
"Child returncode", "Empty %files file"]:
if search in line:
infiles = 2
for start in ["Building", "Child return code was"]:
if line.startswith(start):
infiles = 2
if infiles == 0 and "Installed (but unpackaged) file(s) found:" in line:
infiles = 1
elif infiles == 1 and "not matching the package arch" not in line:
# exclude blank lines from consideration...
file = line.strip()
if file and file[0] == "/":
filemanager.push_file(file, content.name)
if line.startswith("Sorry: TabError: inconsistent use of tabs and spaces in indentation"):
print(line)
returncode = 99
nvr = f"{content.name}-{content.version}-{content.release}"
match = f"File not found: /builddir/build/BUILDROOT/{nvr}.x86_64/"
if match in line:
missing_file = "/" + line.split(match)[1].strip()
filemanager.remove_file(missing_file)
if line.startswith("Executing(%clean") and returncode == 0:
print("RPM build successful")
self.success = 1
def package(self, filemanager, mockconfig, mockopts, config, requirements, content, cleanup=False):
"""Run main package build routine."""
self.round += 1
self.success = 0
mock_cmd = get_mock_cmd()
print("Building package " + content.name + " round", self.round)
self.uniqueext = content.name
if cleanup:
cleanup_flag = "--cleanup-after"
else:
cleanup_flag = "--no-cleanup-after"
print("{} mock chroot at /var/lib/mock/clear-{}".format(content.name, self.uniqueext))
if self.round == 1:
shutil.rmtree('{}/results'.format(config.download_path), ignore_errors=True)
os.makedirs('{}/results'.format(config.download_path))
cmd_args = [
mock_cmd,
f"--root={mockconfig}",
"--buildsrpm",
"--sources=./",
f"--spec={content.name}.spec",
f"--uniqueext={self.uniqueext}-src",
"--result=results/",
cleanup_flag,
mockopts,
]
util.call(" ".join(cmd_args),
logfile=f"{config.download_path}/results/mock_srpm.log",
cwd=config.download_path)
# back up srpm mock logs
util.call("mv results/root.log results/srpm-root.log", cwd=config.download_path)
util.call("mv results/build.log results/srpm-build.log", cwd=config.download_path)
srcrpm = f"results/{content.name}-{content.version}-{content.release}.src.rpm"
cmd_args = [
mock_cmd,
f"--root={mockconfig}",
"--result=results/",
srcrpm,
"--enable-plugin=ccache",
f"--uniqueext={self.uniqueext}",
cleanup_flag,
mockopts,
]
if not cleanup and self.must_restart == 0 and self.file_restart > 0 and set(filemanager.excludes) == set(filemanager.manual_excludes):
cmd_args.append("--no-clean")
cmd_args.append("--short-circuit=binary")
ret = util.call(" ".join(cmd_args),
logfile=f"{config.download_path}/results/mock_build.log",
check=False,
cwd=config.download_path)
# sanity check the build log
if not os.path.exists(config.download_path + "/results/build.log"):
util.print_fatal("Mock command failed, results log does not exist. User may not have correct permissions.")
exit(1)
if not self.parse_buildroot_log(config.download_path + "/results/root.log", ret):
return
self.parse_build_results(config.download_path + "/results/build.log", ret, filemanager, config, requirements, content)
if filemanager.has_banned:
util.print_fatal("Content in banned paths found, aborting build")
exit(1)