Prechod na unifikovane ziskavani verze z gitu

This commit is contained in:
Pavel Brychta 2023-03-29 08:24:05 +02:00
parent 40bf89adb6
commit 81abcd3d23
4 changed files with 88 additions and 74 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

View File

@ -2,14 +2,15 @@
# -*- coding: utf-8 -*-
import gzip, shutil, os, errno, json, subprocess, sys, getopt
from distutils import dir_util
# from distutils import dir_util
from string import Template
import gitrev
__author__ = "Pavel Brychta"
__copyright__ = "Copyright (c) 2019-2020, Pavel Brychta"
__copyright__ = "Copyright (c) 2019-2023, Pavel Brychta"
__credits__ = ["Pavel Brychta"]
__license__ = "Private"
__version__ = "1.3.0"
__version__ = "1.4.0"
__maintainer__ = "Pavel Brychta"
__email__ = "Pablo@xPablo.cz"
__status__ = "Beta"
@ -182,41 +183,8 @@ def copycompresstree(src, dst, compress, dirbase):
# Ziskani verze repozitare z gitu
def getgitversion():
version = subprocess.check_output(["python3","tools/generate/gitrev.py"])
return version.decode("utf-8")
"""
isgit = True
after = b"0"
mod = b""
try:
tag = subprocess.check_output(["git", "describe", "--tags", "--always"]).strip()
except:
tag = b"0.0.0"
isgit = False
if isgit:
if tag.find(b".") < 0:
tag = b"0.0.0"
if tag == b"0.0.0":
after = subprocess.check_output(["git", "rev-list", "--all", "--count"]).strip()
else:
if tag.find(b"-") > 0:
taginfo = tag.split(b'-')
tag = taginfo[0]
after = taginfo[1]
else:
after = subprocess.check_output(["git", "rev-list", tag + "..HEAD", "--count"]).strip()
mod = subprocess.check_output(["git", "diff-index", "HEAD"]).strip()
if after == b"0":
revision = tag
else:
revision = tag + b'.' + after
if mod != b"":
revision = revision + b"m"
else:
revision = b"0.0.0.x"
return revision.decode("utf-8")
"""
version = gitrev.version_info()
return str(version)
def main(argv):
global flist

View File

@ -1,36 +0,0 @@
#!/usr/bin/env python3
""" Automaticky generator cisla verze
Automaticky generuje cislo verze z informaci, poskytnutych gitem
Copyright (c) 2019, 2020, 2021 xPablo.cz
Autor: Pavel Brychta
"""
import subprocess
dirty = False
branch = "HEAD"
build = "0"
try:
version_parts = subprocess.check_output(["git", "describe", "--tags", "--dirty", "--long", "--always"]).decode().strip().split('-')
branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip()
if version_parts[-1] == "dirty":
dirty = True
version_parts.pop()
if len(version_parts) >= 3:
# zahodime abbrev
version_parts.pop();
build = version_parts.pop()
version = '-'.join(version_parts)
else:
# neni zatim zadny tag
version = '0.0.0'
build = subprocess.check_output(["git", "rev-list", "--all", "--count"]).decode().strip()
except:
version = 'x.x.x'
build = "" if build == "0" else "." + build
dirty_text = "" if not dirty else "m"
branch_text = "" if branch in ("master", "HEAD") else "[{}]".format(branch)
print("{}{}{}{}".format(version, build, dirty_text, branch_text))

81
gitrev/__init__.py Normal file
View File

@ -0,0 +1,81 @@
""" 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