Coverage for src/package/version.py: 87.88%
33 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-08 13:49 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-08 13:49 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from os import environ
5from sys import version_info
7# Components
8from ..system.platform import Platform
9from .bundle import Bundle
11# Version class
12class Version:
14 # Getter
15 @staticmethod
16 def get() -> str:
18 # Fake test version
19 if Bundle.ENV_DEBUG_VERSION_FAKE in environ:
20 return environ[Bundle.ENV_DEBUG_VERSION_FAKE]
22 # Acquire version from metadata
23 try:
24 from importlib import metadata # pylint: disable=import-outside-toplevel
25 return metadata.version(Bundle.PACKAGE)
26 except Exception: # pylint: disable=broad-exception-caught # pragma: no cover
27 pass
29 # Acquire version from resources
30 try: # pragma: no cover
31 import importlib.metadata # pylint: disable=import-outside-toplevel
32 name = __name__.split('.', maxsplit=1)[0]
33 return importlib.metadata.version(name)
35 # Acquire version from package resources
36 except Exception: # pylint: disable=broad-exception-caught # pragma: no cover
37 try:
38 import importlib.metadata # pylint: disable=import-outside-toplevel
39 return importlib.metadata.version(Bundle.PACKAGE)
41 # Default fallback
42 except Exception: # pylint: disable=broad-exception-caught # pragma: no cover
43 return '0.0.0'
45 # Path
46 @staticmethod
47 def path() -> str:
49 # Acquire path
50 path = __file__
52 # Strip package path
53 index = path.rfind(Platform.PATH_SEPARATOR)
54 index = path.rfind(Platform.PATH_SEPARATOR, 0, index)
55 path = path[0:index]
57 # Result
58 return path
60 # Python
61 @staticmethod
62 def python() -> str:
64 # Acquire Python version
65 version = f'{version_info.major}.{version_info.minor}'
67 # Result
68 return version
70 # Revision
71 @staticmethod
72 def revision() -> str:
74 # Force package revision
75 if Bundle.ENV_DEBUG_REVISION_SHA in environ:
76 return environ[Bundle.ENV_DEBUG_REVISION_SHA]
78 # Acquire package version
79 version: str = Version.get()
81 # Handle development version
82 if '+g' in version:
83 version = version.replace('.dev', '-')
84 version = version.replace('+g', '-g')
85 return version.rsplit('.d', 1)[0]
87 # Result
88 return version