Sync project metadata with pyproject.toml

Updated scripts and backend to dynamically retrieve project name and version from `pyproject.toml`. This ensures consistent metadata across the OpenAPI client generation and the FastAPI application.
This commit is contained in:
GotthardG
2024-12-17 11:54:32 +01:00
parent 7d6168a197
commit 8cb2154740
3 changed files with 30 additions and 35 deletions

View File

@ -2,6 +2,7 @@
import sys
import os
import json
import tomllib
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
@ -25,24 +26,21 @@ from app.database import Base, engine, SessionLocal, load_sample_data
# Utility function to fetch metadata from pyproject.toml
def get_project_metadata():
from pathlib import Path
import tomllib
# Start from the current script's directory and search for pyproject.toml
script_dir = Path(__file__).resolve().parent
for parent in script_dir.parents:
pyproject_path = parent / "pyproject.toml"
if pyproject_path.exists():
with open(pyproject_path, "rb") as f:
pyproject = tomllib.load(f)
name = pyproject["project"]["name"]
version = pyproject["project"]["version"]
return name, version
# Dynamically resolve the project root folder correctly.
# Assume that `pyproject.toml` is located in the heidi-v2 root folder
root_dir = Path(__file__).resolve().parent.parent.parent
pyproject_path = root_dir / "heidi-v2" / "pyproject.toml"
print(f"Looking for pyproject.toml at: {pyproject_path}") # Debugging output
if not pyproject_path.exists():
raise FileNotFoundError(f"{pyproject_path} not found")
with open(pyproject_path, "rb") as f:
pyproject = tomllib.load(f)
name = pyproject["project"]["name"]
version = pyproject["project"]["version"]
return name, version
# If no pyproject.toml is found, raise FileNotFoundError
raise FileNotFoundError(
f"pyproject.toml not found in any parent directory of {script_dir}"
)
# Get project metadata from pyproject.toml