From 6fae3372f2700967339125b077579afd025795a6 Mon Sep 17 00:00:00 2001 From: Derek DeJonghe Date: Mon, 15 Jun 2020 16:01:33 -0400 Subject: [PATCH] enable npm and maven prerelease version syntax --- semver/__init__.py | 19 ++++-------- semver/get_version.py | 68 +++++++++++++++---------------------------- semver/utils.py | 41 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 58 deletions(-) create mode 100644 semver/utils.py diff --git a/semver/__init__.py b/semver/__init__.py index 987763a..554fa35 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -1,7 +1,7 @@ import argparse import re import subprocess -from semver.get_version import get_tag_version +from semver.utils import get_tag_version from semver.logger import logging, logger, console_logger try: @@ -35,6 +35,7 @@ class SemVer(object): self.major_branches = self._setting_to_array('major_branches') self.minor_branches = self._setting_to_array('minor_branches') self.patch_branches = self._setting_to_array('patch_branches') + self.get_branches() def _setting_to_array(self, setting): config = ConfigParser() @@ -44,7 +45,6 @@ class SemVer(object): return list(filter(bool, [v.strip() for v in value.split(',')])) # based on commit message see what branches are involved in the merge - def get_branches(self): p = subprocess.Popen(['git', 'log', '-1'], stdout=subprocess.PIPE, cwd='.') @@ -76,15 +76,15 @@ class SemVer(object): for prefix in self.major_branches: if prefix == merged_prefix: self.version_type = 'major' - return True + return self.version_type for prefix in self.minor_branches: if prefix == merged_prefix: self.version_type = 'minor' - return True + return self.version_type for prefix in self.patch_branches: if prefix == merged_prefix: self.version_type = 'patch' - return True + return self.version_type return False # setup git settings so we can commit and tag @@ -104,15 +104,6 @@ class SemVer(object): config_file = "" with open(".bumpversion.cfg", "r") as file: config_file = file.read() - # Update .bumpconfig - #pattern = re.compile("current_version = [0-9.]*") - #current_config = re.search(pattern, config_file) - #if current_config: - #config_file.replace(current_config.group(0), "current_version = " + get_tag_version()) - #else: - #config_file.replace("[bumpversion]","[bumpversion]\ncurrent_version = " + get_tag_version()) - #with open(".bumpversion.cfg", "w") as file: - #file.write(config_file) # version repo logger.debug("Running bumpversion of type: " + self.version_type) diff --git a/semver/get_version.py b/semver/get_version.py index 38f659c..2b143ef 100644 --- a/semver/get_version.py +++ b/semver/get_version.py @@ -1,44 +1,11 @@ import argparse -from semver.logger import logging, logger, console_logger +import re import subprocess -try: - from configparser import ConfigParser -except ImportError: - # Python < 3 - from ConfigParser import ConfigParser -try: - from subprocess import DEVNULL # py3k -except ImportError: - import os - DEVNULL = open(os.devnull, 'wb') +from semver.logger import logging, logger, console_logger +from semver.utils import get_tag_version, get_file_version, DEVNULL +from semver import SemVer -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]*') - - 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 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_version(dot=False): +def get_version(build=0,npm=False,maven=False,dot=False): version = get_tag_version() # Get the commit hash of the version @@ -52,20 +19,33 @@ def get_version(dot=False): # do not match return the branch name else return the version if v_hash != c_hash: logger.debug("v_hash and c_hash do not match!") - b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, + branch = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() + # Find the next version + semver = SemVer() + semver.merged_branch = branch + version_type = semver.get_version_type() + p = subprocess.Popen(['bumpversion', '--dry-run', '--verbose', '--current-version', get_tag_version(), version_type], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd='.') + bump_output = p.stderr.read().decode() + next_version = match = re.search("New version will be '([0-9]*.[0-9]*.[0-9]*)'", bump_output).group(1) + + if npm: + return "{}-{}.{}".format(next_version,branch.replace('/','-'),build) + if maven: + qualifier = 'SNAPSHOT' if build == 0 else build + return "{}-{}-{}".format(next_version,branch.replace('/','-'),qualifier) if dot: - b = b.replace('/','.') - return b + branch = branch.replace('/','.') + return branch return version def main(): parser = argparse.ArgumentParser(description='Get Version or Branch.') - parser.add_argument('-d','--dot', help='Switch out / for . to be used in docker tag', action='store_true', dest='dot') + parser.add_argument('-d', '--dot', help='Switch out / for . to be used in docker tag', action='store_true', dest='dot') parser.add_argument('-D', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False) parser.add_argument('-n', '--npm', help='NPM sytle pre-release version', action='store_true', dest='npm', default=False) - parser.add_argument('-m', '--maven', help='Maven sytle pre-release version', action='store_true', dest='npm', default=False) + parser.add_argument('-m', '--maven', help='Maven sytle pre-release version', action='store_true', dest='maven', default=False) parser.add_argument('-b', '--build-number', help='Build number, used in pre-releases', default=0) args = parser.parse_args() @@ -73,7 +53,7 @@ def main(): if args.debug: console_logger.setLevel(logging.DEBUG) - print(get_version(args.dot)) + print(get_version(args.build_number,args.npm,args.maven,args.dot)) if __name__ == '__main__': try: main() diff --git a/semver/utils.py b/semver/utils.py new file mode 100644 index 0000000..88f813b --- /dev/null +++ b/semver/utils.py @@ -0,0 +1,41 @@ +import subprocess +from semver.logger import logging, logger, console_logger + +try: + from configparser import ConfigParser +except ImportError: + # Python < 3 + from ConfigParser import ConfigParser + +try: + 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]*') + + 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 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 +