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
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-07 12:14 +0200
1from typing import Optional, Callable
3__version__ = "N/A"
5IS_RELEASE_BUILD = False
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
10 class LazyString:
11 def __init__(self, initializer: Callable[[], str]) -> None:
12 self._initializer = initializer
13 self._value: Optional[str] = None
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
22 def __getattribute__(self, item):
23 value = str(self)
24 return getattr(value, item)
26 def __contains__(self, item):
27 return item in str(self)
29 def _initialize_version() -> str:
30 try:
31 devnull: Optional[int] = subprocess.DEVNULL
32 except AttributeError:
33 devnull = None # Not supported, but not critical
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 )
55 except (subprocess.CalledProcessError, FileNotFoundError):
56 v = "N/A"
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
62 __version__ = LazyString(_initialize_version)
63 IS_RELEASE_BUILD = False
65else:
66 # Disregard snapshot versions (gbp dch -S) as "release builds"
67 IS_RELEASE_BUILD = ".gbp" not in __version__