Coverage for debputy/plugins/grantlee.py: 96%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2025-01-27 13:59 +0000

1import functools 

2import os 

3import re 

4import subprocess 

5from typing import Any, Optional 

6 

7from debputy.plugin.api import ( 

8 DebputyPluginInitializer, 

9 BinaryCtrlAccessor, 

10 PackageProcessingContext, 

11 VirtualPath, 

12) 

13from debputy.util import _error 

14 

15 

16_RE_GRANTLEE_VERSION = re.compile(r"^\d\.\d$") 

17 

18 

19def initialize(api: DebputyPluginInitializer) -> None: 

20 api.metadata_or_maintscript_detector( 

21 "detect-grantlee-dependencies", 

22 detect_grantlee_dependencies, 

23 ) 

24 

25 

26def detect_grantlee_dependencies( 

27 fs_root: VirtualPath, 

28 ctrl: BinaryCtrlAccessor, 

29 context: PackageProcessingContext, 

30) -> None: 

31 binary_package = context.binary_package 

32 if binary_package.is_arch_all: 

33 # Delta from dh_grantlee, but the MULTIARCH paths should not 

34 # exist in arch:all packages 

35 return 

36 ma = binary_package.package_deb_architecture_variable("MULTIARCH") 

37 grantlee_root_dirs = [ 

38 f"usr/lib/{ma}/grantlee", 

39 f"usr/lib/{ma}/qt5/plugins/grantlee", 

40 ] 

41 grantee_version: Optional[str] = None 

42 for grantlee_root_dir in grantlee_root_dirs: 

43 grantlee_root_path = fs_root.lookup(grantlee_root_dir) 

44 if grantlee_root_path is None or not grantlee_root_path.is_dir: 

45 continue 

46 # Delta: The original code recurses and then checks for "grantee/VERSION". 

47 # Code here assumes that was just File::Find being used as a dir iterator. 

48 for child in grantlee_root_path.iterdir: 

49 if not _RE_GRANTLEE_VERSION.fullmatch(child.name): 

50 continue 

51 version = child.name 

52 if grantee_version is not None and version != grantee_version: 52 ↛ 53line 52 didn't jump to line 53 because the condition on line 52 was never true

53 _error( 

54 f"Package {binary_package.name} contains plugins for different grantlee versions" 

55 ) 

56 grantee_version = version 

57 

58 if grantee_version is None: 

59 return 

60 dep_version = grantee_version.replace(".", "-") 

61 grantee_dep = f"grantlee5-templates-{dep_version}" 

62 ctrl.substvars["grantlee:Depends"] = grantee_dep