Rework utils
This commit is contained in:
		@ -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
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user