summaryrefslogtreecommitdiffstats
path: root/src/debputy/packaging/alternatives.py
blob: 249fa9e92b25eaae7bcf9acffb9948de3414a687 (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
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
import textwrap
from typing import List, Dict, Tuple, Mapping

from debian.deb822 import Deb822

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 import VirtualPath
from debputy.util import _error, escape_shell, POSTINST_DEFAULT_CONDITION

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


def process_alternatives(
    binary_package: BinaryPackage,
    fs_root: VirtualPath,
    reserved_packager_provided_files: Dict[str, List[PackagerProvidedFile]],
    maintscript_snippets: Dict[str, MaintscriptSnippetContainer],
) -> None:
    if binary_package.is_udeb:
        return

    provided_alternatives_file = resolve_reserved_provided_file(
        "alternatives",
        reserved_packager_provided_files,
    )
    if provided_alternatives_file is None:
        return

    with provided_alternatives_file.open() as fd:
        alternatives = list(Deb822.iter_paragraphs(fd))

    for no, alternative in enumerate(alternatives):
        process_alternative(
            provided_alternatives_file.fs_path,
            fs_root,
            alternative,
            no,
            maintscript_snippets,
        )


def process_alternative(
    provided_alternatives_fs_path: str,
    fs_root: VirtualPath,
    alternative_deb822: Deb822,
    no: int,
    maintscript_snippets: Dict[str, MaintscriptSnippetContainer],
) -> None:
    name = _mandatory_key(
        "Name",
        alternative_deb822,
        provided_alternatives_fs_path,
        f"Stanza number {no}",
    )
    error_context = f"Alternative named {name}"
    link_path = _mandatory_key(
        "Link",
        alternative_deb822,
        provided_alternatives_fs_path,
        error_context,
    )
    impl_path = _mandatory_key(
        "Alternative",
        alternative_deb822,
        provided_alternatives_fs_path,
        error_context,
    )
    priority = _mandatory_key(
        "Priority",
        alternative_deb822,
        provided_alternatives_fs_path,
        error_context,
    )
    if "/" in name:
        _error(
            f'The "Name" ({link_path}) key must be a basename and cannot contain slashes'
            f" ({error_context} in {provided_alternatives_fs_path})"
        )
    if link_path == impl_path:
        _error(
            f'The "Link" key and the "Alternative" key must not have the same value'
            f" ({error_context} in {provided_alternatives_fs_path})"
        )
    impl = fs_root.lookup(impl_path)
    if impl is None or impl.is_dir:
        _error(
            f'The path listed in "Alternative" ("{impl_path}") does not exist'
            f" in the package. ({error_context} in {provided_alternatives_fs_path})"
        )
    for key in ["Slave", "Slaves", "Slave-Links"]:
        if key in alternative_deb822:
            _error(
                f'Please use "Dependents" instead of "{key}".'
                f" ({error_context} in {provided_alternatives_fs_path})"
            )
    dependents = alternative_deb822.get("Dependents")
    install_command = [
        escape_shell(
            "update-alternatives",
            "--install",
            link_path,
            name,
            impl_path,
            priority,
        )
    ]
    remove_command = [
        escape_shell(
            "update-alternatives",
            "--remove",
            link_path,
            impl_path,
        )
    ]
    if dependents:
        seen_link_path = set()
        for line in dependents.splitlines():
            line = line.strip()
            if not line:  # First line is usually empty
                continue
            dlink_path, dlink_name, dimpl_path = parse_dependent_link(
                line,
                error_context,
                provided_alternatives_fs_path,
            )
            if dlink_path in seen_link_path:
                _error(
                    f'The Dependent link path "{dlink_path}" was used twice.'
                    f" ({error_context} in {provided_alternatives_fs_path})"
                )
            dimpl = fs_root.lookup(dimpl_path)
            if dimpl is None or dimpl.is_dir:
                _error(
                    f'The path listed in "Dependents" ("{dimpl_path}") does not exist'
                    f" in the package. ({error_context} in {provided_alternatives_fs_path})"
                )
            seen_link_path.add(dlink_path)
            install_command.append(LINE_PREFIX)
            install_command.append(
                escape_shell(
                    # update-alternatives still uses this old option name :-/
                    "--slave",
                    dlink_path,
                    dlink_name,
                    dimpl_path,
                )
            )
    postinst = textwrap.dedent(
        """\
    if {CONDITION}; then
        {COMMAND}
    fi
    """
    ).format(
        CONDITION=POSTINST_DEFAULT_CONDITION,
        COMMAND=" ".join(install_command),
    )

    prerm = textwrap.dedent(
        """\
    if [ "$1" = "remove" ]; then
        {COMMAND}
    fi
    """
    ).format(COMMAND=" ".join(remove_command))
    maintscript_snippets["postinst"].append(
        MaintscriptSnippet(
            f"debputy (via {provided_alternatives_fs_path})",
            snippet=postinst,
        )
    )
    maintscript_snippets["prerm"].append(
        MaintscriptSnippet(
            f"debputy (via {provided_alternatives_fs_path})",
            snippet=prerm,
        )
    )


def parse_dependent_link(
    line: str,
    error_context: str,
    provided_alternatives_file: str,
) -> Tuple[str, str, str]:
    parts = line.split()
    if len(parts) != 3:
        if len(parts) > 1:
            pass
        _error(
            f"The each line in Dependents links must have exactly 3 space separated parts."
            f' The "{line}" split into {len(parts)} part(s).'
            f" ({error_context} in {provided_alternatives_file})"
        )

    dlink_path, dlink_name, dimpl_path = parts
    if "/" in dlink_name:
        _error(
            f'The Dependent link name "{dlink_path}" must be a basename and cannot contain slashes'
            f" ({error_context} in {provided_alternatives_file})"
        )
    if dlink_path == dimpl_path:
        _error(
            f'The Dependent Link path and Alternative must not have the same value ["{dlink_path}"]'
            f" ({error_context} in {provided_alternatives_file})"
        )
    return dlink_path, dlink_name, dimpl_path


def _mandatory_key(
    key: str,
    alternative_deb822: Mapping[str, str],
    provided_alternatives_file: str,
    error_context: str,
) -> str:
    try:
        return alternative_deb822[key]
    except KeyError:
        _error(
            f'Missing mandatory key "{key}" in {provided_alternatives_file} ({error_context})'
        )