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

38 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-07 12:14 +0200

1from typing import Optional, Callable 

2 

3__version__ = "N/A" 

4 

5IS_RELEASE_BUILD = False 

6 

7if __version__ in ("N/A",): 7 ↛ 67line 7 didn't jump to line 67, because the condition on line 7 was never false

8 import subprocess 

9 

10 class LazyString: 

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

12 self._initializer = initializer 

13 self._value: Optional[str] = None 

14 

15 def __str__(self) -> str: 

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

17 if value is None: 

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

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

20 return value 

21 

22 def __getattribute__(self, item): 

23 value = str(self) 

24 return getattr(value, item) 

25 

26 def __contains__(self, item): 

27 return item in str(self) 

28 

29 def _initialize_version() -> str: 

30 try: 

31 devnull: Optional[int] = subprocess.DEVNULL 

32 except AttributeError: 

33 devnull = None # Not supported, but not critical 

34 

35 try: 

36 v = ( 

37 subprocess.check_output( 

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

39 stderr=devnull, 

40 ) 

41 .strip() 

42 .decode("utf-8") 

43 ) 

44 except (subprocess.CalledProcessError, FileNotFoundError): 

45 try: 

46 v = ( 

47 subprocess.check_output( 

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

49 stderr=devnull, 

50 ) 

51 .strip() 

52 .decode("utf-8") 

53 ) 

54 

55 except (subprocess.CalledProcessError, FileNotFoundError): 

56 v = "N/A" 

57 

58 if v.startswith("debian/"): 58 ↛ 60line 58 didn't jump to line 60, because the condition on line 58 was never false

59 v = v[7:] 

60 return v 

61 

62 __version__ = LazyString(_initialize_version) 

63 IS_RELEASE_BUILD = False 

64 

65else: 

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

67 IS_RELEASE_BUILD = ".gbp" not in __version__