diff --git a/docs/source/conf.py b/docs/source/conf.py index 73117b0fb..6027d8e5a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -24,10 +24,14 @@ # -- Project information ----------------------------------------------------- +# Import the version string +from simopt import __version__ # noqa: E402 + project = "SimOpt" -copyright = "2025, simopt-admin" # noqa: A001 +project_copyright = "%Y, simopt-admin" author = "simopt-admin" -release = "1.2.1" +release = __version__ +version: str = ".".join(release.split(".")[:2]) # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index 3e7ec4adb..698770c27 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ exclude = ["build*"] [project] name = "simoptlib" -version = "1.2.2.dev0" +dynamic = ["version"] authors = [ { name = "David Eckman", email = "eckman@tamu.edu" }, { name = "Shane Henderson", email = "sgh9@cornell.edu" }, @@ -54,6 +54,9 @@ dev = [ docs = ["sphinx>=8.2.3", "sphinx-autoapi>=3.6.1", "sphinx-rtd-theme>=3.0.2"] notebooks = ["ipykernel>=7.1.0"] +[tool.setuptools.dynamic] +version = { attr = "simopt.__version__" } + [project.urls] "Homepage" = "https://github.com/simopt-admin/simopt" "Documentation" = "https://simopt.readthedocs.io/en/latest/" diff --git a/scripts/sync_citation_version.py b/scripts/sync_citation_version.py new file mode 100644 index 000000000..1ba88dff9 --- /dev/null +++ b/scripts/sync_citation_version.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +"""A script to synchronize the version from _version.py to CITATION.cff.""" + +import re +import sys +from pathlib import Path + +# --- Configuration --------------------------------------------------- + +# Append the parent directory (simopt package) to the system path +SIMOPT_PACKAGE_DIR = Path(__file__).resolve().parent.parent +sys.path.append(str(SIMOPT_PACKAGE_DIR)) + +# Source directory containing the _version.py file +SIMOPT_DIR = SIMOPT_PACKAGE_DIR / "simopt" + +# Source of the citation file +CITATION_FILENAME = "CITATION.cff" +CITATION_FILE: Path = SIMOPT_PACKAGE_DIR / CITATION_FILENAME + +# Regex pattern to identify the version line in CITATION.cff + +VERSION_PATTERN = re.compile(r"^(version:\s*).+$") + +# --------------------------------------------------------------------- + + +def main() -> None: + """Reads the version and updates the CITATION.cff file.""" + # --- Get the source version --- + from simopt._version import __version__ + + # --- Read and update the CITATION.cff file --- + if not CITATION_FILE.exists(): + print(f"Error: File not found at {CITATION_FILE}") + sys.exit(1) + + # We use 'str(__version__)' to handle non-string version types + # and add quotes to be safe YAML, especially for pre-release tags. + replacement_line = f'version: "{__version__!s}"' + + print(f"Updating {CITATION_FILE}...") + + new_lines = [] + found = False + + with CITATION_FILE.open("r") as f: + for line in f: + # Check if the line matches our version pattern + if not found and VERSION_PATTERN.match(line): + # If it matches, replace it with our new line + new_lines.append(replacement_line + "\n") + found = True + print(f" - Replaced: {line.strip()}") + print(f" + With: {replacement_line}") + else: + # Otherwise, keep the line as-is + new_lines.append(line) + + if not found: + print("Error: A 'version:' line was not found in the file.") + sys.exit(1) + + # Write the modified content back to the file + with CITATION_FILE.open("w") as f: + f.writelines(new_lines) + + print("Update complete.") + + +if __name__ == "__main__": + main() diff --git a/simopt/__init__.py b/simopt/__init__.py index 954d5c087..0fc6b927f 100644 --- a/simopt/__init__.py +++ b/simopt/__init__.py @@ -1 +1,4 @@ """Init file for the simopt package.""" + +# Make the version easily accessible to users +from simopt._version import __version__ # noqa: F401 diff --git a/simopt/_version.py b/simopt/_version.py new file mode 100644 index 000000000..21d49100c --- /dev/null +++ b/simopt/_version.py @@ -0,0 +1 @@ +__version__ = "1.2.2.dev0"