Add version metadata and --version command

This commit is contained in:
MarkRx
2024-12-09 09:27:27 -07:00
parent 39a461b3b2
commit 75a120952c
4 changed files with 29 additions and 3 deletions

23
pr_agent/version.py Normal file
View File

@ -0,0 +1,23 @@
import os
import sys
from importlib.metadata import version, PackageNotFoundError
from pr_agent.log import get_logger
def get_version() -> str:
# First check pyproject.toml if running directly out of repository
if os.path.exists("pyproject.toml"):
if sys.version_info >= (3, 11):
import tomllib
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
return data["project"]["version"]
else:
get_logger().warn("Unable to determine local version from pyproject.toml")
# Otherwise get the installed pip package version
try:
return version('pr-agent')
except PackageNotFoundError:
get_logger().error("Unable to find package named 'pr-agent'")
return "unknown"