|
| 1 | +import os |
| 2 | +import re |
| 3 | +from pathlib import Path |
| 4 | +from typing import Optional, Union |
| 5 | + |
| 6 | +from packaging.version import Version |
| 7 | +from setuptools import setup |
| 8 | +from setuptools_git_versioning import count_since, get_branch, get_sha, get_tags |
| 9 | + |
| 10 | +LAST_RELEASE_VERSION = Version("0.0.0") |
| 11 | +TAG_VERSION_PATTERN = re.compile(r"^v(\d+\.\d+\.\d+)$") |
| 12 | + |
| 13 | + |
| 14 | +def get_last_version_diff() -> tuple[Version, Optional[str], Optional[int]]: |
| 15 | + """ |
| 16 | + Get the last version, last tag, and the number of commits since the last tag. |
| 17 | + If no tags are found, return the last release version and None for the tag/commits. |
| 18 | +
|
| 19 | + :returns: A tuple containing the last version, last tag, and number of commits since |
| 20 | + the last tag. |
| 21 | + """ |
| 22 | + tagged_versions = [ |
| 23 | + (Version(match.group(1)), tag) |
| 24 | + for tag in get_tags(root=Path(__file__).parent) |
| 25 | + if (match := TAG_VERSION_PATTERN.match(tag)) |
| 26 | + ] |
| 27 | + tagged_versions.sort(key=lambda tv: tv[0]) |
| 28 | + last_version, last_tag = ( |
| 29 | + tagged_versions[-1] if tagged_versions else (LAST_RELEASE_VERSION, None) |
| 30 | + ) |
| 31 | + commits_since_last = ( |
| 32 | + count_since(last_tag + "^{commit}", root=Path(__file__).parent) |
| 33 | + if last_tag |
| 34 | + else None |
| 35 | + ) |
| 36 | + |
| 37 | + return last_version, last_tag, commits_since_last |
| 38 | + |
| 39 | + |
| 40 | +def get_next_version( |
| 41 | + build_type: str, build_iteration: Optional[Union[str, int]] |
| 42 | +) -> tuple[Version, Optional[str], int]: |
| 43 | + """ |
| 44 | + Get the next version based on the build type and iteration. |
| 45 | + - build_type == release: take the last version and add a post if build iteration |
| 46 | + - build_type == candidate: increment to next minor, add 'rc' with build iteration |
| 47 | + - build_type == nightly: increment to next minor, add 'a' with build iteration |
| 48 | + - build_type == alpha: increment to next minor, add 'a' with build iteration |
| 49 | + - build_type == dev: increment to next minor, add 'dev' with build iteration |
| 50 | +
|
| 51 | + :param build_type: The type of build (release, candidate, nightly, alpha, dev). |
| 52 | + :param build_iteration: The build iteration number. If None, defaults to the number |
| 53 | + of commits since the last tag or 0 if no commits since the last tag. |
| 54 | + :returns: A tuple containing the next version, the last tag the version is based |
| 55 | + off of (if any), and the final build iteration used. |
| 56 | + """ |
| 57 | + version, tag, commits_since_last = get_last_version_diff() |
| 58 | + |
| 59 | + if not build_iteration and build_iteration != 0: |
| 60 | + build_iteration = commits_since_last or 0 |
| 61 | + elif isinstance(build_iteration, str): |
| 62 | + build_iteration = int(build_iteration) |
| 63 | + |
| 64 | + if build_type == "release": |
| 65 | + if commits_since_last: |
| 66 | + # add post since we have commits since last tag |
| 67 | + version = Version(f"{version.base_version}.post{build_iteration}") |
| 68 | + return version, tag, build_iteration |
| 69 | + |
| 70 | + # not in release pathway, so need to increment to target next release version |
| 71 | + version = Version(f"{version.major}.{version.minor + 1}.0") |
| 72 | + |
| 73 | + if build_type == "candidate": |
| 74 | + # add 'rc' since we are in candidate pathway |
| 75 | + version = Version(f"{version}.rc{build_iteration}") |
| 76 | + elif build_type in ["nightly", "alpha"]: |
| 77 | + # add 'a' since we are in nightly or alpha pathway |
| 78 | + version = Version(f"{version}.a{build_iteration}") |
| 79 | + else: |
| 80 | + # assume 'dev' if not in any of the above pathways |
| 81 | + version = Version(f"{version}.dev{build_iteration}") |
| 82 | + |
| 83 | + return version, tag, build_iteration |
| 84 | + |
| 85 | + |
| 86 | +def write_version_files() -> tuple[Path, Path]: |
| 87 | + """ |
| 88 | + Write the version information to version.txt and version.py files. |
| 89 | + version.txt contains the version string. |
| 90 | + version.py contains the version plus additional metadata. |
| 91 | +
|
| 92 | + :returns: A tuple containing the paths to the version.txt and version.py files. |
| 93 | + """ |
| 94 | + build_type = os.getenv("GUIDELLM_BUILD_TYPE", "dev").lower() |
| 95 | + version, tag, build_iteration = get_next_version( |
| 96 | + build_type=build_type, |
| 97 | + build_iteration=os.getenv("GUIDELLM_BUILD_ITERATION"), |
| 98 | + ) |
| 99 | + module_path = Path(__file__).parent / "src" / "guidellm" |
| 100 | + version_txt_path = module_path / "version.txt" |
| 101 | + version_py_path = module_path / "version.py" |
| 102 | + |
| 103 | + with version_txt_path.open("w") as file: |
| 104 | + file.write(str(version)) |
| 105 | + |
| 106 | + with version_py_path.open("w") as file: |
| 107 | + file.writelines( |
| 108 | + [ |
| 109 | + f'version = "{version}"\n', |
| 110 | + f'build_type = "{build_type}"\n', |
| 111 | + f'build_iteration = "{build_iteration}"\n', |
| 112 | + f'git_commit = "{get_sha()}"\n', |
| 113 | + f'git_branch = "{get_branch()}"\n', |
| 114 | + f'git_last_tag = "{tag}"\n', |
| 115 | + ] |
| 116 | + ) |
| 117 | + |
| 118 | + return version_txt_path, version_py_path |
| 119 | + |
| 120 | + |
| 121 | +setup( |
| 122 | + setuptools_git_versioning={ |
| 123 | + "enabled": True, |
| 124 | + "version_file": str(write_version_files()[0]), |
| 125 | + } |
| 126 | +) |
0 commit comments