Coverage for self-hosting-plugins/debputy_self_hosting.py: 54%
24 statements
« prev ^ index » next coverage.py v7.6.0, created at 2025-03-24 16:38 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2025-03-24 16:38 +0000
1import textwrap
2from typing import cast, Any, TYPE_CHECKING
4from debputy.plugin.api import (
5 DebputyPluginInitializer,
6 VirtualPath,
7 BinaryCtrlAccessor,
8 PackageProcessingContext,
9)
11from debputy.util import POSTINST_DEFAULT_CONDITION
13if TYPE_CHECKING:
14 from debputy.filesystem_scan import VirtualPathBase
15 from debputy.plugin.api.impl import DebputyPluginInitializerProvider
17PRIVATE_PYTHON_DIR = "/usr/share/debputy"
20def _maintscript_generator(
21 _path: VirtualPath,
22 ctrl: BinaryCtrlAccessor,
23 context: PackageProcessingContext,
24) -> None:
25 maintscript = ctrl.maintscript
26 package_name = context.binary_package.name
28 # When `debputy` becomes a stand-alone package, it should have these maintscripts instead of dh-debputy
29 # Admittedly, I hope to get rid of this plugin before then, but ...
30 assert package_name == "dh-debputy", "Update the self-hosting plugin"
32 ctrl.dpkg_trigger("interest-noawait", PRIVATE_PYTHON_DIR)
33 maintscript.unconditionally_in_script(
34 "postinst",
35 textwrap.dedent(
36 f"""\
37 \
38 if {POSTINST_DEFAULT_CONDITION} || [ "$1" = "triggered" ] ; then
39 # Ensure all plugins are byte-compiled (plus uninstalled plugins are cleaned up)
40 py3clean {PRIVATE_PYTHON_DIR}
41 if command -v py3compile >/dev/null 2>&1; then
42 py3compile {PRIVATE_PYTHON_DIR}
43 fi
44 if command -v pypy3compile >/dev/null 2>&1; then
45 pypy3compile {PRIVATE_PYTHON_DIR} || true
46 fi
47 fi
48 """
49 ),
50 )
51 maintscript.unconditionally_in_script(
52 "prerm",
53 textwrap.dedent(
54 f"""\
55 \
56 if command -v py3clean >/dev/null 2>&1; then
57 py3clean {PRIVATE_PYTHON_DIR}
58 else
59 find {PRIVATE_PYTHON_DIR}/ -type d -name __pycache__ -empty -print0 | xargs --null --no-run-if-empty rmdir
60 fi
61 """
62 ),
63 )
66def _rtupdate_generator(
67 fs_root: VirtualPath,
68 _: Any,
69 context: "PackageProcessingContext",
70) -> None:
71 package_name = context.binary_package.name
72 # When `debputy` becomes a stand-alone package, it should have these scripts instead of dh-debputy
73 # Admittedly, I hope to get rid of this plugin before then, but ...
74 assert package_name == "dh-debputy", "Update the self-hosting plugin"
75 rtupdate_dir: VirtualPathBase = cast(
76 "VirtualPathBase",
77 fs_root.mkdirs("/usr/share/python3/runtime.d/"),
78 )
79 with rtupdate_dir.open_child("debputy.rtupdate", "w") as fd:
80 template = textwrap.dedent(
81 f"""\
82 \
83 #! /bin/sh
84 if [ "$1" = rtupdate ]; then
85 py3clean {PRIVATE_PYTHON_DIR}
86 py3compile {PRIVATE_PYTHON_DIR}
87 fi
88 """
89 )
90 fd.write(template)
91 rtupdate_dir["debputy.rtupdate"].chmod(0o755)
94def initializer(api: DebputyPluginInitializer) -> None:
95 api.metadata_or_maintscript_detector(
96 "debputy-self-hosting",
97 _maintscript_generator,
98 )
99 internal_api: DebputyPluginInitializerProvider = cast(
100 "DebputyPluginInitializerProvider", api
101 )
102 internal_api.package_processor(
103 "rtupdate",
104 _rtupdate_generator,
105 )