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

38 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2026-01-26 19:30 +0000

1"debputy: version and paths computed at build time" 

2 

3import functools 

4import pathlib 

5import re 

6import subprocess 

7 

8# Replaced during install; must be a single line 

9# fmt: off 

10DEBPUTY_ROOT_DIR = pathlib.Path(__file__).parent.parent.parent 

11DEBPUTY_PLUGIN_ROOT_DIR = pathlib.Path(__file__).parent.parent.parent 

12_VERSION = "" 

13# fmt: on 

14 

15DEBPUTY_IS_RUN_FROM_SOURCE = not _VERSION 

16 

17 

18def _from_git() -> str: 

19 try: 

20 v = subprocess.check_output( 

21 ("git", "describe", "--tags"), 

22 stderr=subprocess.DEVNULL, 

23 encoding="utf-8", 

24 ) 

25 except (subprocess.CalledProcessError, FileNotFoundError): 

26 return "" 

27 v = v.rstrip() 

28 

29 parts = v.split("-") 

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

31 v = f"{parts[0]}.dev{parts[1]}+{parts[2]}" 

32 

33 # Git tags contain a prefix like archive/debian/ or debian/. 

34 v = v.removeprefix("archive/") 

35 v = v.removeprefix("debian/") 

36 

37 return v 

38 

39 

40def _from_dch() -> str: 

41 try: 

42 v = subprocess.check_output( 

43 ("dpkg-parsechangelog", "-SVersion"), 

44 stderr=subprocess.DEVNULL, 

45 encoding="utf-8", 

46 ) 

47 except (subprocess.CalledProcessError, FileNotFoundError): 

48 return "" 

49 v = v.rstrip() 

50 

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

52 

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

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

55 v = f"{m.group(1)}.dev{m.group(2)}+{m.group(3)}" 

56 

57 return v 

58 

59 

60@functools.cache 

61def version() -> str: 

62 "The debputy version, extracted from the Debian packaging or git." 

63 return _VERSION or _from_git() or _from_dch() or "N/A" 

64 

65 

66def debputy_doc_root_dir() -> str: 

67 """The doc URL differs in releases and development builds 

68 (including `gbp dch -S` snapshots).""" 

69 is_release_build = _VERSION and ".gbp" not in _VERSION 

70 blob = f"debian/{version()}" if is_release_build else "main" 

71 return f"https://salsa.debian.org/debian/debputy/-/blob/{blob}"