-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.py
More file actions
79 lines (62 loc) · 2.33 KB
/
Copy pathversion.py
File metadata and controls
79 lines (62 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Single source of truth for the LinuxPop version string.
Resolution order (first hit wins):
1. A baked ``_version.py`` (``VERSION = "x.y.z"``) sitting next to this file.
It is generated from the git tag at *package build time*
(see ``packaging/gen-version.sh``) and is what installed / Flatpak / .deb
copies use, since those have no ``.git`` to inspect.
2. ``git describe --tags`` when running straight from a git checkout, i.e.
during development. Gives ``"0.9.7"`` on a tagged commit, or
``"0.9.7-3-gabc1234"`` a few commits later so dev builds are identifiable.
3. The newest ``<release>`` in the AppStream metainfo, if reachable.
4. ``"0.0.0"`` as a last resort.
Because (1) is generated from the git tag and (2) reads the tag directly, the
About dialog, ``--version`` output and the crash-log header can no longer drift
from the actual release: bump the tag and everything follows.
"""
from __future__ import annotations
import subprocess
from pathlib import Path
_HERE = Path(__file__).resolve().parent
def _from_baked() -> str | None:
try:
from _version import VERSION # type: ignore
except Exception:
return None
return VERSION or None
def _from_git() -> str | None:
if not (_HERE / ".git").exists():
return None
try:
out = subprocess.run(
["git", "describe", "--tags", "--always", "--dirty"],
cwd=_HERE,
capture_output=True,
text=True,
timeout=2,
)
except Exception:
return None
if out.returncode != 0:
return None
tag = out.stdout.strip()
if not tag:
return None
return tag[1:] if tag.startswith("v") else tag
def _from_metainfo() -> str | None:
import re
candidates = (
_HERE / "packaging" / "io.github.GaimsDevSoftware.LinuxPop.metainfo.xml",
_HERE.parent / "metainfo" / "io.github.GaimsDevSoftware.LinuxPop.metainfo.xml",
)
for path in candidates:
try:
text = path.read_text(encoding="utf-8")
except OSError:
continue
match = re.search(r'<release[^>]*\bversion="([^"]+)"', text)
if match:
return match.group(1)
return None
def get_version() -> str:
return _from_baked() or _from_git() or _from_metainfo() or "0.0.0"
__version__ = get_version()