Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 0 additions & 99 deletions Version/__init__.tpl

This file was deleted.

122 changes: 68 additions & 54 deletions Version/version.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,98 @@
import re
import subprocess
from pathlib import Path

from setuptools_scm import ScmVersion, get_version
from setuptools_scm._integration.dump_version import write_version_to_path

BASE_DIR = Path(__file__).parent
REPO_DIR = BASE_DIR / ".."
PYRX_INIT_PY_TPL = BASE_DIR / "__init__.tpl"
PYRX_INIT_PY = BASE_DIR / "../pyrx/__init__.py"
BASE_VERSION_FILE = BASE_DIR / "version.txt"
PYRX_VERSION_PY = BASE_DIR / "../pyrx/_version.py"
PYRX_VERSION_H_TPL = BASE_DIR / "pyrx_version.tpl"
PYRX_VERSION_H = BASE_DIR / "../pyrx_version.h"
PYRX_PACKAGE_CONTENTS_TPL = BASE_DIR / "PackageContents.tpl"
PYRX_PACKAGE_CONTENTS = BASE_DIR / "../PyRxAcSetup/Include/PackageContents.xml"

MAJOR = 2
MINOR = 2
REVISION= 23
BASE_VERSION_PATTERN = re.compile(r"^(\d+)\.(\d+)\.(\d+)$")

def custom_version_scheme(version: ScmVersion) -> str:
base_version = version.format_with("{tag}")

def get_base_version() -> str:
version = BASE_VERSION_FILE.read_text("utf-8").strip()
if not BASE_VERSION_PATTERN.fullmatch(version):
raise ValueError(f"Invalid version format: {version}")
return version


def get_revision_number() -> str:
number = subprocess.check_output(
["git", "rev-list", "--count", "--first-parent", "main"], text=True
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subprocess call does not specify a working directory. This could lead to unexpected behavior if the script is run from a different directory. Consider adding cwd=REPO_DIR parameter to ensure consistency.

Suggested change
["git", "rev-list", "--count", "--first-parent", "main"], text=True
["git", "rev-list", "--count", "--first-parent", "main"], text=True, cwd=str(REPO_DIR)

Copilot uses AI. Check for mistakes.
).strip()
if not number.isdigit():
raise ValueError(f"Revision number is not a digit: {number}")
return number


def get_project_version() -> tuple[str, tuple[str, str, str, str]]:
base_version = get_base_version()
try:
count = subprocess.check_output(
["git", "rev-list", "--count", "--first-parent", "main"], text=True, cwd=REPO_DIR
).strip()
revision = get_revision_number()
except Exception as err:
err.add_note("Failed to get commit count from git.")
raise err
version_tuple = parse_version(base_version)

return f"{base_version}.{count}"
return f"{base_version}.{revision}", (*version_tuple, revision)


def get_project_version():
return get_version(
root=REPO_DIR,
version_scheme=custom_version_scheme,
local_scheme="no-local-version",
)
def parse_version(version: str) -> tuple:
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type annotation tuple is too generic. Consider using tuple[str, str, str] to specify that it returns a 3-tuple of strings representing major, minor, and revision components.

Suggested change
def parse_version(version: str) -> tuple:
def parse_version(version: str) -> tuple[str, str, str]:

Copilot uses AI. Check for mistakes.
match = BASE_VERSION_PATTERN.fullmatch(version)
if not match:
raise ValueError(f"Invalid version format: {version}")
return match.groups()

def override_pyrx_version_h(version: str) -> str:
_, _, _, revision = version.split(".")
revision_inc = f"{int(revision) + 1}"

def override_pyrx_version_h(major: str, minor: str, revision: str, version_revision: str) -> None:
content = PYRX_VERSION_H_TPL.read_text("utf-8")
content = content.replace("{MAJOR}", str(MAJOR))
content = content.replace("{MINOR}", str(MINOR))
content = content.replace("{REVISION}", str(REVISION))
content = content.replace("{VERSION_REVISION}", revision_inc)
content = (
content.replace("{MAJOR}", str(major))
.replace("{MINOR}", str(minor))
.replace("{REVISION}", str(revision))
.replace("{VERSION_REVISION}", version_revision)
)
PYRX_VERSION_H.write_text(content, "utf-8")
return revision_inc


def override_pyrx_init_h(version: str) -> str:
_, _, _, revision = version.split(".")
revision_inc = f"{int(revision) + 1}"
content = PYRX_INIT_PY_TPL.read_text("utf-8")
content = content.replace("{MAJOR}", str(MAJOR))
content = content.replace("{MINOR}", str(MINOR))
content = content.replace("{REVISION}", str(REVISION))
content = content.replace("{VERSION_REVISION}", revision_inc)
PYRX_INIT_PY.write_text(content, "utf-8")
return revision_inc

