File: get_version.py

package info (click to toggle)
mkdocstrings-python-legacy 0.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 536 kB
  • sloc: python: 907; makefile: 29; sh: 17; javascript: 13
file content (26 lines) | stat: -rw-r--r-- 1,030 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Get current project version from Git tags or changelog.

import re
from contextlib import suppress
from pathlib import Path

from pdm.backend.hooks.version import SCMVersion, Version, default_version_formatter, get_version_from_scm

_root = Path(__file__).parent.parent
_changelog = _root / "CHANGELOG.md"
_changelog_version_re = re.compile(r"^## \[(\d+\.\d+\.\d+)\].*$")
_default_scm_version = SCMVersion(Version("0.0.0"), None, False, None, None)  # noqa: FBT003


def get_version() -> str:
    scm_version = get_version_from_scm(_root) or _default_scm_version
    if scm_version.version <= Version("0.1"):  # Missing Git tags?
        with suppress(OSError, StopIteration):  # noqa: SIM117
            with _changelog.open("r", encoding="utf8") as file:
                match = next(filter(None, map(_changelog_version_re.match, file)))
                scm_version = scm_version._replace(version=Version(match.group(1)))
    return default_version_formatter(scm_version)


if __name__ == "__main__":
    print(get_version())