diff --git a/deepaas/__init__.py b/deepaas/__init__.py index ac074f0e..37e30e5c 100644 --- a/deepaas/__init__.py +++ b/deepaas/__init__.py @@ -14,4 +14,24 @@ # License for the specific language governing permissions and limitations # under the License. +from contextlib import suppress +import importlib.metadata +from pathlib import Path + __version__ = "2.4.0" + + +def extract_version() -> str: + """Returns either the version of the package installed.""" + with suppress(FileNotFoundError, StopIteration): + with open((root_dir := 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}-dev (at {root_dir})" + return importlib.metadata.version( + __package__ or __name__.split(".", maxsplit=1)[0] + ) diff --git a/deepaas/api/__init__.py b/deepaas/api/__init__.py index 3c45713e..9a12e939 100644 --- a/deepaas/api/__init__.py +++ b/deepaas/api/__init__.py @@ -100,7 +100,7 @@ async def get_app( "url": "https://deepaas.readthedocs.org/", }, basePath=base_path, - version=deepaas.__version__, + version=deepaas.extract_version(), url="/swagger.json", swagger_path=doc if enable_doc else None, prefix=prefix, diff --git a/deepaas/cmd/cli.py b/deepaas/cmd/cli.py index e59971eb..e15cfbc7 100644 --- a/deepaas/cmd/cli.py +++ b/deepaas/cmd/cli.py @@ -279,7 +279,7 @@ def main(): log.register_options(CONF) log.set_defaults(default_log_levels=log.get_default_log_levels()) - CONF(sys.argv[1:], project="deepaas", version=deepaas.__version__) + CONF(sys.argv[1:], project="deepaas", version=deepaas.extract_version()) log.setup(CONF, "deepaas-cli") diff --git a/deepaas/cmd/run.py b/deepaas/cmd/run.py index 8f953d27..e1a9d0d3 100644 --- a/deepaas/cmd/run.py +++ b/deepaas/cmd/run.py @@ -100,7 +100,7 @@ def main(): print(INTRO) print(BANNER.format(docs, spec, v2)) - log.info("Starting DEEPaaS version %s", deepaas.__version__) + log.info("Starting DEEPaaS version %s", deepaas.extract_version()) app = api.get_app( enable_doc=CONF.doc_endpoint, diff --git a/deepaas/config.py b/deepaas/config.py index 6d85997b..20bea2f2 100644 --- a/deepaas/config.py +++ b/deepaas/config.py @@ -116,7 +116,7 @@ def parse_args(argv, default_config_files=None): cfg.CONF( argv[1:], project="deepaas", - version=deepaas.__version__, + version=deepaas.extract_version(), default_config_files=default_config_files, ) diff --git a/doc/source/conf.py b/doc/source/conf.py index a4dae53a..cedfbb68 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -72,7 +72,7 @@ # built documents. # # The full version, including alpha/beta/rc tags. -release = deepaas.__version__ +release = deepaas.extract_version() # The short X.Y version. version = deepaas.__version__