-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path_version.py
More file actions
48 lines (38 loc) · 1.63 KB
/
Copy path_version.py
File metadata and controls
48 lines (38 loc) · 1.63 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
"""Best-effort version string derived from git, shared by both converters.
Stdlib-only on purpose: ``ee_to_pipewire.py`` imports this for ``--version``
and must not pull numpy/scipy into its startup path just to read a version.
"""
import subprocess
from pathlib import Path
__all__ = ["get_version"]
_CACHE: str | None = None
def get_version(repo_dir: Path | None = None) -> str:
"""Return a ``git describe`` version string, or ``"unknown"``.
Never raises. Falls back to ``"unknown"`` when there is no git CLI, the
directory is not a git checkout (e.g. a tarball download), or git fails
for any other reason. With no tags yet, ``--always`` yields the short
commit hash; a dirty tree gets a ``-dirty`` suffix.
``repo_dir`` defaults to this module's directory and exists mainly so
tests can point the lookup at a temporary non-repo directory; results are
only cached for the default (no-argument) call.
"""
global _CACHE
if repo_dir is None and _CACHE is not None:
return _CACHE
base = repo_dir if repo_dir is not None else Path(__file__).resolve().parent
version = "unknown"
try:
result = subprocess.run(
["git", "-C", str(base), "describe", "--tags", "--always", "--dirty"],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode == 0 and result.stdout.strip():
version = result.stdout.strip()
except (OSError, subprocess.SubprocessError):
# FileNotFoundError (no git binary) is an OSError; timeouts etc. too.
pass
if repo_dir is None:
_CACHE = version
return version