Coverage for src/debputy/version.py: 78%
42 statements
« prev ^ index » next coverage.py v7.6.0, created at 2025-01-27 13:59 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2025-01-27 13:59 +0000
1from typing import Optional, Callable
3__version__ = "N/A"
5IS_RELEASE_BUILD = False
6_RUN_FROM_SOURCE = False
8if __version__ in ("N/A",): 8 ↛ 72line 8 didn't jump to line 72 because the condition on line 8 was always true
9 _RUN_FROM_SOURCE = True
10 import subprocess
12 class LazyString:
13 def __init__(self, initializer: Callable[[], str]) -> None:
14 self._initializer = initializer
15 self._value: Optional[str] = None
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
24 def __getattribute__(self, item):
25 value = str(self)
26 return getattr(value, item)
28 def __contains__(self, item):
29 return item in str(self)
31 def _initialize_version() -> str:
32 try:
33 devnull: Optional[int] = subprocess.DEVNULL
34 except AttributeError:
35 devnull = None # Not supported, but not critical
37 try:
38 v = (
39 subprocess.check_output(
40 ["git", "describe", "--tags"],
41 stderr=devnull,
42 )
43 .strip()
44 .decode("utf-8")
45 )
46 except (subprocess.CalledProcessError, FileNotFoundError):
47 try:
48 v = (
49 subprocess.check_output(
50 ["dpkg-parsechangelog", "-SVersion"],
51 stderr=devnull,
52 )
53 .strip()
54 .decode("utf-8")
55 )
57 except (subprocess.CalledProcessError, FileNotFoundError):
58 v = "N/A"
60 if v.startswith("archive/"): 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true
61 v = v[8:]
63 if v.startswith("debian/"): 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true
64 v = v[7:]
65 return v
67 __version__ = LazyString(_initialize_version)
68 IS_RELEASE_BUILD = False
70else:
71 # Disregard snapshot versions (gbp dch -S) as "release builds"
72 IS_RELEASE_BUILD = ".gbp" not in __version__