Skip to content

Commit 217ac00

Browse files
committed
Local and distribution versioning
1 parent 49fec67 commit 217ac00

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ include = ["warnet*", "test_framework*", "resources*"]
5959

6060
[tool.setuptools_scm]
6161
write_to = "src/warnet/_version.py"
62-
version_scheme = "guess-next-dev"
63-
local_scheme = "dirty-tag"
62+
# Note: Custom version scheme is handled in setup.py
63+
fallback_version = "0.0.0"

setup.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import subprocess
2+
3+
from setuptools import setup
4+
5+
6+
def version(version) -> str:
7+
"""
8+
Format the version string.
9+
This function is called by setuptools_scm to determine the distribution version.
10+
"""
11+
# Use base_version if available, otherwise fall back to a default
12+
if hasattr(version, "base_version") and version.base_version:
13+
vers = version.base_version
14+
elif hasattr(version, "tag") and version.tag:
15+
# If we have a tag but no base_version, try to extract from tag
16+
vers = str(version.tag).lstrip("v")
17+
else:
18+
# Default fallback version if nothing else is available
19+
vers = "0.0.0"
20+
21+
return vers
22+
23+
24+
def local_version(version) -> str:
25+
"""
26+
Format the local version using the git hash and status of the working directory.
27+
If all succeeds, the returned string will be something like "63fe5aa1-dirty"
28+
This function is called by setuptools_scm to determine the local version part.
29+
"""
30+
suffix = ""
31+
32+
try:
33+
# Get the short git hash
34+
result = subprocess.run(
35+
["git", "rev-parse", "--short=8", "HEAD"], capture_output=True, text=True, check=True
36+
)
37+
commit = result.stdout.strip()
38+
39+
if commit:
40+
suffix = f"{commit}"
41+
42+
# Check if working directory is dirty
43+
status_result = subprocess.run(
44+
["git", "status", "--porcelain"], capture_output=True, text=True, check=True
45+
)
46+
47+
if status_result.stdout.strip():
48+
suffix += "-dirty"
49+
except (subprocess.SubprocessError, FileNotFoundError):
50+
# Ignore errors from git commands
51+
pass
52+
53+
return suffix
54+
55+
56+
# Using setuptools_scm with custom version functions
57+
setup(
58+
use_scm_version={
59+
"version_scheme": version,
60+
"local_scheme": local_version,
61+
"fallback_version": "0.0.0",
62+
"relative_to": __file__,
63+
}
64+
)

src/warnet/main.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import subprocess
2+
13
import click
24

35
from .admin import admin
@@ -21,12 +23,59 @@ def cli():
2123
@click.command()
2224
def version() -> None:
2325
"""Display the installed version of warnet"""
26+
# Try to get dynamic version from the Git repository
27+
# This allows a developer to get up-to-date version information
28+
# depending on the state of their local git repository
29+
# (e.g. when installed from source in editable mode).
30+
try:
31+
# Check if we're in a git repository
32+
subprocess.check_output(
33+
["git", "rev-parse", "--is-inside-work-tree"], stderr=subprocess.DEVNULL
34+
)
35+
36+
# Get the base version from the latest tag
37+
try:
38+
tag = (
39+
subprocess.check_output(
40+
["git", "describe", "--tags", "--abbrev=0"],
41+
stderr=subprocess.DEVNULL,
42+
text=True,
43+
)
44+
.strip()
45+
.lstrip("v")
46+
)
47+
except subprocess.SubprocessError:
48+
# No tags found
49+
tag = "0.0.0"
50+
51+
# Get the short commit hash
52+
commit = subprocess.check_output(
53+
["git", "rev-parse", "--short=8", "HEAD"], stderr=subprocess.DEVNULL, text=True
54+
).strip()
55+
56+
# Check if there are uncommitted changes
57+
status = subprocess.check_output(
58+
["git", "status", "--porcelain"], stderr=subprocess.DEVNULL, text=True
59+
).strip()
60+
61+
# Format the version string to match setup.py
62+
version_str = tag
63+
if commit:
64+
version_str += f".{commit}"
65+
if status:
66+
version_str += "-dirty"
67+
68+
click.echo(f"warnet version {version_str} (from git)")
69+
return
70+
except (subprocess.SubprocessError, FileNotFoundError):
71+
# Git commands failed or git not available, fall back to installed version
72+
pass
73+
74+
# Fall back to the version file generated during installation
2475
try:
2576
from warnet._version import __version__
2677

2778
version = __version__
28-
# If running from source/git, setuptools_scm will append git info
29-
# e.g. "1.1.11.dev1+g123456[.dirty]"
3079
click.echo(f"warnet version {version}")
3180
except ImportError:
3281
click.echo("warnet version unknown")

0 commit comments

Comments
 (0)