From 8341a29a36ca098dad880421e39f8e2fd38ed13a Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 25 Dec 2024 17:33:29 +0100 Subject: [PATCH] Set version from git for development --- src/gisaf/config.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/gisaf/config.py b/src/gisaf/config.py index ab771f2..9e9a465 100644 --- a/src/gisaf/config.py +++ b/src/gisaf/config.py @@ -3,7 +3,7 @@ import logging from pathlib import Path from typing import Any, Type, Tuple from yaml import safe_load -from importlib.metadata import version +from importlib.metadata import version as importlib_version from xdg import BaseDirectory from pydantic import BaseModel @@ -14,6 +14,27 @@ from pydantic_settings import ( YamlConfigSettingsSource, ) + +def get_version(): + version = importlib_version("gisaf-backend") + if version == "0.0.0": + from subprocess import run + + try: + v_git_describe = run( + ["git", "describe", "--always", "--tags", "--dirty"], + capture_output=True, + text=True, + cwd=Path(__file__).parent.parent, + ) + v_git_describe.check_returncode() + version = v_git_describe.stdout.strip() + except Exception: + logger.warn("Version not set in Python package and cannot determine ") + version = "?" + return version + + logger = logging.getLogger(__name__) ENV = environ.get("env", "prod") @@ -349,7 +370,7 @@ class Config(BaseSettings): plot: Plot = Plot() plugins: dict[str, dict[str, Any]] = {} survey: Survey = Survey() - version: str = version("gisaf-backend") + version: str = get_version() weather_station: dict[str, dict[str, Any]] = {} widgets: Widgets = Widgets()