########################################################################### # # Copyright (c) 2020-2024 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file version.py # # @author (last) Zoltan Miskolci # @date (last) 22-Jun-2026 # @author (original) Peter Lucia # @date (original) 22-Jun-2021 # ############################################################################ # Module imports import subprocess import os.path from typing import Tuple VERSION_FILE_LOCATION = './VERSION' def get_branch(): """ Gets the current branch name in the current git repository @return: The current branch name, None if it can't be determined """ try: # Change the folder to where this file is for git versioning and then back to the original position curdir = os.path.abspath(os.curdir) os.chdir(os.path.dirname(__file__)) res = subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True).decode("utf-8").strip() os.chdir(curdir) return res except subprocess.CalledProcessError: return None def get_last_commit(): """ Gets the latest commit in the current git repository @return: (str) The latest commit in the current git repository, None if it can't be determined """ try: # Change the folder to where this file is for git versioning and then back to the original position curdir = os.path.abspath(os.curdir) os.chdir(os.path.dirname(__file__)) res = subprocess.check_output("git rev-parse --short=7 HEAD", shell=True).decode("utf-8").strip() os.chdir(curdir) return res except subprocess.CalledProcessError: return None def check_if_git_repo(): """ Checks if we're in a git repo or not to know if we can get the git branch and commit @return: True if in a git repo, False otherwise """ curdir = os.path.abspath(os.curdir) os.chdir(os.path.dirname(__file__)) res = subprocess.call(["git", "branch"], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL) == 0 os.chdir(curdir) return res def read_version_file() -> Tuple[str, str, str, str]: """ Loads the information from the VERSION file. @return: Version information """ version = None build_num = None branch = None commit = None with open(file = os.path.abspath(VERSION_FILE_LOCATION)) as f: content = f.read().split('-') version = content[0] build_num = content[1] branch = content[2] if branch == 'local' and check_if_git_repo(): branch = get_branch() commit = get_last_commit() return f'{version}.{build_num}.{branch}.{commit}' DEV_VERSION = read_version_file() if __name__ == '__main__': print(DEV_VERSION)