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

import argparse
import contextlib
import os
import pathlib
import shlex
import subprocess
import sys
import tempfile
import typing

from .common import AddSpaceSeparatedValues, buildjson, clean_dir, \
        compute_env, get_dsc, make_option, profile_option, \
        temporary_static_file

def find_basetgz(distribution: str,
                 basedir: str = "/var/cache/pbuiler") -> typing.Optional[str]:
    """Locate a pbuilder base.tgz for the given distribution."""
    if distribution in ("sid", "unstable"):
        return None
    for pat in ("/%s-base.tgz", "/%s.tgz"):
        basetgz = basedir + pat % distribution
        if pathlib.Path(basetgz).is_file():
            return basetgz
    raise ValueError("unsupported distribution %s" % distribution)


@contextlib.contextmanager
def hookdir() -> typing.Iterator[
    typing.Tuple[pathlib.Path, typing.Callable[[str, str], None]]
]:
    """A context manager that returns a pair of a hook directory suitable for
    pbuilder and a function for adding hooks to the directory.
    """
    with tempfile.TemporaryDirectory() as tdir:
        directory = pathlib.Path(tdir)
        counter = 0

        def addhook(key: str, content: str, prio: str = "50") -> None:
            nonlocal counter
            hookpath = directory / ("%s%s%05X" % (key, prio, counter))
            hookpath.write_text(content)
            hookpath.chmod(0o755)

        yield (directory, addhook)


def main() -> None:
    """Entry point for mdbp-pbuilder backend"""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--pbuilderopt",
        dest="pbuilderopts",
        action="append",
        metavar="OPT",
        default=[],
        help="a custom option to be passed down to pbuilder, can be specified "
             "multiple times and mixed with --pbuilderopts",
    )
    parser.add_argument(
        "--pbuilderopts",
        action=AddSpaceSeparatedValues,
        metavar="OPTS",
        default=[],
        help="space-separated options to be passed down to pbuilder, can be "
             "specified multiple times and mixed with --pbuilderopt",
    )
    parser.add_argument("buildjson", type=buildjson)
    args = parser.parse_args()
    build = args.buildjson

    if "sourcename" in build["input"]:
        raise ValueError("building a source package by name is not supported")
    enablelog = build["output"].get("log", True)
    if "bd-uninstallable-explainer" in build:
        raise ValueError("bd-uninstallable-explainer %r not supported" %
                         build["bd-uinstallable-explainer"])

    cmd = [
        *([] if os.getuid() == 0 else ["sudo", "-E", "--"]),
        "/usr/sbin/pbuilder",
        "build",
        "--no-source-only-changes",
        *make_option("--basetgz", find_basetgz(build["distribution"])),
        *make_option("--architecture", build.get("build_architecture")),
        *make_option("--host-arch", build.get("host_architecture")),
        *make_option("--othermirror",
                     "|".join(build.get("extrarepositories", ()))),
        *make_option("--use-network",
                     {"enable": "yes", "try-enable": "yes", "disable": "no",
                      "try-disable": "no"}.get(build.get("network"))),
        *dict(any=["--binary-arch"],
              all=["--binary-indep"],
              binary=["--debbuildopts", "-b"])[
                  build.get("type", "binary")],
        *profile_option(build, "--profiles"),
        "--buildresult", build["output"]["directory"],
        *([] if enablelog else ["--loglevel", "E"]),
    ]
    apt_get = ["apt-get", "-oAPT::Keep-Downloaded-Path=false", "--yes"]
    with contextlib.ExitStack() as stack:
        if build.get("build_path"):
            pbuilderrc = "BUILDDIR=%s\n" % shlex.quote(build["build_path"])
            cmd.extend([
                "--configfile",
                str(stack.enter_context(temporary_static_file(pbuilderrc))),
            ])
        hookdirname, addhook = stack.enter_context(hookdir())
        addhook(
            "D",
            """#/bin/sh
set -e
apt-get --error-on=any update
%s dist-upgrade
"""
            % shlex.join(apt_get),
        )
        if build.get("lintian", {}).get("run", False):
            addhook(
                "B",
                """#!/bin/sh
set -e
%s install lintian
runuser -u pbuilder -- lintian %s "${BUILDDIR:-/tmp/buildd}"/*.changes
"""
                % (
                    shlex.join(apt_get),
                    shlex.join(build["lintian"].get("options", [])),
                ),
            )
        cmd.extend(["--hookdir", str(hookdirname), *args.pbuilderopts,
                    str(stack.enter_context(get_dsc(build)))])
        ret = subprocess.call(cmd, env=compute_env(build),
                              stdout=None if enablelog
                                     else subprocess.DEVNULL,
                              stderr=subprocess.STDOUT if enablelog
                                     else subprocess.DEVNULL)
        clean_dir(pathlib.Path(build["output"]["directory"]),
                  build["output"].get("artifacts", ["*"]))
        sys.exit(ret)

if __name__ == "__main__":
    main()