summaryrefslogtreecommitdiffstats
path: root/src/debputy/packaging/debconf_templates.py
blob: b8277635cb691df967b0800f5dd21f7137020f99 (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
import os.path
import shutil
import subprocess
import textwrap
from typing import List, Dict

from debputy.maintscript_snippet import MaintscriptSnippetContainer, MaintscriptSnippet
from debputy.packager_provided_files import PackagerProvidedFile
from debputy.packages import BinaryPackage
from debputy.packaging.makeshlibs import resolve_reserved_provided_file
from debputy.plugin.api.spec import FlushableSubstvars
from debputy.util import _error, escape_shell

# Match debhelper (minus one space in each end, which comes
# via join).
LINE_PREFIX = "\\\n            "


def process_debconf_templates(
    binary_package: BinaryPackage,
    reserved_packager_provided_files: Dict[str, List[PackagerProvidedFile]],
    maintscript_snippets: Dict[str, MaintscriptSnippetContainer],
    substvars: FlushableSubstvars,
    control_output_dir: str,
) -> None:
    provided_templates_file = resolve_reserved_provided_file(
        "templates",
        reserved_packager_provided_files,
    )
    if provided_templates_file is None:
        return

    templates_file = os.path.join(control_output_dir, "templates")
    debian_dir = provided_templates_file.parent_dir
    po_template_dir = debian_dir.get("po") if debian_dir is not None else None
    if po_template_dir is not None and po_template_dir.is_dir:
        with open(templates_file, "wb") as fd:
            cmd = [
                "po2debconf",
                provided_templates_file.fs_path,
            ]
            print(f"   {escape_shell(*cmd)} > {templates_file}")
            try:
                subprocess.check_call(
                    cmd,
                    stdout=fd.fileno(),
                )
            except subprocess.CalledProcessError:
                _error(
                    f"Failed to generate the templates files for {binary_package.name}. Please review "
                    f" the output of {escape_shell('po-debconf', provided_templates_file.fs_path)}"
                    " to understand the issue."
                )
    else:
        shutil.copyfile(provided_templates_file.fs_path, templates_file)

    dependency = (
        "cdebconf-udeb" if binary_package.is_udeb else "debconf (>= 0.5) | debconf-2.0"
    )
    substvars.add_dependency("misc:Depends", dependency)
    if not binary_package.is_udeb:
        # udebs do not have `postrm` scripts
        maintscript_snippets["postrm"].append(
            MaintscriptSnippet(
                f"debputy (due to {provided_templates_file.fs_path})",
                # FIXME: `debconf` sourcing should be an overarching feature
                snippet=textwrap.dedent(
                    """\
                    if [ "$1" = purge ] && [ -e /usr/share/debconf/confmodule ]; then
                        . /usr/share/debconf/confmodule
                        db_purge
                        db_stop
                    fi
                """
                ),
            )
        )