Added git commit and build date env and API

This commit is contained in:
Donkie
2023-09-17 10:51:28 +02:00
parent a1a4ad6b45
commit 1a3efd9ea9
6 changed files with 50 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
import logging
import os
from datetime import datetime
from enum import Enum
from pathlib import Path
from typing import Optional
@@ -245,3 +246,29 @@ def get_version() -> str:
str: The version.
"""
return pkg_resources.get_distribution("spoolman").version
def get_commit_hash() -> Optional[str]:
"""Get the latest commit hash of the package.
Can end with "-dirty" if there are uncommitted changes.
Returns:
Optional[str]: The commit hash.
"""
commit_hash = os.getenv("GIT_COMMIT", "unknown")
if commit_hash == "unknown":
return None
return commit_hash
def get_build_date() -> Optional[datetime]:
"""Get the build date of the package.
Returns:
Optional[datetime.datetime]: The build date.
"""
build_date = os.getenv("BUILD_DATE", "unknown")
if build_date == "unknown":
return None
return datetime.fromisoformat(build_date)