From 22035b89e0b80c59a552d804328c83b8c8c5edf0 Mon Sep 17 00:00:00 2001 From: Layla Date: Fri, 30 Jun 2023 18:46:13 -0400 Subject: [PATCH] Rework utils --- semver/utils.py | 77 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/semver/utils.py b/semver/utils.py index 88f813b..d0d325a 100644 --- a/semver/utils.py +++ b/semver/utils.py @@ -1,41 +1,72 @@ import subprocess -from semver.logger import logging, logger, console_logger +from typing import Union, List try: - from configparser import ConfigParser -except ImportError: - # Python < 3 - from ConfigParser import ConfigParser - -try: - from subprocess import DEVNULL # py3k + from subprocess import DEVNULL # py3k except ImportError: import os - DEVNULL = open(os.devnull, 'wb') -def get_tag_version(): - config = ConfigParser() - config.read('./.bumpversion.cfg') - tag_expression = config.get('bumpversion','tag_name').replace('{new_version}','[0-9]*.[0-9]*.[0-9]*') + DEVNULL = open(os.devnull, "wb") + +import toml + +from semver.logger import logging, logger, console_logger + + +def get_tag_version() -> str: + """ + Get the latest tagged version from git tags + :return: The latest tagged version + """ + config: dict = toml.load("./.bumpversion.cfg") + + tag_expression: str = config["bumpversion"]["tag_name"].replace( + "{new_version}", "[0-9]*.[0-9]*.[0-9]*" + ) logger.debug("Tag expression: " + str(tag_expression)) - # Default version is `0.0.0` or what is found in + # Default version is `0.0.0` or what is found in version = get_file_version(config) - # If a version is found in tags, use that the lastest tagged version - tagged_versions = subprocess.Popen(['git','tag','--sort=v:refname', '-l',tag_expression], - stdout=subprocess.PIPE, stderr=DEVNULL, cwd=".").stdout.read().decode('utf-8').rstrip().split('\n') + # If a version is found in git tags, use that the latest tagged version + tagged_versions: Union[List[str], None] = None + try: + proc = subprocess.run( + ["git", "tag", "--sort=v:refname", "-l", tag_expression], + capture_output=True, + text=True, + check=True, + ) + tagged_versions = proc.stdout.rstrip().split("\n") + except subprocess.CalledProcessError as e: + raise RuntimeError( + f"Error getting latest tagged git version: {str(e.stderr).rstrip()}" + ) + if len(tagged_versions) > 0 and tagged_versions[-1] != "": version = tagged_versions[-1] logger.debug("Tag Version: " + str(version)) return version -def get_file_version(config): - version = config.get('bumpversion','current_version') - if not version: - config.set('bumpversion', 'current_version', '0.0.0') - version = '0.0.0' - return version +def get_file_version(config: dict) -> str: + """ + :param config: The bumpversion config as a dict + :return: The current version from the config file + """ + bumpversion: Union[str, None] = config.get("bumpversion", None) + version: Union[str, None] = ( + bumpversion.get("current_version", None) if bumpversion else None + ) + + if not bumpversion: + config["bumpversion"] = {} + version = "0.0.0" + + if not version: + config["bumpversion"]["current_version"] = "0.0.0" + version = "0.0.0" + + return version