82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
""" Automatic version number generator
|
|
Automatically generates a version number from information provided by git
|
|
Copyright (c) 2019, 2020, 2021, 2022 xPablo.cz
|
|
"""
|
|
import subprocess
|
|
import dataclasses
|
|
from typing import Optional
|
|
|
|
__author__ = "Pavel Brychta"
|
|
__copyright__ = "Copyright (c) 2019-2022, Pavel Brychta"
|
|
__credits__ = ["Pavel Brychta","Jan Breuer"]
|
|
__license__ = "Private"
|
|
__version__ = "2.0.0"
|
|
__maintainer__ = "Pavel Brychta"
|
|
__email__ = "Pablo@xPablo.cz"
|
|
__status__ = "Stable"
|
|
|
|
__all__ = ['version_info']
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Version:
|
|
major: int = 0
|
|
minor: int = 0
|
|
patch: int = 0
|
|
user: int = 0
|
|
dirty: bool = False
|
|
branch: Optional[str] = None
|
|
variant: Optional[str] = None
|
|
|
|
def __str__(self):
|
|
dirty = "m" if self.dirty else ""
|
|
branch = ""
|
|
if self.branch is not None:
|
|
branch = f'({self.branch})'
|
|
variant = ''
|
|
if self.variant is not None:
|
|
variant = f'-{self.variant}'
|
|
if self.user == 0:
|
|
return f"{self.major}.{self.minor}.{self.patch}{dirty}{variant}{branch}"
|
|
else:
|
|
return f"{self.major}.{self.minor}.{self.patch}.{self.user}{dirty}{variant}{branch}"
|
|
|
|
|
|
def version_info():
|
|
version = Version()
|
|
|
|
try:
|
|
branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip()
|
|
if branch not in ("master", "main", "HEAD"):
|
|
version.branch = branch
|
|
|
|
info = subprocess.check_output(["git", "describe", "--tags", "--dirty", "--long", "--always"]).decode().strip().split('-')
|
|
if info[-1] == "dirty":
|
|
version.dirty = True
|
|
info.pop()
|
|
|
|
if len(info) >= 3:
|
|
# drop abbrev
|
|
info.pop()
|
|
version.user = int(info.pop())
|
|
text = info.pop(0)
|
|
if text.startswith('v') or text.startswith('V'):
|
|
text = text[1:]
|
|
parts = text.split('.')
|
|
version.major = int(parts[0])
|
|
if len(parts) > 1:
|
|
version.minor = int(parts[1])
|
|
if len(parts) > 2:
|
|
version.patch = int(parts[2])
|
|
|
|
if info:
|
|
# there was some additional text in the tag, store it in the variant
|
|
version.variant = '-'.join(info)
|
|
else:
|
|
# no tag so far
|
|
version.user = int(subprocess.check_output(["git", "rev-list", "--all", "--count"]).decode().strip())
|
|
except Exception as e:
|
|
version.major = -1
|
|
|
|
return version
|