-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebrebuild.py
executable file
·1261 lines (1125 loc) · 47.8 KB
/
debrebuild.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
#
# Copyright (C) 2021 Frédéric Pierret (fepitre) <[email protected]>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
import json
import logging
import os
import shutil
import subprocess
import sys
import tempfile
from shlex import quote, join
import apt
import apt_pkg
import debian.deb822
import debian.debian_support
import requests
import rstr
from dateutil.parser import parse as parsedate
from lib.downloads import download_with_retry
from lib.openpgp import OpenPGPEnvironment, OpenPGPException
logger = logging.getLogger("debrebuild")
console_handler = logging.StreamHandler(sys.stderr)
logger.addHandler(console_handler)
DEBIAN_KEYRINGS = [
"/usr/share/keyrings/debian-archive-bullseye-automatic.gpg",
"/usr/share/keyrings/debian-archive-bullseye-security-automatic.gpg",
"/usr/share/keyrings/debian-archive-bullseye-stable.gpg",
"/usr/share/keyrings/debian-archive-buster-automatic.gpg",
"/usr/share/keyrings/debian-archive-buster-security-automatic.gpg",
"/usr/share/keyrings/debian-archive-buster-stable.gpg",
"/usr/share/keyrings/debian-archive-keyring.gpg",
"/usr/share/keyrings/debian-archive-removed-keys.gpg",
"/usr/share/keyrings/debian-archive-stretch-automatic.gpg",
"/usr/share/keyrings/debian-archive-stretch-security-automatic.gpg",
"/usr/share/keyrings/debian-archive-stretch-stable.gpg",
"/usr/share/keyrings/debian-ports-archive-keyring-removed.gpg",
"/usr/share/keyrings/debian-ports-archive-keyring.gpg",
"/usr/share/keyrings/debian-keyring.gpg",
]
# Adapted from reproducible-builds/reprotest
def run_or_tee(progargs, filename, store_dir, *args, **kwargs):
if store_dir:
tee = subprocess.Popen(
["tee", filename],
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
cwd=store_dir,
)
r = subprocess.run(progargs, *args, stdout=tee.stdin, **kwargs)
tee.communicate()
return r
else:
return subprocess.run(progargs, *args, **kwargs)
class PackageException(Exception):
pass
class BuildInfoException(Exception):
pass
class RebuilderException(Exception):
pass
class RebuilderInTotoError(Exception):
pass
class RebuilderChecksumsError(Exception):
pass
class Package:
def __init__(
self,
name,
version,
architecture=None,
archive_name="debian",
suite_name="unstable",
component_name="main",
):
self.name = name
self.version = version
self.architecture = architecture
self.archive_name = archive_name
self.timestamp = None
self.suite_name = suite_name
self.component_name = component_name
self.hash = None
def to_index_format(self):
if self.architecture:
result = f"{self.name} {self.version} {self.architecture}"
else:
result = f"{self.name} {self.version}"
return result
def to_apt_install_format(self, build_arch=None):
result = f"{self.name}={self.version}"
if build_arch and self.architecture in ("all", build_arch):
result = f"{self.name}:{self.architecture}={self.version}"
return result
def __repr__(self):
return f"Package({self.name}, {self.version}, architecture={self.architecture})"
class RebuilderBuildInfo:
def __init__(self, buildinfo_file):
if not os.path.exists(buildinfo_file):
raise BuildInfoException(f"Cannot find buildinfo file: {buildinfo_file}")
with open(buildinfo_file) as fd:
self.parsed_info = debian.deb822.BuildInfo(fd)
# in case of binnmu we have e.g.
# Source: 0ad (0.0.23-1)
self.source, self.source_version = self.parsed_info.get_source()
self.architecture = [
arch
for arch in self.parsed_info.get_architecture()
if arch not in ("source", "all")
]
if len(self.architecture) > 1:
raise BuildInfoException("More than one architecture in Architecture field")
self.binary = self.parsed_info.get_binary()
self.version = self.parsed_info["version"]
if not self.source_version:
self.source_version = self.version
if ":" in self.version:
self.version = self.version.split(":")[1]
self.build_path = self.parsed_info.get("build-path", None)
self.build_arch = self.parsed_info.get("build-architecture", None)
if not self.build_arch:
raise BuildInfoException("Need Build-Architecture field")
self.build_date = self.parsed_info.get_build_date().strftime("%Y%m%dT%H%M%SZ")
self.host_arch = self.parsed_info.get("host-architecture", self.build_arch)
self.env = self.parsed_info.get_environment()
self.build_source = self.parsed_info.is_build_source()
self.build_archall = self.parsed_info.is_build_arch_all()
self.build_archany = self.parsed_info.is_build_arch_any()
self.checksums = {}
for alg in ("md5", "sha1", "sha256", "sha512"):
if self.parsed_info.get(f"checksums-{alg}", None):
self.checksums[alg] = self.parsed_info[f"checksums-{alg}"]
self.logentry = self.parsed_info.get_changelog()
if self.logentry:
# Due to storing the binnmu changelog entry in deb822 buildinfo,
# the first character is an unwanted newline
self.logentry = str(self.logentry).lstrip("\n")
# while the linebreak at the beginning is wrong, there are one
# missing at the end
self.logentry += "\n"
self.build_depends = []
self.required_timestamps = {}
self.archive_name = None
self.source_date = None
self.suite_name = None
self.component_name = None
def get_debian_suite(self):
"""Returns the Debian suite suited for debootstraping the build
environment as described by the .buildinfo file.
(For *re*building we cannot base upon packages from sid as else
we might be forced to downgrades which are not supported.)
This is then used by rebuilders usage of debootstrap for
rebuilding the underling packages.
"""
debian_suite = "sid"
for pkg in self.parsed_info.relations["installed-build-depends"]:
if pkg[0]["name"] == "base-files":
_, version = pkg[0]["version"]
try:
version = str(int(float(version)))
except ValueError:
break
for rel in debian.debian_support._release_list.values():
if rel and rel.version == version:
debian_suite = str(rel)
break
return debian_suite
def get_build_path(self):
if not self.build_path:
self.build_path = f"/build/{self.source}-{rstr.letters(10)}"
self.build_path = self.build_path.replace("~", "-")
return self.build_path
def get_build_depends(self):
# Storing self.build_depends is needed as we refresh information
# from apt cache
if not self.build_depends:
installed = self.parsed_info.relations["installed-build-depends"]
for dep in installed:
name = dep[0]["name"]
_, version = dep[0]["version"]
self.build_depends.append(Package(name, version))
return self.build_depends
class Rebuilder:
def __init__(
self,
buildinfo_file,
snapshot_url,
snapshot_mirror,
extra_repository_files=None,
extra_repository_keys=None,
gpg_sign_keyid=None,
gpg_verify=False,
gpg_verify_key=None,
proxy=None,
use_metasnap=False,
metasnap_url="http://snapshot.notset.fr",
build_options_nocheck=False,
):
self.buildinfo = None
self.snapshot_url = snapshot_url
self.base_mirror = f"{snapshot_mirror}/archive"
self.extra_repository_files = extra_repository_files
self.extra_repository_keys = extra_repository_keys
self.gpg_sign_keyid = gpg_sign_keyid
self.proxy = proxy
self.session = requests.Session()
self.session.proxies = {"http:": self.proxy, "https": self.proxy}
self.use_metasnap = use_metasnap
self.metasnap_url = metasnap_url
self.build_options_nocheck = build_options_nocheck
self.tempaptdir = None
self.tempaptcache = None
self.required_timestamp_sources = {}
self.tmpdir = os.environ.get("TMPDIR", "/tmp")
self.buildinfo_file = None
logger.debug(f"Input buildinfo: {buildinfo_file}")
if buildinfo_file.startswith("http://") or buildinfo_file.startswith(
"https://"
):
resp = self.get_response(buildinfo_file)
if not resp.ok:
raise RebuilderException(f"Cannot get buildinfo: {resp.reason}")
# We store remote buildinfo in a temporary file
handle, self.buildinfo_file = tempfile.mkstemp(
prefix="buildinfo-", dir=self.tmpdir
)
with open(handle, "w") as fd:
fd.write(resp.text)
else:
self.buildinfo_file = realpath(buildinfo_file)
if gpg_verify and gpg_verify_key:
gpg_env = OpenPGPEnvironment()
try:
gpg_env.import_key(gpg_verify_key)
data = gpg_env.verify_file(self.buildinfo_file)
logger.info(f"GPG ({data.primary_key_fingerprint}): OK")
except OpenPGPException as e:
raise RebuilderException(f"Failed to verify buildinfo: {str(e)}")
finally:
gpg_env.close()
self.buildinfo = RebuilderBuildInfo(self.buildinfo_file)
def get_env(self):
env = []
for key, val in self.buildinfo.env.items():
env.append(f'{key}="{val}"')
if self.build_options_nocheck:
env.append("DEB_BUILD_OPTIONS=nocheck")
return env
def get_response(self, url):
try:
resp = self.session.get(url)
except requests.exceptions.ConnectionError as e:
# logger.error(f"Failed to get URL {url}: {str(e)}")
# WIP: forge a better response?
resp = requests.models.Response()
resp.status_code = 503
resp.reason = str(e)
return resp
def download_from_snapshot(self, path, sha256):
url = f"{self.snapshot_url}/mr/file/{sha256}/download"
if not requests.head(url, timeout=10).ok:
raise RebuilderException(f"Cannot find URL: {url}")
return download_with_retry(url, path, sha256)
# TODO: refactor get_src_date and get_bin_date. Do a better distinction between "BuildInfo"
# and the source package which as to be defined.
def get_src_date(self):
if all(
[
self.buildinfo.archive_name,
self.buildinfo.source_date,
self.buildinfo.suite_name,
self.buildinfo.component_name,
]
):
return (
self.buildinfo.archive_name,
self.buildinfo.source_date,
self.buildinfo.suite_name,
self.buildinfo.component_name,
)
srcpkgname = self.buildinfo.source
srcpkgver = self.buildinfo.source_version
json_url = f"{self.snapshot_url}/mr/package/{srcpkgname}/{srcpkgver}/srcfiles?fileinfo=1"
logger.debug(f"Get source package info: {srcpkgname}={srcpkgver}")
logger.debug(f"Source URL: {json_url}")
resp = self.get_response(json_url)
try:
data = resp.json()
except json.decoder.JSONDecodeError:
raise RebuilderException(
f"Cannot parse response for source: {self.buildinfo.source}"
)
source_info = None
for h in data.get("fileinfo", {}).values():
# We pick the first dsc found.
for f in h:
if f["name"].endswith(".dsc"):
source_info = f
break
if source_info:
break
if not source_info:
raise RebuilderException(
f"No source info found for {srcpkgname}-{srcpkgver}"
)
self.buildinfo.archive_name = source_info["archive_name"]
self.buildinfo.source_date = source_info["timestamp_ranges"][0][0]
self.buildinfo.suite_name = source_info["suite_name"]
self.buildinfo.component_name = source_info["component_name"]
return (
self.buildinfo.archive_name,
self.buildinfo.source_date,
self.buildinfo.suite_name,
self.buildinfo.component_name,
)
def get_bin_date(self, package):
pkgname = package.name
pkgver = package.version
pkgarch = package.architecture
json_url = (
f"{self.snapshot_url}/mr/binary/{pkgname}/{pkgver}/binfiles?fileinfo=1"
)
logger.debug(f"Get binary package info: {pkgname}={pkgver}")
logger.debug(f"Binary URL: {json_url}")
resp = self.get_response(json_url)
try:
data = resp.json()
except json.decoder.JSONDecodeError:
raise RebuilderException(
f"Cannot parse response for package: {package.name}"
)
pkghash = None
if len(data.get("result", [])) == 1:
pkghash = data["result"][0]["hash"]
package.architecture = data["result"][0]["architecture"]
if pkgarch and pkgarch != package.architecture:
raise RebuilderException(
f"Package {pkgname} was explicitly requested "
f"{pkgarch} but only {package.architecture} was found"
)
if (
not pkgarch
and self.buildinfo.build_arch != package.architecture
and "all" != package.architecture
):
raise RebuilderException(
f"Package {pkgname} was implicitly requested "
f"{self.buildinfo.build_arch} but only "
f"{package.architecture} was found"
)
pkgarch = package.architecture
else:
if not pkgarch:
pkgarch = self.buildinfo.build_arch
for result in data.get("result", []):
if result["architecture"] == pkgarch:
pkghash = result["hash"]
break
if not pkghash:
raise RebuilderException(
f"Cannot find package in architecture {pkgarch}"
)
package.architecture = pkgarch
binary_info = [pkg for pkg in data["fileinfo"].get(pkghash, [])]
if not binary_info:
raise RebuilderException(
f"No binary info found for {pkgname}:{pkgarch}-{pkgver}"
)
package.hash = pkghash
package.archive_name = binary_info[0]["archive_name"]
package.timestamp = binary_info[0]["timestamp_ranges"][0][0]
package.suite_name = binary_info[0]["suite_name"]
package.component_name = binary_info[0]["component_name"]
return (
package.archive_name,
package.timestamp,
package.suite_name,
package.component_name,
)
def get_sources_list(self):
"""
Returns a list of all inline Debian repositories for to the package
to be rebuilt (not dependencies)
"""
sources_list = []
archive_name, source_date, dist, component = self.get_src_date()
build_url = f"{self.base_mirror}/{archive_name}/{source_date}"
# Add deb repository
release_url = f"{build_url}/dists/{dist}/Release"
resp = self.get_response(release_url)
if not resp.ok:
RebuilderException(f"Cannot fetch {dist} Release file: {release_url}")
build_repo = f"deb {build_url}/ {dist} {component}"
sources_list.append(build_repo)
# Add deb-src repository
source_release_url = f"{build_url}/dists/{dist}/main/source/Release"
resp = self.get_response(source_release_url)
if not resp.ok:
RebuilderException(
f"Cannot fetch {dist} Release file: {source_release_url}"
)
source_repo = f"deb-src {build_url}/ {dist} main"
sources_list.append(source_repo)
if self.extra_repository_files:
for repo_file in self.extra_repository_files:
try:
with open(repo_file) as fd:
for line in fd:
if not line.startswith("#") and not line.startswith("\n"):
sources_list.append(line.rstrip("\n"))
except FileNotFoundError:
raise RebuilderException(
f"Cannot find repository file: {repo_file}"
)
return sources_list
def get_sources_list_timestamps(self):
"""
Returns all timestamp inline Debian repositories for all archives
"""
sources_list = []
for sources in self.required_timestamp_sources.values():
sources_list += sources
return sources_list
def find_build_dependencies_from_metasnap(self):
status = False
files = {"buildinfo": open(self.buildinfo_file, "rb")}
# It means someone wants to use original metasnap service which is not
# compatible with the default JSON layout from snapshot.notset.fr
if "metasnap.debian.net" in self.metasnap_url:
metasnap_endpoint = f"{self.metasnap_url}/cgi-bin/api"
else:
metasnap_endpoint = f"{self.metasnap_url}/mr/buildinfo"
try:
resp = self.session.post(metasnap_endpoint, files=files)
if not resp.ok:
msg = f"{resp.status_code} ({resp.reason})"
else:
status = True
except requests.exceptions.ConnectionError as e:
msg = str(e)
pass
if not status:
logger.error(f"Cannot get timestamps from metasnap: {msg}")
return
if "metasnap.debian.net" in self.metasnap_url:
# It means someone wants to use original metasnap service which is not
# compatible with the default JSON layout from snapshot.notset.fr
# latest first
content = reversed(resp.text.strip("\n").split("\n"))
for line in content:
arch, timestamp = line.split()
if arch != self.buildinfo.build_arch:
raise RebuilderException("Unable to handle multiple architectures")
self.required_timestamp_sources.setdefault(
"debian+unstable+main", []
).append(f"deb {self.base_mirror}/debian/{timestamp}/ unstable main")
# We store timestamp value itself for the base mirror used for creating chroot
self.buildinfo.required_timestamps.setdefault(
"debian+unstable+main", []
).append(timestamp)
else:
try:
content = resp.json()["results"]
except Exception as e:
logger.error(f"Cannot get timestamps from metasnap: {str(e)}")
return
timestamps_sets = sorted(content, key=lambda x: len(x["timestamps"]))
archive = timestamps_sets[0]["archive_name"]
suite = timestamps_sets[0]["suite_name"]
component = timestamps_sets[0]["component_name"]
key = f"{archive}+{suite}+{component}"
for timestamp in timestamps_sets[0]["timestamps"]:
self.required_timestamp_sources.setdefault(key, []).append(
f"deb {self.base_mirror}/{archive}/{timestamp}/ {suite} {component}"
)
self.buildinfo.required_timestamps.setdefault(key, []).append(timestamp)
def get_build_depends_timestamps(self):
"""
Returns a dict with keys Debian archives and
values lists of tuple(timestamp, pkgs)
where pkgs is a list of packages living there
"""
required_timestamps = {}
for pkg in self.buildinfo.get_build_depends():
if not pkg.timestamp:
self.get_bin_date(pkg)
timestamp = parsedate(pkg.timestamp).strftime("%Y%m%dT%H%M%SZ")
location = f"{pkg.archive_name}+{pkg.suite_name}+{pkg.component_name}"
required_timestamps.setdefault(location, {}).setdefault(
timestamp, []
).append(pkg)
# We store timestamp value itself for the base mirror used for creating chroot
self.buildinfo.required_timestamps.setdefault(location, []).append(
timestamp
)
location_required_timestamps = {}
for location, timestamps in required_timestamps.items():
# sort by the number of packages found there, convert to list of tuples
timestamps = sorted(
timestamps.items(), key=lambda x: len(x[1]), reverse=True
)
location_required_timestamps[location] = timestamps
return location_required_timestamps
def get_sources_list_from_timestamp(self):
"""
Returns a dict with keys archive+suite+component and
values lists inline Debian repositories
"""
sources_list = {}
for location, timestamps in self.get_build_depends_timestamps().items():
for timestamp, pkgs in timestamps:
archive, suite, component = location.split("+", 3)
sources_list.setdefault(location, []).append(
(
f"deb {self.base_mirror}/{archive}/{timestamp}/ {suite} {component}",
pkgs,
)
)
return sources_list
def find_build_dependencies(self):
# Prepare APT cache for finding dependencies
self.prepare_aptcache()
notfound_packages = [pkg for pkg in self.buildinfo.get_build_depends()]
temp_sources_list = self.tempaptdir + "/etc/apt/sources.list"
with open(temp_sources_list, "a") as fd:
for (
location,
repositories,
) in self.get_sources_list_from_timestamp().items():
for timestamp_source, pkgs in repositories:
if not notfound_packages:
break
if not any(
pkg.to_apt_install_format()
in [p.to_apt_install_format() for p in notfound_packages]
for pkg in pkgs
):
logger.info(f"Skipping snapshot: {timestamp_source}")
continue
logger.info(
f"Remaining packages to be found: {len(notfound_packages)}"
)
self.required_timestamp_sources.setdefault(location, []).append(
timestamp_source
)
logger.debug(
f"Timestamp source ({len(pkgs)} packages): {timestamp_source}"
)
fd.write(f"\n{timestamp_source}")
fd.flush()
# provides sources.list explicitly, otherwise `update()`
# doesn't reload it until the next `open()`
self.tempaptcache.update(sources_list=temp_sources_list)
self.tempaptcache.open()
for notfound_pkg in notfound_packages.copy():
pkg = self.tempaptcache.get(
f"{notfound_pkg.name}:{notfound_pkg.architecture}"
)
if (
pkg is not None
and pkg.versions.get(notfound_pkg.version) is not None
):
notfound_packages.remove(notfound_pkg)
self.tempaptcache.close()
if notfound_packages:
for notfound_pkg in notfound_packages:
logger.debug(
f"{notfound_pkg.name}-{notfound_pkg.version}.{notfound_pkg.architecture}"
)
raise RebuilderException(
"Cannot locate the following packages via "
"snapshots or the current repo/mirror"
)
def prepare_aptcache(self):
self.tempaptdir = tempfile.mkdtemp(prefix="debrebuild-", dir=self.tmpdir)
# Create apt.conf
temp_apt_conf = f"{self.tempaptdir}/etc/apt/apt.conf"
# Create sources.list
temp_sources_list = f"{self.tempaptdir}/etc/apt/sources.list"
apt_dirs = ["/etc/apt", "/etc/apt/trusted.gpg.d"]
for directory in apt_dirs:
os.makedirs(f"{self.tempaptdir}/{directory}")
with open(temp_apt_conf, "w") as fd:
apt_conf = """
Apt {{
Architecture "{build_arch}";
Architectures "{build_arch}";
}};
Acquire::Check-Valid-Until "false";
Acquire::Languages "none";
Acquire::http::Dl-Limit "1000";
Acquire::https::Dl-Limit "1000";
Acquire::Retries "5";
Binary::apt-get::Acquire::AllowInsecureRepositories "false";
""".format(
build_arch=self.buildinfo.build_arch, tempdir=self.tempaptdir
)
if self.proxy:
apt_conf += f'\nAcquire::http::proxy "{self.proxy}";\n'
fd.write(apt_conf)
with open(temp_sources_list, "w") as fd:
fd.write("\n".join(self.get_sources_list()))
keyrings = DEBIAN_KEYRINGS
if self.extra_repository_keys:
keyrings += self.extra_repository_keys
for keyring_src in keyrings:
keyring_dst = f"{self.tempaptdir}/etc/apt/trusted.gpg.d/{os.path.basename(keyring_src)}"
os.symlink(keyring_src, keyring_dst)
# Init temporary APT cache
try:
logger.debug("Initialize APT cache")
self.tempaptcache = apt.Cache(rootdir=self.tempaptdir, memonly=True)
self.tempaptcache.close()
except (PermissionError, apt_pkg.Error):
raise RebuilderException("Failed to initialize APT cache")
def get_apt_build_depends(self):
apt_build_depends = []
for pkg in self.buildinfo.get_build_depends():
apt_build_depends.append(
pkg.to_apt_install_format(self.buildinfo.build_arch)
)
return apt_build_depends
def get_chroot_basemirror(self):
# Workaround for standard method. libc6 should be the parent of all the packages.
for pkg in ["libc6", "dpkg", "build-essential", "util-linux"]:
dependency = self.get_build_dependency(pkg)
if dependency:
break
if not self.use_metasnap and dependency:
archive_name = dependency.archive_name
suite_name = dependency.suite_name
component_name = dependency.component_name
sorted_timestamp_sources = [dependency.timestamp]
else:
reference_key = f"debian+{self.buildinfo.get_debian_suite()}+main"
if self.buildinfo.required_timestamps.get(reference_key, None):
timestamps = self.buildinfo.required_timestamps[reference_key]
else:
reference_key, timestamps = list(
self.buildinfo.required_timestamps.items()
)[0]
sorted_timestamp_sources = sorted(timestamps)
archive_name, suite_name, component_name = reference_key.split("+", 3)
for timestamp in sorted_timestamp_sources:
url = f"{self.base_mirror}/{archive_name}/{timestamp}"
basemirror = f"deb {url} {suite_name} {component_name}"
release_url = f"{url}/dists/{suite_name}/Release"
resp = self.get_response(release_url)
if resp.ok:
return basemirror
raise RebuilderException("Cannot determine base mirror to use")
def get_build_dependency(self, name):
build_dependency = None
for pkg in self.buildinfo.get_build_depends():
if pkg.name == name:
build_dependency = pkg
break
return build_dependency
def mmdebstrap(self, output):
if self.buildinfo.build_archany and self.buildinfo.build_archall:
build = "any,all"
elif self.buildinfo.build_archall:
build = "all"
elif self.buildinfo.build_archany:
build = "any"
elif self.buildinfo.build_source:
build = "source"
else:
raise RebuilderException("Cannot determine what to build")
# Prepare mmdebstrap command
cmd = [
"env",
"-i",
"PATH=/usr/sbin:/usr/bin:/sbin:/bin",
"TMPDIR={}".format(self.tmpdir),
"mmdebstrap",
"--arch={}".format(self.buildinfo.build_arch),
"--include={}".format(" ".join(self.get_apt_build_depends())),
"--variant=apt",
'--aptopt=Acquire::Check-Valid-Until "false"',
'--aptopt=Acquire::http::Dl-Limit "1000";',
'--aptopt=Acquire::https::Dl-Limit "1000";',
'--aptopt=Acquire::Retries "5";',
'--aptopt=APT::Get::allow-downgrades "true";',
]
# Support for proxy
if self.proxy:
cmd += ['--aptopt=Acquire::http::proxy "{}";'.format(self.proxy)]
# Use all Debian keyrings at the mmdebstrap initial phase
cmd += ["--keyring=/usr/share/keyrings/"]
# Workaround for missing build-essential in buildinfo dependencies
if not self.get_build_dependency("build-essential"):
cmd += [
'--essential-hook=chroot "$1" sh -c "apt-get --yes install build-essential"'
]
# Add dependencies for running build as builduser
cmd += [
'--essential-hook=chroot "$1" sh -c "apt-get --yes install fakeroot util-linux"'
]
# Add Debian keyrings into mmdebstrap trusted keys after init phase
cmd += [
"--essential-hook=copy-in {} /etc/apt/trusted.gpg.d/".format(
join(DEBIAN_KEYRINGS)
)
]
# Copy extra keys and repository files
if self.extra_repository_keys:
cmd += [
"--essential-hook=copy-in {} /etc/apt/trusted.gpg.d/".format(
join(self.extra_repository_keys)
)
]
if self.extra_repository_files:
cmd += [
'--essential-hook=chroot "$1" sh -c "apt-get --yes install apt-transport-https ca-certificates"'
]
# Update APT cache with provided sources.list
cmd += [
'--essential-hook=chroot "$1" sh -c "{}"'.format(
" && ".join(
[
"rm /etc/apt/sources.list",
"echo '{}' >> /etc/apt/sources.list".format(
"\n".join(
self.get_sources_list()
+ self.get_sources_list_timestamps()
)
),
"apt-get update",
]
)
)
]
# Create builduser for running the build in mmdebstrap as builduser
cmd += [
'--customize-hook=chroot "$1" useradd --no-create-home -d /nonexistent -p "" builduser -s /bin/bash'
]
# In case of binNMU build, we add the changelog entry from buildinfo
binnmucmds = []
if self.buildinfo.logentry:
binnmucmds += [
"cd {}".format(quote(self.buildinfo.get_build_path())),
"{{ printf '%s' {}; cat debian/changelog; }} > debian/changelog.debrebuild".format(
quote(self.buildinfo.logentry)
),
"mv debian/changelog.debrebuild debian/changelog",
]
# Prepare build directory and get package source
cmd += [
'--customize-hook=chroot "$1" env sh -c "{}"'.format(
" && ".join(
[
"apt-get source --only-source -d {}={}".format(
self.buildinfo.source, self.buildinfo.source_version
),
"mkdir -p {}".format(
os.path.dirname(quote(self.buildinfo.get_build_path()))
),
"dpkg-source --no-check -x /*.dsc {}".format(
quote(self.buildinfo.get_build_path())
),
]
+ binnmucmds
+ [
"chown -R builduser:builduser {}".format(
os.path.dirname(quote(self.buildinfo.get_build_path()))
),
]
)
)
]
# Prepare build command
cmd += [
'--customize-hook=chroot "$1" env --unset=TMPDIR runuser builduser -c "{}"'.format(
" && ".join(
[
"cd {}".format(quote(self.buildinfo.get_build_path())),
"env {} dpkg-buildpackage -uc -a {} --build={}".format(
" ".join(self.get_env()), self.buildinfo.host_arch, build
),
]
)
)
]
cmd += [
"--customize-hook=sync-out {} {}".format(
os.path.dirname(quote(self.buildinfo.get_build_path())), output
),
self.buildinfo.get_debian_suite(),
"/dev/null",
self.get_chroot_basemirror(),
]
logger.debug(" ".join(cmd))
if subprocess.run(cmd).returncode != 0:
raise RebuilderException("mmdebstrap failed")
def verify_checksums(self, output, new_buildinfo):
status = True
summary = {}
for alg in self.buildinfo.checksums.keys():
checksums = self.buildinfo.checksums[alg]
new_checksums = new_buildinfo.checksums[alg]
files = [f for f in checksums if not f["name"].endswith(".dsc")]
new_files = [f for f in new_checksums if not f["name"].endswith(".dsc")]
summary.setdefault(alg, {})
if len(files) != len(new_files):
logger.debug(f"old buildinfo: {' '.join(files)}")
logger.debug(f"new buildinfo: {' '.join(new_files)}")
raise RebuilderException(
f"New buildinfo contains a different number of files in {alg} checksums."
)
for f in files:
new_file = None
for nf in new_files:
if nf["name"] == f["name"]:
new_file = nf
break
if not new_file:
raise RebuilderException(
f"{alg}: Cannot find {f['name']} in new files"
)
summary[alg].setdefault(f["name"], {})
cur_status = True
for prop in f.keys():
if prop not in new_file.keys():
raise RebuilderException(
f"{alg}: '{prop}' is not used in both buildinfo files"
)
if prop != "name":
summary[alg][f["name"]][prop] = {
"old": f[prop],
"new": new_file[prop],
}
if prop == "size":
if f["size"] != new_file["size"]:
logger.error(f"{alg}: Size differs for {f['name']}")
cur_status = False
continue
if f[prop] != new_file[prop]:
logger.error(
f"{alg}: Value of '{prop}' differs for {f['name']}"
)
cur_status = False
if cur_status:
logger.info(f"{alg}: {f['name']}: OK")
else:
status = False
if not status:
logger.error("Checksums: FAIL")
else:
logger.info("Checksums: OK")
return status, summary
def generate_intoto_metadata(self, output, new_buildinfo):
new_files = [
f["name"]
for f in new_buildinfo.checksums["sha256"]
if not f["name"].endswith(".dsc")
]
cmd = [
"in-toto-run",
"--step-name=rebuild",
"--no-command",
"--products",
] + list(new_files)
if self.gpg_sign_keyid:
cmd += ["--gpg", self.gpg_sign_keyid]
else:
cmd += ["--gpg"]
try:
result = subprocess.run(cmd, cwd=output)
returncode = result.returncode
except FileNotFoundError:
logger.error("in-toto-run not found!")
returncode = 1
if returncode != 0:
raise RebuilderInTotoError("in-toto metadata generation failed!")
logger.info("in-toto metadata generation passed")
@staticmethod
def run_diffoscope(output, file1, file2):