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

27 statements  

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

1import os 

2from functools import lru_cache 

3 

4from typing import FrozenSet, Optional, Mapping, Dict 

5 

6 

7def _parse_deb_build_options(value: str) -> Mapping[str, Optional[str]]: 

8 res: Dict[str, Optional[str]] = {} 

9 for kvish in value.split(): 

10 if "=" in kvish: 

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

12 res[key] = value 

13 else: 

14 res[kvish] = None 

15 return res 

16 

17 

18class DebBuildOptionsAndProfiles: 

19 """Accessor to common environment related values 

20 

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

22 >>> 'noudeb' in env.deb_build_profiles 

23 True 

24 >>> 'nojava' in env.deb_build_profiles 

25 True 

26 >>> 'nopython' in env.deb_build_profiles 

27 False 

28 >>> sorted(env.deb_build_profiles) 

29 ['nojava', 'noudeb'] 

30 """ 

31 

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

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

34 

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

36 """ 

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

38 environ = os.environ 

39 self._deb_build_profiles = frozenset( 

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

41 ) 

42 self._deb_build_options = _parse_deb_build_options( 

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

44 ) 

45 

46 @staticmethod 

47 @lru_cache(1) 

48 def instance() -> "DebBuildOptionsAndProfiles": 

49 return DebBuildOptionsAndProfiles() 

50 

51 @property 

52 def deb_build_profiles(self) -> FrozenSet[str]: 

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

54 

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

56 >>> 'noudeb' in env.deb_build_profiles 

57 True 

58 >>> 'nojava' in env.deb_build_profiles 

59 True 

60 >>> 'nopython' in env.deb_build_profiles 

61 False 

62 >>> sorted(env.deb_build_profiles) 

63 ['nojava', 'noudeb'] 

64 

65 """ 

66 return self._deb_build_profiles 

67 

68 @property 

69 def deb_build_options(self) -> Mapping[str, Optional[str]]: 

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

71 

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

73 >>> 'nostrip' in env.deb_build_options 

74 True 

75 >>> 'parallel' in env.deb_build_options 

76 True 

77 >>> 'noautodbgsym' in env.deb_build_options 

78 False 

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

80 True 

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

82 '4' 

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

84 Traceback (most recent call last): 

85 ... 

86 KeyError: 'noautodbgsym' 

87 >>> sorted(env.deb_build_options) 

88 ['nostrip', 'parallel'] 

89 

90 """ 

91 return self._deb_build_options