Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read package version from pyproject.toml if not found in metadata #194

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion aleph_alpha_client/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
import importlib.metadata

__version__ = importlib.metadata.version("aleph-alpha-client")
import re
from pathlib import Path
import logging


MIN_API_VERSION = "1.19.0"


def pyproject_version() -> str:
"""Return the package version specified in pyproject.toml.

Poetry inject the package version from the pyproject.toml file into the package
metadata at build time. This means any user of the package has access to the
version (it is send as User-Agent header in requests).

If this repository is cloned and the package is not installed, the version will
be not available (it is 0.0.0 per default). To also provide a version in these
cases, this function tries to read the version from the pyproject.toml file.

To not break imports in cases where both, the pyproject.toml file and the package
metadata are not available, no error is raised and a default version of 0.0.0
will be returned.
"""
NO_VERSION = "0.0.0"
pyproject_path = Path(__file__).resolve().parent.parent / "pyproject.toml"

if not pyproject_path.is_file():
logging.error("pyproject.toml file not found.")
return NO_VERSION

version_pattern = re.compile(r'^version\s*=\s*["\']([^"\']+)["\']', re.MULTILINE)

with pyproject_path.open("r", encoding="utf-8") as file:
content = file.read()

if (match := version_pattern.search(content)):
return match.group(1)

logging.error("Version not found in pyproject.toml")
return NO_VERSION


__version__ = importlib.metadata.version("aleph-alpha-client")

if __version__ == "0.0.0":
__version__ = pyproject_version()