Skip to content

Commit 0193078

Browse files
committed
Don't use subprocess, just use version detected on install
Ok. Finalised
1 parent 27af3a7 commit 0193078

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
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 = "only-version"
63-
local_scheme = "no-local-version"
62+
version_scheme = "no-guess-dev"
63+
local_scheme = "node-and-date"

src/warnet/main.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import subprocess
2-
31
import click
42

53
from .admin import admin
@@ -23,35 +21,44 @@ def cli():
2321
@click.command()
2422
def version() -> None:
2523
"""Display the installed version of warnet"""
26-
# First try to get the version from the installed package
2724
try:
2825
from warnet._version import __version__
2926

30-
version_str = __version__
31-
except ImportError:
32-
version_str = "0.0.0"
27+
# For PyPI releases, this will be the exact tag (e.g. "1.1.11")
28+
# For dev installs, it will be something like "1.1.11.post1.dev17+g27af3a7.d20250309"
29+
# Which is <tag>.post<postN>.dev<devN>+g<git commit hash>.d<YYYYMMDD>
30+
# <postN> is the number of local commits since the checkout commit
31+
# <devN> is the number of commits since the last tag
32+
raw_version = __version__
3333

34-
# Try to get git commit to append
35-
try:
36-
# Check if we're in a git repository
37-
subprocess.check_output(
38-
["git", "rev-parse", "--is-inside-work-tree"], stderr=subprocess.DEVNULL
39-
)
34+
# Format the version string to our desired format
35+
if "+" in raw_version:
36+
version_part, git_date_part = raw_version.split("+", 1)
4037

41-
# Get the short commit hash
42-
commit = subprocess.check_output(
43-
["git", "rev-parse", "--short=8", "HEAD"], stderr=subprocess.DEVNULL, text=True
44-
).strip()
38+
# Get just the git commit hash
39+
commit_hash = (
40+
git_date_part[1:].split(".", 1)[0]
41+
if git_date_part.startswith("g")
42+
else git_date_part.split(".", 1)[0]
43+
)
4544

46-
# If we have a commit hash, append it to the version
47-
# Don't append if already has a hash
48-
if commit and "-" not in version_str:
49-
version_str = f"{version_str}-{commit}"
45+
# Remove .dev component (from "no-guess-dev" scheme)
46+
clean_version = version_part
47+
if ".dev" in clean_version:
48+
clean_version = clean_version.split(".dev")[0]
49+
50+
# Apply dirty status (from "no-guess-dev" scheme)
51+
if ".post" in clean_version:
52+
base = clean_version.split(".post")[0]
53+
version_str = f"{base}-{commit_hash}-dirty"
54+
else:
55+
version_str = f"{clean_version}-{commit_hash}"
56+
else:
57+
version_str = raw_version
5058

5159
click.echo(f"warnet version {version_str}")
52-
except (subprocess.SubprocessError, FileNotFoundError):
53-
# Git commands failed or git not available, just use the version without the commit
54-
click.echo(f"warnet version {version_str}")
60+
except ImportError:
61+
click.echo("warnet version unknown")
5562

5663

5764
cli.add_command(admin)

0 commit comments

Comments
 (0)