Coverage for src/debputy/packaging/debconf_templates.py: 32%

32 statements  

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

1import os.path 

2import shutil 

3import subprocess 

4import textwrap 

5from typing import List, Dict 

6 

7from debputy.maintscript_snippet import MaintscriptSnippetContainer, MaintscriptSnippet 

8from debputy.packager_provided_files import PackagerProvidedFile 

9from debputy.packages import BinaryPackage 

10from debputy.packaging.makeshlibs import resolve_reserved_provided_file 

11from debputy.plugin.api.spec import FlushableSubstvars 

12from debputy.util import _error, escape_shell 

13 

14# Match debhelper (minus one space in each end, which comes 

15# via join). 

16LINE_PREFIX = "\\\n " 

17 

18 

19def process_debconf_templates( 

20 binary_package: BinaryPackage, 

21 reserved_packager_provided_files: Dict[str, List[PackagerProvidedFile]], 

22 maintscript_snippets: Dict[str, MaintscriptSnippetContainer], 

23 substvars: FlushableSubstvars, 

24 control_output_dir: str, 

25) -> None: 

26 provided_templates_file = resolve_reserved_provided_file( 

27 "templates", 

28 reserved_packager_provided_files, 

29 ) 

30 if provided_templates_file is None: 

31 return 

32 

33 templates_file = os.path.join(control_output_dir, "templates") 

34 debian_dir = provided_templates_file.parent_dir 

35 po_template_dir = debian_dir.get("po") if debian_dir is not None else None 

36 if po_template_dir is not None and po_template_dir.is_dir: 

37 with open(templates_file, "wb") as fd: 

38 cmd = [ 

39 "po2debconf", 

40 provided_templates_file.fs_path, 

41 ] 

42 print(f" {escape_shell(*cmd)} > {templates_file}") 

43 try: 

44 subprocess.check_call( 

45 cmd, 

46 stdout=fd.fileno(), 

47 ) 

48 except subprocess.CalledProcessError: 

49 _error( 

50 f"Failed to generate the templates files for {binary_package.name}. Please review " 

51 f" the output of {escape_shell('po-debconf', provided_templates_file.fs_path)}" 

52 " to understand the issue." 

53 ) 

54 else: 

55 shutil.copyfile(provided_templates_file.fs_path, templates_file) 

56 

57 dependency = ( 

58 "cdebconf-udeb" if binary_package.is_udeb else "debconf (>= 0.5) | debconf-2.0" 

59 ) 

60 substvars.add_dependency("misc:Depends", dependency) 

61 if not binary_package.is_udeb: 

62 # udebs do not have `postrm` scripts 

63 maintscript_snippets["postrm"].append( 

64 MaintscriptSnippet( 

65 f"debputy (due to {provided_templates_file.fs_path})", 

66 # FIXME: `debconf` sourcing should be an overarching feature 

67 snippet=textwrap.dedent( 

68 """\ 

69 if [ "$1" = purge ] && [ -e /usr/share/debconf/confmodule ]; then 

70 . /usr/share/debconf/confmodule 

71 db_purge 

72 db_stop 

73 fi 

74 """ 

75 ), 

76 ) 

77 )