Coverage for src/debputy/version.py: 71%

51 statements  

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

1from typing import Optional 

2from collections.abc import Callable 

3 

4__version__ = "N/A" 

5 

6IS_RELEASE_BUILD = False 

7_RUN_FROM_SOURCE = False 

8 

9if __version__ in ("N/A",): 9 ↛ 98line 9 didn't jump to line 98 because the condition on line 9 was always true

10 _RUN_FROM_SOURCE = True 

11 import subprocess 

12 

13 class LazyString: 

14 def __init__(self, initializer: Callable[[], str]) -> None: 

15 self._initializer = initializer 

16 self._value: str | None = None 

17 

18 def __str__(self) -> str: 

19 value = object.__getattribute__(self, "_value") 

20 if value is None: 

21 value = object.__getattribute__(self, "_initializer")() 

22 object.__setattr__(self, "_value", value) 

23 return value 

24 

25 def __getattribute__(self, item): 

26 value = str(self) 

27 return getattr(value, item) 

28 

29 def __contains__(self, item): 

30 return item in str(self) 

31 

32 def _initialize_version() -> str: 

33 try: 

34 devnull: int | None = subprocess.DEVNULL 

35 except AttributeError: 

36 devnull = None # Not supported, but not critical 

37 

38 try: 

39 v = ( 

40 subprocess.check_output( 

41 ["git", "describe", "--tags"], 

42 stderr=devnull, 

43 ) 

44 .strip() 

45 .decode("utf-8") 

46 ) 

47 parts = v.split("-") 

48 if "-g" in v and len(parts) == 3: 

49 v = "".join( 

50 ( 

51 parts[0], 

52 ".dev", 

53 parts[1], 

54 "+", 

55 parts[2], 

56 ) 

57 ) 

58 except (subprocess.CalledProcessError, FileNotFoundError): 

59 try: 

60 v = ( 

61 subprocess.check_output( 

62 ["dpkg-parsechangelog", "-SVersion"], 

63 stderr=devnull, 

64 ) 

65 .strip() 

66 .decode("utf-8") 

67 ) 

68 except (subprocess.CalledProcessError, FileNotFoundError): 

69 v = "N/A" 

70 else: 

71 import re 

72 

73 v = re.sub(r"~bpo\d+[+]\d+$", "", v) 

74 m = re.match(r"(.+)~(\d+)[.]g(.+)", v) 

75 if m: 75 ↛ 76line 75 didn't jump to line 76 because the condition on line 75 was never true

76 v = "".join( 

77 ( 

78 m.group(1), 

79 ".dev", 

80 m.group(2), 

81 "+", 

82 m.group(3), 

83 ) 

84 ) 

85 

86 if v.startswith("archive/"): 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true

87 v = v[8:] 

88 

89 if v.startswith("debian/"): 89 ↛ 90line 89 didn't jump to line 90 because the condition on line 89 was never true

90 v = v[7:] 

91 return v 

92 

93 __version__ = LazyString(_initialize_version) 

94 IS_RELEASE_BUILD = False 

95 

96else: 

97 # Disregard snapshot versions (gbp dch -S) as "release builds" 

98 IS_RELEASE_BUILD = ".gbp" not in __version__