Skip to content

Commit 0fdca54

Browse files
committed
initial dynamic versioning code
1 parent a0109cd commit 0fdca54

5 files changed

Lines changed: 88 additions & 4 deletions

File tree

docs/source/conf.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@
1919
# Figure out the top-level directory and add it to the system path
2020
# This is necessary to import the simopt package
2121
src_path = Path(__file__).resolve().parent
22-
project_path = src_path.parents[1]
22+
project_path = src_path.parent.parent
2323
sys.path.insert(0, str(project_path))
2424

2525
# -- Project information -----------------------------------------------------
2626

27+
# Import the version string
28+
from simopt import __version__ # noqa: E402
29+
2730
project = "SimOpt"
28-
copyright = "2025, simopt-admin" # noqa: A001
31+
project_copyright = "%Y, simopt-admin"
2932
author = "simopt-admin"
30-
release = "1.2.0"
33+
release = __version__
34+
version: str = ".".join(release.split(".")[:2])
3135

3236
# -- General configuration ---------------------------------------------------
3337

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages = ["simopt", "simopt.gui", "simopt.models", "simopt.solvers"]
77

88
[project]
99
name = "simoptlib"
10-
version = "1.2.0"
10+
dynamic = ["version"]
1111
authors = [
1212
{ name = "David Eckman", email = "eckman@tamu.edu" },
1313
{ name = "Shane Henderson", email = "sgh9@cornell.edu" },
@@ -42,6 +42,9 @@ dev = ["ruff>=0.14.2", "jupytext"]
4242
docs = ["sphinx>=8.2.3", "sphinx-autoapi>=3.6.1", "sphinx-rtd-theme>=3.0.2"]
4343
notebooks = ["ipykernel"]
4444

45+
[tool.setuptools.dynamic]
46+
version = { attr = "simopt.__version__" }
47+
4548
[project.urls]
4649
"Homepage" = "https://github.com/simopt-admin/simopt"
4750
"Documentation" = "https://simopt.readthedocs.io/en/latest/"

scripts/sync_citation_version.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
3+
"""A script to synchronize the version from _version.py to CITATION.cff."""
4+
5+
import re
6+
import sys
7+
from pathlib import Path
8+
9+
# --- Configuration ---------------------------------------------------
10+
11+
# Append the parent directory (simopt package) to the system path
12+
SIMOPT_PACKAGE_DIR = Path(__file__).resolve().parent.parent
13+
sys.path.append(str(SIMOPT_PACKAGE_DIR))
14+
15+
# Source directory containing the _version.py file
16+
SIMOPT_DIR = SIMOPT_PACKAGE_DIR / "simopt"
17+
18+
# Source of the citation file
19+
CITATION_FILENAME = "CITATION.cff"
20+
CITATION_FILE: Path = SIMOPT_PACKAGE_DIR / CITATION_FILENAME
21+
22+
# Regex pattern to identify the version line in CITATION.cff
23+
24+
VERSION_PATTERN = re.compile(r"^(version:\s*).+$")
25+
26+
# ---------------------------------------------------------------------
27+
28+
29+
def main() -> None:
30+
"""Reads the version and updates the CITATION.cff file."""
31+
# --- Get the source version ---
32+
from simopt._version import __version__
33+
34+
# --- Read and update the CITATION.cff file ---
35+
if not CITATION_FILE.exists():
36+
print(f"Error: File not found at {CITATION_FILE}")
37+
sys.exit(1)
38+
39+
# We use 'str(__version__)' to handle non-string version types
40+
# and add quotes to be safe YAML, especially for pre-release tags.
41+
replacement_line = f'version: "{__version__!s}"'
42+
43+
print(f"Updating {CITATION_FILE}...")
44+
45+
new_lines = []
46+
found = False
47+
48+
with CITATION_FILE.open("r") as f:
49+
for line in f:
50+
# Check if the line matches our version pattern
51+
if not found and VERSION_PATTERN.match(line):
52+
# If it matches, replace it with our new line
53+
new_lines.append(replacement_line + "\n")
54+
found = True
55+
print(f" - Replaced: {line.strip()}")
56+
print(f" + With: {replacement_line}")
57+
else:
58+
# Otherwise, keep the line as-is
59+
new_lines.append(line)
60+
61+
if not found:
62+
print("Error: A 'version:' line was not found in the file.")
63+
sys.exit(1)
64+
65+
# Write the modified content back to the file
66+
with CITATION_FILE.open("w") as f:
67+
f.writelines(new_lines)
68+
69+
print("Update complete.")
70+
71+
72+
if __name__ == "__main__":
73+
main()

simopt/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
"""Init file for the simopt package."""
2+
3+
# Make the version easily accessible to users
4+
from simopt._version import __version__ # noqa: F401

simopt/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "1.2.0"

0 commit comments

Comments
 (0)