From 88e6c12b5953344d7d9255a92401bfad6763dfa2 Mon Sep 17 00:00:00 2001 From: Mark Kurtz Date: Thu, 25 Jul 2024 09:49:45 -0400 Subject: [PATCH] Adapt build system for dynamic versioning and package naming based on build settings (#19) ## Summary Enhances the build system by introducing dynamic versioning and package naming based on the build type and build number, which are loaded from env variables. ## Details - Dynamic Versioning: The build version is now determined dynamically based on the environment variables GUIDELLM_BUILD_TYPE and GUIDELLM_BUILD_NUMBER. This allows for flexible versioning strategies such as dev, nightly, and release builds. - Package Naming: The package name is adapted according to the build type, supporting different names for dev, nightly, and release versions. - Script Addition: Added utils/inject_build_props.py to automate the injection of build properties into the pyproject.toml file. - Dependencies: Updated dependencies in pyproject.toml to enable pre script builds. - CI/CD Integration: Updated tox.ini to integrate the new build script and ensure all checks are aligned with the updated build process. - Code Cleanup: Removed unused dependencies from .pre-commit-config.yaml.. ## Test Plan - Manually tested the build process for dev, nightly, and release builds to ensure proper versioning and naming. --- .pre-commit-config.yaml | 11 +----- pyproject.toml | 5 ++- src/guidellm/__init__.py | 6 ++- src/guidellm/py.typed | 0 tox.ini | 9 ++++- utils/inject_build_props.py | 78 +++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 14 deletions(-) delete mode 100644 src/guidellm/py.typed create mode 100644 utils/inject_build_props.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ec7b37e..3dc52df 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,14 +8,6 @@ repos: rev: v0.5.2 hooks: - id: ruff -- repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.4.2 - hooks: - - id: black -- repo: https://github.com/pycqa/isort - rev: 5.13.2 - hooks: - - id: isort - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.10.1 hooks: @@ -39,6 +31,7 @@ repos: # types types-click, - types-requests, types-PyYAML, + types-requests, + types-toml, ] diff --git a/pyproject.toml b/pyproject.toml index fef87e7..c60ff77 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,8 +54,9 @@ dev = [ # type-checking "types-click~=7.1.8", - "types-requests~=2.32.0", "types-PyYAML~=6.0.1", + "types-requests~=2.32.0", + "types-toml", ] @@ -85,7 +86,7 @@ show_error_codes = true namespace_packages = true exclude = ["venv", ".tox"] -# Silint "type import errors" as our 3rd-party libs does not have types +# Silence "type import errors" as our 3rd-party libs does not have types # Check: https://mypy.readthedocs.io/en/latest/config_file.html#import-discovery follow_imports = 'silent' diff --git a/src/guidellm/__init__.py b/src/guidellm/__init__.py index 65b8851..6e90897 100644 --- a/src/guidellm/__init__.py +++ b/src/guidellm/__init__.py @@ -5,4 +5,8 @@ from .logger import LoggerConfig, configure_logger, logger -__all__ = ["logger", "configure_logger", "LoggerConfig"] +__all__ = [ + "logger", + "configure_logger", + "LoggerConfig", +] diff --git a/src/guidellm/py.typed b/src/guidellm/py.typed deleted file mode 100644 index e69de29..0000000 diff --git a/tox.ini b/tox.ini index 0c36e27..aff2adf 100644 --- a/tox.ini +++ b/tox.ini @@ -40,7 +40,7 @@ description = Run all quality checks deps = .[dev] commands = - ruff check src tests + ruff check src tests utils [testenv:style] @@ -48,7 +48,7 @@ description = Run style checks and fixes deps = .[dev] commands = - ruff format src tests + ruff format src tests utils [testenv:types] @@ -65,7 +65,10 @@ deps = build setuptools wheel + loguru + toml commands = + python utils/inject_build_props.py python -m build @@ -82,3 +85,5 @@ commands = rm -rf .mypy_cache rm -rf .pytest_cache rm -rf .tox + rm -rf .ruff_cache + rm -rf .coverage diff --git a/utils/inject_build_props.py b/utils/inject_build_props.py new file mode 100644 index 0000000..9a25ba7 --- /dev/null +++ b/utils/inject_build_props.py @@ -0,0 +1,78 @@ +from loguru import logger +import toml +import os +from datetime import datetime +import re + + +def get_build_type(): + return os.getenv("GUIDELLM_BUILD_TYPE", "dev") + + +def get_build_number(): + return os.getenv("GUIDELLM_BUILD_NUMBER", "0") + + +def construct_project_name_and_version(build_type, build_number, current_version): + if not re.match(r"^\d+\.\d+\.\d+$", current_version): + raise ValueError( + f"Version '{current_version}' does not match the " + f"semantic versioning pattern '#.#.#'" + ) + + if build_type == "dev": + project_name = "guidellm_dev" + version = f"{current_version}.dev{build_number}" + elif build_type == "nightly": + project_name = "guidellm_nightly" + date_str = datetime.now().strftime("%Y%m%d") + version = f"{current_version}.{date_str}" + elif build_type == "release": + project_name = "guidellm" + version = current_version + else: + raise ValueError(f"Unknown build type: {build_type}") + + return project_name, version + + +def update_pyproject_toml(project_name, version): + try: + with open("pyproject.toml", "r") as file: + data = toml.load(file) + + data["project"]["name"] = project_name + data["project"]["version"] = version + + with open("pyproject.toml", "w") as file: + toml.dump(data, file) + + logger.info( + f"Updated project name to: {project_name} and version to: {version}" + ) + except (FileNotFoundError, toml.TomlDecodeError) as e: + logger.error(f"Error reading or writing pyproject.toml: {e}") + raise + + +def main(): + try: + build_type = get_build_type() + build_number = get_build_number() + + with open("pyproject.toml", "r") as file: + pyproject_data = toml.load(file) + + current_version = pyproject_data["project"]["version"] + project_name, version = construct_project_name_and_version( + build_type, build_number, current_version + ) + + if build_type != "release": + update_pyproject_toml(project_name, version) + except Exception as e: + logger.error(f"An error occurred: {e}") + + +if __name__ == "__main__": + main()