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

24 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2026-01-26 19:30 +0000

1"Access to DEB_BUILD_OPTIONS and DEB_BUILD_OPTIONS from the environment." 

2 

3import functools 

4import os 

5from collections.abc import Mapping 

6 

7 

8@functools.cache 

9def _parse_profiles(value: str) -> frozenset[str]: 

10 return frozenset(value.split()) 

11 

12 

13@functools.cache 

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

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

16 for kvish in value.split(): 

17 if "=" in kvish: 

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

19 res[key] = value 

20 else: 

21 res[kvish] = None 

22 return res 

23 

24 

25class DebBuildOptionsAndProfiles: 

26 """Accessor to common environment related values 

27 

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

29 >>> 'noudeb' in env.deb_build_profiles 

30 True 

31 >>> 'nojava' in env.deb_build_profiles 

32 True 

33 >>> 'nopython' in env.deb_build_profiles 

34 False 

35 >>> sorted(env.deb_build_profiles) 

36 ['nojava', 'noudeb'] 

37 """ 

38 

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

40 """Provide a view of the options. 

41 

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

43 """ 

44 self._env = os.environ if environ is None else environ 

45 

46 @property 

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

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

49 

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

51 >>> 'noudeb' in env.deb_build_profiles 

52 True 

53 >>> 'nojava' in env.deb_build_profiles 

54 True 

55 >>> 'nopython' in env.deb_build_profiles 

56 False 

57 >>> sorted(env.deb_build_profiles) 

58 ['nojava', 'noudeb'] 

59 

60 """ 

61 return _parse_profiles(self._env.get("DEB_BUILD_PROFILES", "")) 

62 

63 @property 

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

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

66 

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

68 >>> 'nostrip' in env.deb_build_options 

69 True 

70 >>> 'parallel' in env.deb_build_options 

71 True 

72 >>> 'noautodbgsym' in env.deb_build_options 

73 False 

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

75 True 

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

77 '4' 

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

79 Traceback (most recent call last): 

80 ... 

81 KeyError: 'noautodbgsym' 

82 >>> sorted(env.deb_build_options) 

83 ['nostrip', 'parallel'] 

84 

85 """ 

86 return _parse_deb_build_options(self._env.get("DEB_BUILD_OPTIONS", ""))