summaryrefslogtreecommitdiffstats
path: root/self-hosting-plugins/debputy_self_hosting.py
blob: 4db4e05ff464c9381bff8fc500f8dfe04eb11086 (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
import textwrap

from debputy.plugin.api import (
    DebputyPluginInitializer,
    VirtualPath,
    BinaryCtrlAccessor,
    PackageProcessingContext,
)
from debputy.util import POSTINST_DEFAULT_CONDITION


def _maintscript_generator(
    _path: VirtualPath,
    ctrl: BinaryCtrlAccessor,
    context: PackageProcessingContext,
) -> None:
    maintscript = ctrl.maintscript

    # When `debputy` becomes a stand-alone package, it should have these maintscripts instead of dh-debputy
    # Admittedly, I hope to get rid of this plugin before then, but ...
    assert context.binary_package.name != "debputy", "Update the self-hosting plugin"
    dirname = "/usr/share/debputy"

    if context.binary_package.name == "dh-debputy":
        ctrl.dpkg_trigger("interest-noawait", dirname)
        maintscript.unconditionally_in_script(
            "postinst",
            textwrap.dedent(
                f"""\
            if {POSTINST_DEFAULT_CONDITION} || [ "$1" = "triggered" ] ; then
                # Ensure all plugins are byte-compiled (plus uninstalled plugins are cleaned up)
                py3clean {dirname}
                if command -v py3compile >/dev/null 2>&1; then
                    py3compile {dirname}
                fi
                if command -v pypy3compile >/dev/null 2>&1; then
                    pypy3compile {dirname} || true
                fi
            fi
        """
            ),
        )
        maintscript.unconditionally_in_script(
            "prerm",
            textwrap.dedent(
                f"""\
        if command -v py3clean >/dev/null 2>&1; then
            py3clean {dirname}
        else
            find {dirname}/ -type d -name __pycache__ -empty -print0 | xargs --null --no-run-if-empty rmdir
        fi
        """
            ),
        )


def initializer(api: DebputyPluginInitializer) -> None:
    api.metadata_or_maintscript_detector(
        "debputy-self-hosting",
        _maintscript_generator,
    )