forked from The-DevOps-Daily/devops-daily
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__version__.py
More file actions
42 lines (29 loc) · 1.13 KB
/
__version__.py
File metadata and controls
42 lines (29 loc) · 1.13 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
import os
import re
from importlib.metadata import PackageNotFoundError, version as pkg_version
APP_NAME = "101-linux"
PACKAGE_NAME = "linux-commands-cli"
def _discover_package_version() -> str:
"""Discover version from installed package or local setup.py when running from source.
- Prefer the installed distribution version (if available)
- Fallback to parsing version from cli/setup.py (source checkout)
- Lastly, fallback to "0.0.0" if nothing else is found
"""
try:
return pkg_version(PACKAGE_NAME)
except PackageNotFoundError:
pass
setup_path = os.path.join(os.path.dirname(__file__), "..", "setup.py")
if os.path.exists(setup_path):
try:
with open(setup_path, "r", encoding="utf-8") as f:
content = f.read()
match = re.search(r"version\s*=\s*[\"'](.*?)[\"']", content)
if match:
return match.group(1)
except (OSError, IOError, UnicodeDecodeError):
pass
return "0.0.0"
__version__ = _discover_package_version()
def get_version_string() -> str:
return f"{APP_NAME} v{__version__}"