Coverage for debputy/plugins/numpy3.py: 86%

34 statements  

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

1import functools 

2import os 

3from typing import Any, Tuple 

4 

5from debputy.plugin.api import ( 

6 DebputyPluginInitializer, 

7 BinaryCtrlAccessor, 

8 PackageProcessingContext, 

9) 

10from debputy.util import _error 

11 

12 

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

14 api.metadata_or_maintscript_detector( 

15 "numpy-depends", 

16 numpy3_versions, 

17 # Probably not necessary, but this is the most faithful conversion 

18 package_type=["deb", "udeb"], 

19 ) 

20 

21 

22@functools.lru_cache 

23def _parse_numpy3_versions(versions_file: str) -> Tuple[str, str]: 

24 attributes = {} 

25 try: 

26 with open(versions_file, "rt", encoding="utf-8") as fd: 

27 for line in fd: 

28 if line.startswith("#") or line.isspace(): 

29 continue 

30 k, v = line.split() 

31 attributes[k] = v 

32 except FileNotFoundError: 

33 _error( 

34 f"Missing Build-Dependency on python3-numpy to ensure {versions_file}" 

35 " is present." 

36 ) 

37 

38 try: 

39 api_min_version = attributes["api-min-version"] 

40 abi_version = attributes["abi"] 

41 except KeyError as e: 

42 k = e.args[0] 

43 _error(f'Expected {versions_file} to contain the key "{k}"') 

44 assert False 

45 

46 return api_min_version, abi_version 

47 

48 

49def numpy3_versions( 

50 _unused: Any, 

51 ctrl: BinaryCtrlAccessor, 

52 context: PackageProcessingContext, 

53) -> None: 

54 if context.binary_package.is_arch_all: 

55 dep = "python3-numpy" 

56 else: 

57 # Note we do not support --strict; codesearch.d.n suggests it is not used 

58 # anywhere and this saves us figuring out how to support it here. 

59 versions_file = os.environ.get("_NUMPY_TEST_PATH", "/usr/share/numpy3/versions") 

60 api_min_version, abi_version = _parse_numpy3_versions(versions_file) 

61 dep = f"python3-numpy (>= {api_min_version}), python3-numpy-abi{abi_version}" 

62 ctrl.substvars.add_dependency("python3:Depends", dep)