Coverage for src/debputy/_deb_options_profiles.py: 91%

28 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-10-12 15:06 +0000

1import os 

2from functools import lru_cache 

3 

4from typing import FrozenSet, Optional, Dict 

5from collections.abc import Mapping 

6 

7 

8def _parse_deb_build_options(value: str) -> Mapping[str, str | None]: 

9 res: dict[str, str | None] = {} 

10 for kvish in value.split(): 

11 if "=" in kvish: 

12 key, value = kvish.split("=", 1) 

13 res[key] = value 

14 else: 

15 res[kvish] = None 

16 return res 

17 

18 

19class DebBuildOptionsAndProfiles: 

20 """Accessor to common environment related values 

21 

22 >>> env = DebBuildOptionsAndProfiles(environ={'DEB_BUILD_PROFILES': 'noudeb nojava'}) 

23 >>> 'noudeb' in env.deb_build_profiles 

24 True 

25 >>> 'nojava' in env.deb_build_profiles 

26 True 

27 >>> 'nopython' in env.deb_build_profiles 

28 False 

29 >>> sorted(env.deb_build_profiles) 

30 ['nojava', 'noudeb'] 

31 """ 

32 

33 def __init__(self, *, environ: Mapping[str, str] | None = None) -> None: 

34 """Provide a view of the options. Though consider using DebBuildOptionsAndProfiles.instance() instead 

35 

36 :param environ: Alternative to os.environ. Mostly useful for testing purposes 

37 """ 

38 if environ is None: 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true

39 environ = os.environ 

40 self._deb_build_profiles = frozenset( 

41 x for x in environ.get("DEB_BUILD_PROFILES", "").split() 

42 ) 

43 self._deb_build_options = _parse_deb_build_options( 

44 environ.get("DEB_BUILD_OPTIONS", "") 

45 ) 

46 

47 @staticmethod 

48 @lru_cache(1) 

49 def instance() -> "DebBuildOptionsAndProfiles": 

50 return DebBuildOptionsAndProfiles() 

51 

52 @property 

53 def deb_build_profiles(self) -> frozenset[str]: 

54 """A set-like view of all build profiles active during the build 

55 

56 >>> env = DebBuildOptionsAndProfiles(environ={'DEB_BUILD_PROFILES': 'noudeb nojava'}) 

57 >>> 'noudeb' in env.deb_build_profiles 

58 True 

59 >>> 'nojava' in env.deb_build_profiles 

60 True 

61 >>> 'nopython' in env.deb_build_profiles 

62 False 

63 >>> sorted(env.deb_build_profiles) 

64 ['nojava', 'noudeb'] 

65 

66 """ 

67 return self._deb_build_profiles 

68 

69 @property 

70 def deb_build_options(self) -> Mapping[str, str | None]: 

71 """A set-like view of all build profiles active during the build 

72 

73 >>> env = DebBuildOptionsAndProfiles(environ={'DEB_BUILD_OPTIONS': 'nostrip parallel=4'}) 

74 >>> 'nostrip' in env.deb_build_options 

75 True 

76 >>> 'parallel' in env.deb_build_options 

77 True 

78 >>> 'noautodbgsym' in env.deb_build_options 

79 False 

80 >>> env.deb_build_options['nostrip'] is None 

81 True 

82 >>> env.deb_build_options['parallel'] 

83 '4' 

84 >>> env.deb_build_options['noautodbgsym'] 

85 Traceback (most recent call last): 

86 ... 

87 KeyError: 'noautodbgsym' 

88 >>> sorted(env.deb_build_options) 

89 ['nostrip', 'parallel'] 

90 

91 """ 

92 return self._deb_build_options