summaryrefslogtreecommitdiffstats
path: root/debian/compute_pkgset.py
blob: 3404cb0bf90262bd101618db1521ebdd66b6e728 (plain)
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
#!/usr/bin/env python3

import tempfile
import pathlib
import subprocess
import debian.deb822
from collections import defaultdict


def main():
    pkglist = defaultdict(lambda: defaultdict(set))
    apttrusted = subprocess.check_output(
        "eval $(apt-config shell v Dir::Etc::Trusted/f); printf $v", shell=True
    ).decode()
    apttrustedparts = subprocess.check_output(
        "eval $(apt-config shell v Dir::Etc::TrustedParts/f); printf $v", shell=True
    ).decode()
    debci_arches = set(
        ["amd64", "arm64", "armel", "armhf", "i386", "ppc64el", "riscv64", "s390x"]
    )
    # debci_arches = set(["amd64", "i386"])
    for arch in list(debci_arches):
        with tempfile.TemporaryDirectory() as tmpdir:
            tmpdir = pathlib.Path(tmpdir)
            (tmpdir / "etc" / "apt").mkdir(parents=True)
            (tmpdir / "var" / "cache").mkdir(parents=True)
            (tmpdir / "var" / "lib").mkdir(parents=True)
            (tmpdir / "apt.conf").write_text(
                f"""
                Apt::Architecture "{arch}";
                Apt::Architectures "{arch}";
                Dir "{tmpdir}";
                Dir::Etc::Trusted "{apttrusted}";
                Dir::Etc::TrustedParts "{apttrustedparts}";
            """
            )
            (tmpdir / "etc" / "apt" / "sources.list").write_text(
                "deb http://deb.debian.org/debian/ unstable main"
            )
            subprocess.check_call(
                ["apt-get", "update"], env={"APT_CONFIG": tmpdir / "apt.conf"}
            )
            indextargets = subprocess.check_output(
                [
                    "apt-get",
                    "indextargets",
                    "--format",
                    "$(FILENAME)",
                    "Created-By: Packages",
                    f"Architecture: {arch}",
                ],
                env={"APT_CONFIG": tmpdir / "apt.conf"},
            ).decode()
            if not indextargets.strip():
                print(f"skipping {arch}")
                debci_arches.remove(arch)
                continue
            (tmpdir / "Packages").write_bytes(
                subprocess.check_output(
                    ["/usr/lib/apt/apt-helper", "cat-file", *indextargets.splitlines()]
                )
            )
            pkgset = (
                subprocess.check_output(
                    [
                        "grep-dctrl",
                        "--no-field-names",
                        "--show-field=Package",
                        "--exact-match",
                        "(",
                        "--field=Essential",
                        "yes",
                        "--or",
                        "--field=Priority",
                        "required",
                        "--or",
                        "--field=Priority",
                        "important",
                        "--or",
                        "--field=Priority",
                        "standard",
                        ")",
                        (tmpdir / "Packages"),
                    ]
                )
                .decode()
                .splitlines()
            )
            pkgset.extend(
                [
                    "build-essential",
                    "busybox",
                    "gpg",
                    "eatmydata",
                    "usr-is-merged",
                    "usrmerge",
                ]
            )
            ceve = subprocess.check_output(
                [
                    "dose-ceve",
                    f"--deb-native-arch={arch}",
                    "-c",
                    ",".join(pkgset),
                    "-t",
                    "deb",
                    "-G",
                    "pkg",
                    "-T",
                    "deb",
                    (tmpdir / "Packages"),
                ]
            )
        for pkg in debian.deb822.Packages.iter_paragraphs(ceve):
            src = pkg.get("Source")
            if src is None:
                src = pkg["Package"]
            elif " " in src:
                src = src.split()[0]
            pkglist[src][pkg["Package"]].add(arch)
    result = []
    for src in pkglist:
        srcarches = set()
        for pkg in pkglist[src]:
            srcarches |= pkglist[src][pkg]
        representers = [pkg for pkg in pkglist[src] if pkglist[src][pkg] == srcarches]
        if not representers:
            print(f"nothing represents {src}:", pkglist[src])
            continue
        pkg = sorted(representers)[0]
        if pkglist[src][pkg] == debci_arches:
            result.append(pkg)
        else:
            result.append(f"{pkg} [{' '.join(sorted(pkglist[src][pkg]))}]")
    line = "Depends: "
    for dep in sorted(result):
        newline = line + f"{dep}, "
        if len(newline) > 79:
            print(line.removesuffix(" "))
            line = f" {dep}, "
        else:
            line = newline
    print(line.removesuffix(", "))


if __name__ == "__main__":
    main()