Repository navigation
Expand file tree
/
Copy path_version.py
More file actions
61 lines (45 loc) · 1.74 KB
/
Copy path_version.py
File metadata and controls
61 lines (45 loc) · 1.74 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
"""Version information for OMPython.
Resolves the installed package version via importlib.metadata when
OMPython is installed (pip install, editable install, etc.), and falls
back to reading pyproject.toml directly when running from a source
tree with no installed metadata available.
"""
import sys
from importlib.metadata import PackageNotFoundError, version
from pathlib import Path
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
_FALLBACK_VERSION = "unknown"
def _read_version_from_pyproject() -> str:
"""Return the package version from the pyproject.toml file.
Raises:
FileNotFoundError: if pyproject.toml cannot be found.
KeyError: if the version is not statically defined (e.g. dynamic versioning is used).
"""
pyproject_path = Path(__file__).resolve().parents[1] / "pyproject.toml"
with open(pyproject_path, "rb") as pyproject_file:
pyproject = tomllib.load(pyproject_file)
return pyproject["project"]["version"]
def _resolve_version() -> str:
"""Resolve the installed package version.
Preference is given to the version reported by importlib.metadata for the
installed distribution. If the package is not installed, the version is
read from the ``pyproject.toml`` next to this module; both fall back to
a hardcoded string.
Returns:
The OMPython version as a string.
"""
try:
return version(__package__ or "OMPython")
except PackageNotFoundError:
pass
try:
return _read_version_from_pyproject()
except (FileNotFoundError, KeyError):
return _FALLBACK_VERSION
def get_version() -> str:
"""Return the OMPython version string."""
return __version__
__version__ = _resolve_version()