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 10:21:08 +01:00
parent 98dfc2af4a
commit adf811437d
2 changed files with 35 additions and 6 deletions

View File

@ -2,6 +2,7 @@
import sys import sys
import os import os
import json import json
import tomllib
from pathlib import Path from pathlib import Path
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
@ -22,7 +23,23 @@ from app.routers import (
) )
from app.database import Base, engine, SessionLocal, load_sample_data from app.database import Base, engine, SessionLocal, load_sample_data
app = FastAPI()
# Utility function to fetch metadata from pyproject.toml
def get_project_metadata():
with open("pyproject.toml", "rb") as f:
pyproject = tomllib.load(f)
name = pyproject["project"]["name"]
version = pyproject["project"]["version"]
return name, version
# Get project metadata from pyproject.toml
project_name, project_version = get_project_metadata()
app = FastAPI(
title=project_name, # Syncs with project `name`
description="Backend for next-gen sample management system",
version=project_version, # Syncs with project `version`
)
# Determine environment and configuration file path # Determine environment and configuration file path
environment = os.getenv("ENVIRONMENT", "dev") environment = os.getenv("ENVIRONMENT", "dev")

View File

@ -1,5 +1,18 @@
#!/bin/bash #!/bin/bash
VERSION=0.1.0
# Extract values from pyproject.toml
PYPROJECT_FILE="$(dirname "$0")/../pyproject.toml"
VERSION=$(grep -Po '(?<=version = ")[^"]*' "$PYPROJECT_FILE")
NAME=$(grep -Po '(?<=name = ")[^"]*' "$PYPROJECT_FILE")
if [[ -z "$VERSION" || -z "$NAME" ]]; then
echo "Error: Could not determine version or name from pyproject.toml"
exit 1
fi
echo "Using project name: $NAME"
echo "Using version: $VERSION"
# Navigate to project root # Navigate to project root
cd "$(dirname "$0")/backend" || exit cd "$(dirname "$0")/backend" || exit
@ -17,12 +30,12 @@ if [[ ! -f openapi-generator-cli.jar ]]; then
wget "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${OPENAPI_VERSION}/openapi-generator-cli-${OPENAPI_VERSION}.jar" -O openapi-generator-cli.jar wget "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${OPENAPI_VERSION}/openapi-generator-cli-${OPENAPI_VERSION}.jar" -O openapi-generator-cli.jar
fi fi
# Run OpenAPI generator # Run OpenAPI generator with synced name and version
java -jar openapi-generator-cli.jar generate \ java -jar openapi-generator-cli.jar generate \
-i "$(pwd)/openapi.json" \ -i "$(pwd)/openapi.json" \
-o python-client/ \ -o python-client/ \
-g python \ -g python \
--additional-properties=packageName=aareDBclient,projectName=aareDB,packageVersion=${VERSION} --additional-properties=packageName="${NAME}client",projectName="${NAME}",packageVersion="${VERSION}"
# Check if the generator succeeded # Check if the generator succeeded
if [[ ! -d python-client ]]; then if [[ ! -d python-client ]]; then
@ -39,4 +52,3 @@ python -m build
# Skip uploading the package. This will happen in the release stage. # Skip uploading the package. This will happen in the release stage.
echo "Python client generated and package built successfully." echo "Python client generated and package built successfully."
echo "git.psi.ch pushed"