-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
51 lines (39 loc) · 1.5 KB
/
Copy pathsetup.py
File metadata and controls
51 lines (39 loc) · 1.5 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
"""Setup script for git-version-utils.
The package version is determined dynamically from git tags.
Run `python tools/write_version.py` before building to generate
src/git_version/_version.py with the correct version.
The version in pyproject.toml is declared as dynamic and resolved
from git_version._version.__version__ at build time.
"""
import os
import re
from pathlib import Path
from setuptools import setup
def get_version() -> str:
"""Get the package version from the generated _version.py or from git.
Priority:
1. src/git_version/_version.py (generated by tools/write_version.py)
2. GIT_VERSION_UTILS_VERSION environment variable
3. pyproject.toml fallback (0.0.0)
"""
# Try _version.py first (generated by tools/write_version.py)
version_py = Path(__file__).parent / "src" / "git_version" / "_version.py"
if version_py.exists():
ns: dict[str, str] = {}
exec(version_py.read_text(), ns)
if "__version__" in ns:
return ns["__version__"]
# Try environment variable
env_version = os.environ.get("GIT_VERSION_UTILS_VERSION")
if env_version:
return env_version
# Fallback to pyproject.toml
pyproject = Path(__file__).parent / "pyproject.toml"
if pyproject.exists():
content = pyproject.read_text()
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
if match:
return match.group(1)
return "0.0.0"
if __name__ == "__main__":
setup(version=get_version())