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

50 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-09-07 09:27 +0000

1from typing import Optional, Callable 

2 

3__version__ = "N/A" 

4 

5IS_RELEASE_BUILD = False 

6_RUN_FROM_SOURCE = False 

7 

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

9 _RUN_FROM_SOURCE = True 

10 import subprocess 

11 

12 class LazyString: 

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

14 self._initializer = initializer 

15 self._value: Optional[str] = None 

16 

17 def __str__(self) -> str: 

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

19 if value is None: 

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

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

22 return value 

23 

24 def __getattribute__(self, item): 

25 value = str(self) 

26 return getattr(value, item) 

27 

28 def __contains__(self, item): 

29 return item in str(self) 

30 

31 def _initialize_version() -> str: 

32 try: 

33 devnull: Optional[int] = subprocess.DEVNULL 

34 except AttributeError: 

35 devnull = None # Not supported, but not critical 

36 

37 try: 

38 v = ( 

39 subprocess.check_output( 

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

41 stderr=devnull, 

42 ) 

43 .strip() 

44 .decode("utf-8") 

45 ) 

46 parts = v.split("-") 

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

48 v = "".join( 

49 ( 

50 parts[0], 

51 ".dev", 

52 parts[1], 

53 "+", 

54 parts[2], 

55 ) 

56 ) 

57 except (subprocess.CalledProcessError, FileNotFoundError): 

58 try: 

59 v = ( 

60 subprocess.check_output( 

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

62 stderr=devnull, 

63 ) 

64 .strip() 

65 .decode("utf-8") 

66 ) 

67 except (subprocess.CalledProcessError, FileNotFoundError): 

68 v = "N/A" 

69 else: 

70 import re 

71 

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

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

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

75 v = "".join( 

76 ( 

77 m.group(1), 

78 ".dev", 

79 m.group(2), 

80 "+", 

81 m.group(3), 

82 ) 

83 ) 

84 

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

86 v = v[8:] 

87 

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

89 v = v[7:] 

90 return v 

91 

92 __version__ = LazyString(_initialize_version) 

93 IS_RELEASE_BUILD = False 

94 

95else: 

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

97 IS_RELEASE_BUILD = ".gbp" not in __version__