summaryrefslogtreecommitdiff
path: root/mdbp/pbuilder.py
blob: a5f734a4957a4f4ff7e3bbcc14fe99042b8f5d57 (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
#!/usr/bin/python3
# SPDX-License-Identifier: MIT
"""mdbp backend using pbuilder"""

import argparse
import os
import pathlib
import subprocess
import sys

from .common import buildjson, compute_env, get_dsc, make_option, \
        profile_option

def main() -> None:
    """Entry point for mdbp-pbuilder backend"""
    parser = argparse.ArgumentParser()
    parser.add_argument("buildjson", type=buildjson)
    args = parser.parse_args()
    build = args.buildjson

    enablelog = build["output"].get("log", True)
    if build.get("lintian", {}).get("run"):
        raise ValueError("running lintian not supported")
    if "bd-uninstallable-explainer" in build:
        raise ValueError("bd-uninstallable-explainer %r not supported" %
                         build["bd-uinstallable-explainer"])
    if build.get("buildpath"):
        raise ValueError("buildpath not supported")
    if build["distribution"] in ("sid", "unstable"):
        basetgz = None
    else:
        for pat in ("/var/cache/pbuilder/%s-base.tgz",
                    "/var/cache/pbuidler/%s.tgz"):
            basetgz = pat % build["distribution"]
            if pathlib.Path(basetgz).is_file():
                break
        else:
            raise ValueError("unsupported distribution %s" %
                             build["distribution"])

    cmd = []
    if os.getuid() != 0:
        cmd.extend(["sudo", "-E", "--"])
    cmd.extend(["/usr/sbin/pbuilder", "build", "--no-source-only-changes"])
    cmd.extend(make_option("--basetgz", basetgz))
    cmd.extend(make_option("--architecture", build.get("buildarch")))
    cmd.extend(make_option("--host-arch", build.get("hostarch")))
    cmd.extend(make_option("--othermirror",
                           "|".join(build.get("extrarepositories", ()))))
    cmd.extend(make_option("--use-network",
        {"enable": "yes", "try-enable": "yes", "disable": "no",
         "try-disable": "no"}.get(build.get("network"))))
    cmd.extend(dict(any=["--binary-arch"],
                    all=["--binary-indep"],
                    binary=["--debbuildopts", "-b"])[
                        build.get("type", "binary")])
    cmd.extend(profile_option(build, "--profiles"))
    cmd.extend(["--buildresult", build["output"]["directory"]])
    if not enablelog:
        cmd.extend(["--loglevel", "E"])
    with get_dsc(build) as dscpath:
        cmd.append(str(dscpath))
        proc = subprocess.Popen(cmd, env=compute_env(build),
                                stdout=None if enablelog
                                       else subprocess.DEVNULL,
                                stderr=subprocess.STDOUT if enablelog
                                       else subprocess.DEVNULL)
        sys.exit(proc.wait())

if __name__ == "__main__":
    main()