From 4ae3af0d003f45add4eb46a9e80d136bf6b15db7 Mon Sep 17 00:00:00 2001 From: Jason Sherman Date: Thu, 7 Sep 2023 10:36:06 -0700 Subject: [PATCH] Allow direct reading of pyproject.toml version when package not installed Signed-off-by: Jason Sherman --- aries_cloudagent/version.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/version.py b/aries_cloudagent/version.py index c6432013b7..fd6f0d37b5 100644 --- a/aries_cloudagent/version.py +++ b/aries_cloudagent/version.py @@ -1,6 +1,31 @@ """Library version information.""" -from importlib import metadata +from contextlib import suppress +import importlib.metadata +from pathlib import Path -__version__ = metadata.version("aries-cloudagent") + +def extract_version() -> str: + """Return package version. + + Returns version of the installed package or the one found in nearby pyproject.toml + for cases when package is not installed (ie. local development and testing). + """ + try: + return importlib.metadata.version("aries-cloudagent") + except importlib.metadata.PackageNotFoundError: + with suppress(FileNotFoundError, StopIteration): + with open( + (Path(__file__).parent.parent) / "pyproject.toml", + encoding="utf-8", + ) as pyproject_toml: + version = ( + next(line for line in pyproject_toml if line.startswith("version")) + .split("=")[1] + .strip("'\"\n ") + ) + return f"{version}" + + +__version__ = extract_version() RECORD_TYPE_ACAPY_VERSION = "acapy_version"