|
4 | 4 | Run `python tools/write_version.py` before building to generate |
5 | 5 | src/git_version/_version.py with the correct version. |
6 | 6 |
|
7 | | -The version in pyproject.toml is a fallback placeholder (0.0.0). |
| 7 | +The version in pyproject.toml is declared as dynamic and resolved |
| 8 | +from git_version._version.__version__ at build time. |
8 | 9 | """ |
9 | 10 |
|
10 | 11 | import os |
11 | 12 | import re |
| 13 | +from pathlib import Path |
12 | 14 |
|
13 | 15 | from setuptools import setup |
14 | 16 |
|
15 | 17 |
|
16 | 18 | def get_version() -> str: |
17 | | - """Get the package version from pyproject.toml.""" |
18 | | - pyproject = os.path.join(os.path.dirname(__file__), "pyproject.toml") |
19 | | - if not os.path.exists(pyproject): |
20 | | - return "0.0.0" |
21 | | - with open(pyproject, "r") as f: |
22 | | - content = f.read() |
23 | | - match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE) |
24 | | - return match.group(1) if match else "0.0.0" |
| 19 | + """Get the package version from the generated _version.py or from git. |
| 20 | +
|
| 21 | + Priority: |
| 22 | + 1. src/git_version/_version.py (generated by tools/write_version.py) |
| 23 | + 2. GIT_VERSION_UTILS_VERSION environment variable |
| 24 | + 3. pyproject.toml fallback (0.0.0) |
| 25 | + """ |
| 26 | + # Try _version.py first (generated by tools/write_version.py) |
| 27 | + version_py = Path(__file__).parent / "src" / "git_version" / "_version.py" |
| 28 | + if version_py.exists(): |
| 29 | + ns: dict[str, str] = {} |
| 30 | + exec(version_py.read_text(), ns) |
| 31 | + if "__version__" in ns: |
| 32 | + return ns["__version__"] |
| 33 | + |
| 34 | + # Try environment variable |
| 35 | + env_version = os.environ.get("GIT_VERSION_UTILS_VERSION") |
| 36 | + if env_version: |
| 37 | + return env_version |
| 38 | + |
| 39 | + # Fallback to pyproject.toml |
| 40 | + pyproject = Path(__file__).parent / "pyproject.toml" |
| 41 | + if pyproject.exists(): |
| 42 | + content = pyproject.read_text() |
| 43 | + match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE) |
| 44 | + if match: |
| 45 | + return match.group(1) |
| 46 | + |
| 47 | + return "0.0.0" |
25 | 48 |
|
26 | 49 |
|
27 | 50 | if __name__ == "__main__": |
|
0 commit comments