def override_package_contents(version: str) -> str:
_, _, _, revision = version.split(".")
revision_inc = f"{int(revision) + 1}"
content = PYRX_PACKAGE_CONTENTS_TPL.read_text("utf-8")
content = content.replace("{MAJOR}", str(MAJOR))
content = content.replace("{MINOR}", str(MINOR))
content = content.replace("{REVISION}", str(REVISION))
print(
f"The version in the file {PYRX_VERSION_H.name} "
f"has been set to: {major}.{minor}.{revision}.{version_revision}"
)


def override_pyrx_version_py(major: str, minor: str, revision: str, version_revision: str) -> None:
version = f"{major}.{minor}.{revision}.{version_revision}"
write_version_to_path(PYRX_VERSION_PY, template=None, version=version, scm_version=None)
print(f"The version in the file {PYRX_VERSION_PY.name} has been set to: {version}")


def override_package_contents(major: str, minor: str, revision: str) -> None:
content = (
PYRX_PACKAGE_CONTENTS_TPL.read_text("utf-8")
.replace("{MAJOR}", str(major))
.replace("{MINOR}", str(minor))
.replace("{REVISION}", str(revision))
)
PYRX_PACKAGE_CONTENTS.write_text(content, "utf-8")
return revision_inc
print(
f"The version in the file {PYRX_PACKAGE_CONTENTS.name} "
f"has been set to: {major}.{minor}.{revision}"
)


def main():
version = get_project_version()
revision_inc = override_pyrx_version_h(version)
print(f"The revision number in the file {PYRX_VERSION_H.name} has been set to: {revision_inc}")
version_inc = override_pyrx_init_h(version)
print(f"The version in the file {PYRX_INIT_PY.name} has been set to: {version_inc}")
version_inc = override_package_contents(version)
print(f"The version in the file {PYRX_PACKAGE_CONTENTS.name} has been set to: {version_inc}")
_, version_tuple = get_project_version()
major, minor, revision, version_revision = version_tuple
version_revision_inc = f"{int(version_revision) + 1}"
override_pyrx_version_h(major, minor, revision, version_revision_inc)
override_pyrx_version_py(major, minor, revision, version_revision_inc)
override_package_contents(major, minor, revision)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions Version/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.23
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[build-system]
requires = ["setuptools >= 61.0"]
requires = ["setuptools >= 61.0", "setuptools-scm>=8"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
include = ["pyrx*"]
namespaces = false

[tool.setuptools.dynamic]
version = { attr = "pyrx.__version__" }
[tool.setuptools_scm]
write_to = "pyrx/_version.py"
local_scheme = "no-local-version"

[project]
name = "cad-pyrx"
Expand Down
4 changes: 2 additions & 2 deletions pyrx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#-----------------------------------------------------
# -----------------------------------------------------
# auto generated from __init__.tpl

"""
Expand All @@ -24,7 +24,7 @@
import warnings
from typing import TYPE_CHECKING

__version__ = "2.2.23.4619"
from ._version import __version__ # noqa: F401

try:
import PyRx as Rx # isort: skip # type: ignore
Expand Down
40 changes: 38 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
import re
import subprocess
from pathlib import Path

from setuptools import setup
from setuptools_scm import ScmVersion

BASE_DIR = Path(__file__).parent
BASE_VERSION_FILE = BASE_DIR / "Version/version.txt"
BASE_VERSION_PATTERN = re.compile(r"^(\d+)\.(\d+)\.(\d+)$")


def get_base_version() -> str:
version = BASE_VERSION_FILE.read_text("utf-8").strip()
if not BASE_VERSION_PATTERN.fullmatch(version):
raise ValueError(f"Invalid version format: {version}")
return version


def get_revision_number() -> str:
number = subprocess.check_output(
["git", "rev-list", "--count", "--first-parent", "main"], text=True
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subprocess call does not specify a working directory and could potentially execute from an unexpected location. Consider adding the cwd parameter to ensure the git command runs from the correct repository directory.

Suggested change
["git", "rev-list", "--count", "--first-parent", "main"], text=True
["git", "rev-list", "--count", "--first-parent", "main"], text=True, cwd=BASE_DIR

Copilot uses AI. Check for mistakes.
).strip()
if not number.isdigit():
raise ValueError(f"Revision number is not a digit: {number}")
return number


def version_scheme(version: ScmVersion) -> str:
base_version = get_base_version()
try:
revision = get_revision_number()
except Exception:
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching all exceptions with except Exception: is too broad. Consider catching specific exceptions like subprocess.CalledProcessError or FileNotFoundError to handle known failure cases appropriately.

Suggested change
except Exception:
except (subprocess.CalledProcessError, FileNotFoundError, OSError):

Copilot uses AI. Check for mistakes.
revision = "0"
return f"{base_version}.{revision}"


setup(
long_description="long_description.md",
long_description_content_type="text/markdown",
use_scm_version={
"version_scheme": version_scheme,
"local_scheme": "no-local-version",
"write_to": "pyrx/_version.py",
}
)