Coverage for self-hosting-plugins/debputy_self_hosting.py: 73%

13 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-07 12:14 +0200

1import textwrap 

2 

3from debputy.plugin.api import ( 

4 DebputyPluginInitializer, 

5 VirtualPath, 

6 BinaryCtrlAccessor, 

7 PackageProcessingContext, 

8) 

9from debputy.util import POSTINST_DEFAULT_CONDITION 

10 

11 

12def _maintscript_generator( 

13 _path: VirtualPath, 

14 ctrl: BinaryCtrlAccessor, 

15 context: PackageProcessingContext, 

16) -> None: 

17 maintscript = ctrl.maintscript 

18 

19 # When `debputy` becomes a stand-alone package, it should have these maintscripts instead of dh-debputy 

20 # Admittedly, I hope to get rid of this plugin before then, but ... 

21 assert context.binary_package.name != "debputy", "Update the self-hosting plugin" 

22 dirname = "/usr/share/debputy" 

23 

24 if context.binary_package.name == "dh-debputy": 24 ↛ 25line 24 didn't jump to line 25, because the condition on line 24 was never true

25 ctrl.dpkg_trigger("interest-noawait", dirname) 

26 maintscript.unconditionally_in_script( 

27 "postinst", 

28 textwrap.dedent( 

29 f"""\ 

30 if {POSTINST_DEFAULT_CONDITION} || [ "$1" = "triggered" ] ; then 

31 # Ensure all plugins are byte-compiled (plus uninstalled plugins are cleaned up) 

32 py3clean {dirname} 

33 if command -v py3compile >/dev/null 2>&1; then 

34 py3compile {dirname} 

35 fi 

36 if command -v pypy3compile >/dev/null 2>&1; then 

37 pypy3compile {dirname} || true 

38 fi 

39 fi 

40 """ 

41 ), 

42 ) 

43 maintscript.unconditionally_in_script( 

44 "prerm", 

45 textwrap.dedent( 

46 f"""\ 

47 if command -v py3clean >/dev/null 2>&1; then 

48 py3clean {dirname} 

49 else 

50 find {dirname}/ -type d -name __pycache__ -empty -print0 | xargs --null --no-run-if-empty rmdir 

51 fi 

52 """ 

53 ), 

54 ) 

55 

56 

57def initializer(api: DebputyPluginInitializer) -> None: 

58 api.metadata_or_maintscript_detector( 

59 "debputy-self-hosting", 

60 _maintscript_generator, 

61 )