File size: 7,433 Bytes
932bc69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import os
import re
from pathlib import Path
try:
import tomllib
except ImportError:
import tomli as tomllib
from packaging.version import Version
from setuptools import setup
from setuptools_git_versioning import count_since, get_branch, get_sha, get_tags
REPO_ROOT = Path(__file__).parent
LAST_RELEASE_VERSION = Version("0.7.0")
TAG_VERSION_PATTERN = re.compile(r"^v(\d+\.\d+\.\d+)$")
def get_last_version_diff() -> tuple[Version, str | None, int | None]:
"""
Get the last version, last tag, and the number of commits since the last tag.
If no tags are found, return the last release version and None for the tag/commits.
:returns: A tuple containing the last version, last tag, and number of commits since
the last tag.
"""
tagged_versions = [
(Version(match.group(1)), tag)
for tag in get_tags(root=REPO_ROOT)
if (match := TAG_VERSION_PATTERN.match(tag))
]
tagged_versions.sort(key=lambda tv: tv[0])
last_version, last_tag = (
tagged_versions[-1] if tagged_versions else (LAST_RELEASE_VERSION, None)
)
commits_since_last = (
count_since(last_tag + "^{commit}", root=REPO_ROOT) if last_tag else None
)
return last_version, last_tag, commits_since_last
def get_next_version(
build_type: str, build_iteration: str | int | None
) -> tuple[Version, str | None, int]:
"""
Get the next version based on the build type and iteration.
- build_type == release: take the last version and add a post if build iteration
- build_type == nightly: increment to next minor, add 'a' with build iteration
- build_type == alpha: increment to next minor, add 'a' with build iteration
- build_type == dev: increment to next minor, add 'dev' with build iteration
:param build_type: The type of build (release, candidate, nightly, alpha, dev).
:param build_iteration: The build iteration number. If None, defaults to the number
of commits since the last tag or 0 if no commits since the last tag.
:returns: A tuple containing the next version, the last tag the version is based
off of (if any), and the final build iteration used.
"""
version, tag, commits_since_last = get_last_version_diff()
if not build_iteration and build_iteration != 0:
build_iteration = commits_since_last or 0
elif isinstance(build_iteration, str):
build_iteration = int(build_iteration)
# in case tag is behind LAST_RELEASE_VERSION
version = max(version, LAST_RELEASE_VERSION)
if build_type == "release":
# if not tag:
# raise ValueError("RELEASE build requires a vX.Y.Z tag")
# if commits_since_last:
# raise ValueError(
# f"HEAD is {commits_since_last} commit(s) ahead"
# )
return version, tag, 0
# not in release pathway, so need to increment minor to target next release version
version = Version(f"{version.major}.{version.minor + 1}.0")
if build_type in ["nightly", "alpha"]:
# add 'a' since we are in nightly or alpha pathway
version = Version(f"{version}.a{build_iteration}")
else:
# assume 'dev' if not in any of the above pathways
version = Version(f"{version}.dev{build_iteration}")
return version, tag, build_iteration
def read_existing_version(version_py: Path) -> tuple[Version, str | None, int]:
if version_py.exists():
text = version_py.read_text()
match_version = re.search(r'^version\s*=\s*["\']([^"\']+)["\']', text, re.M)
match_tag = re.search(r'^git_last_tag\s*=\s*["\']([^"\']*)["\']', text, re.M)
match_iteration = re.search(
r'^build_iteration\s*=\s*["\']([^"\']+)["\']', text, re.M
)
version = Version(match_version.group(1)) if match_version else None
tag = match_tag.group(1) if match_tag and match_tag.group(1) else None
build_iteration = int(match_iteration.group(1)) if match_iteration else 0
return version, tag, build_iteration
def building_from_sdist() -> bool:
# sdist extracts as speculators-<version>/setup.py
return REPO_ROOT.name.startswith("speculators-")
def write_version_files() -> tuple[Path, Path]:
"""
Write the version information to version.txt and version.py files.
version.txt contains the version string.
version.py contains the version plus additional metadata.
:returns: A tuple containing the paths to the version.txt and version.py files.
"""
build_type = os.getenv("SPECULATORS_BUILD_TYPE", "dev").lower()
module_path = REPO_ROOT / "src" / "speculators"
version_txt_path = module_path / "version.txt"
version_py_path = module_path / "version.py"
if building_from_sdist() and version_py_path.exists():
version, tag, build_iteration = read_existing_version(version_py_path)
else:
version, tag, build_iteration = get_next_version(
build_type=build_type,
build_iteration=os.getenv("SPECULATORS_BUILD_ITERATION"),
)
git_commit = get_sha(root=REPO_ROOT) if (not building_from_sdist()) else ""
git_branch = get_branch(root=REPO_ROOT) if (not building_from_sdist()) else ""
with version_txt_path.open("w") as file:
file.write(str(version))
with version_py_path.open("w") as file:
file.writelines(
[
f'version = "{version}"\n',
f'build_type = "{build_type}"\n',
f'build_iteration = "{build_iteration}"\n',
f'git_commit = "{git_commit}"\n',
f'git_branch = "{git_branch}"\n',
f'git_last_tag = "{tag or ""}"\n',
]
)
return version_txt_path, version_py_path
def get_hs_connectors_requirement() -> str:
build_type = os.getenv("SPECULATORS_BUILD_TYPE", "dev").lower()
version_py_path = REPO_ROOT / "src" / "speculators" / "version.py"
if building_from_sdist() and version_py_path.exists():
version, tag, build_iteration = read_existing_version(version_py_path)
else:
version, tag, build_iteration = get_next_version(
build_type=build_type,
build_iteration=os.getenv("SPECULATORS_BUILD_ITERATION"),
)
if build_type == "dev":
# Source install: path dep for pip; uv workspace overrides anyway
local = (REPO_ROOT / "hs_connectors").resolve()
return f"hs-connectors @ file://{local.as_posix()}"
elif build_type == "release":
# Install release version: hs_connectors has the same version as speculators
return f"hs-connectors=={version}"
else:
# Install nightly version
return f"hs-connectors>{LAST_RELEASE_VERSION},<={version}"
def get_base_dependencies() -> list[str]:
"""
Read the static base dependency list from pyproject.toml's
[tool.speculators.dependencies].base table, so it stays hand-edited TOML
rather than a Python literal duplicated here.
"""
with (REPO_ROOT / "pyproject.toml").open("rb") as file:
data = tomllib.load(file)
return data["tool"]["speculators"]["dependencies"]["base"]
setup(
# set hs_connectors version to install
install_requires=get_base_dependencies() + [get_hs_connectors_requirement()],
setuptools_git_versioning={
"enabled": True,
"version_file": str(write_version_files()[0]),
},
)
|