From b76c873e264971303ec4ec24892aa73e2e794047 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Fri, 21 Feb 2020 17:44:15 -0500 Subject: [PATCH 1/2] Implemented debug logging --- README.md | 8 ++++++++ semver/__init__.py | 14 +++++++++++--- semver/get_version.py | 12 +++++++++++- semver/logger.py | 19 +++++++++++++++++++ vars/runAutoSemver.groovy | 11 ++++++++--- 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 semver/logger.py diff --git a/README.md b/README.md index 5d891b7..6f58749 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,10 @@ Does not push after versioning. Shows helps screen. +`-D`/`--debug` + +Runs with debug logging. + ### semver_get_version @@ -129,6 +133,10 @@ The `semver_get_version` command returns the version number if the `semver` comm Replaces `/` with `.` in branch names. For example, `feature/test` becomes `feature.test` +`-D`/`--debug` + +Runs with debug logging. + ### Jenkins Shared Library This repository is also home to a Jenkins shared library to assit in running auto-semver. diff --git a/semver/__init__.py b/semver/__init__.py index c654210..73a9ac0 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -2,6 +2,7 @@ import argparse import re import subprocess from semver.get_version import get_tag_version +from semver.logger import logging, logger, console_logger try: from configparser import ConfigParser @@ -50,7 +51,7 @@ class SemVer(object): cwd='.') message = str(p.stdout.read()) branch = b.stdout.read().decode('utf-8').rstrip() - print('Main branch is ' + branch) + logger.info('Main branch is ' + branch) matches = self.GET_COMMIT_MESSAGE.search(message) if matches: if str(matches.group(4)) == branch: @@ -62,7 +63,7 @@ class SemVer(object): # based on branches involved see what type of versioning should be done def get_version_type(self): - print('Merged branch is ' + self.merged_branch) + logger.info('Merged branch is ' + self.merged_branch) merged_prefix = None matches = re.findall("[^\/]*/", self.merged_branch) @@ -112,6 +113,7 @@ class SemVer(object): #file.write(config_file) # version repo + logger.debug("Running bumpversion of type: " + self.version_type) p = subprocess.Popen(['bumpversion', '--current-version', get_tag_version(), self.version_type], cwd='.') p.wait() @@ -152,10 +154,16 @@ def main(): parser = argparse.ArgumentParser(description='Bump Semantic Version.') parser.add_argument('-n','--no-push', help='Do not try to push', action='store_false', dest='push') parser.add_argument('-g','--global-user', help='Set git user at a global level, helps in jenkins', action='store_true', dest='global_user') + parser.add_argument('-D', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False) args = parser.parse_args() + + + if args.debug: + console_logger.setLevel(logging.DEBUG) + SemVer(global_user=args.global_user).run(push=args.push) except Exception as e: - print(e) + logger.error(e) if e == NO_MERGE_FOUND: exit(1) elif e == NOT_MAIN_BRANCH: diff --git a/semver/get_version.py b/semver/get_version.py index f922d27..37745dd 100644 --- a/semver/get_version.py +++ b/semver/get_version.py @@ -1,4 +1,5 @@ import argparse +from semver.logger import logging, logger, console_logger import subprocess try: from configparser import ConfigParser @@ -16,12 +17,15 @@ def get_tag_version(): 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)) + version = "0.0.0" tagged_versions = subprocess.Popen(['git','tag','--sort=taggerdate', '-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_version(dot=False): @@ -33,9 +37,11 @@ def get_version(dot=False): # Get the current commit hash c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() + # If the version commit hash and current commit hash # 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, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() if dot: @@ -47,9 +53,13 @@ def get_version(dot=False): 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', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False) args = parser.parse_args() - print(get_version(args.dot)) + if args.debug: + console_logger.setLevel(logging.DEBUG) + + logger.info(get_version(args.dot)) if __name__ == '__main__': try: main() diff --git a/semver/logger.py b/semver/logger.py new file mode 100644 index 0000000..0b5d96d --- /dev/null +++ b/semver/logger.py @@ -0,0 +1,19 @@ +import logging +import subprocess + +# create logger +logger = logging.getLogger('simple_example') +logger.setLevel(logging.DEBUG) + +# create console handler and set level to INFO +console_logger = logging.StreamHandler() +console_logger.setLevel(logging.INFO) + +# create formatter +formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + +# add formatter to console_logger +console_logger.setFormatter(formatter) + +# add console_logger to logger +logger.addHandler(console_logger) \ No newline at end of file diff --git a/vars/runAutoSemver.groovy b/vars/runAutoSemver.groovy index 13e73c1..2f8f825 100644 --- a/vars/runAutoSemver.groovy +++ b/vars/runAutoSemver.groovy @@ -10,16 +10,21 @@ */ // Run Auto Semver -def call(dockerImage = "rightbrainnetworks/auto-semver:latest") { +def call(dockerImage = "rightbrainnetworks/auto-semver:latest", debug = false) { def docker_image = docker.image(dockerImage) docker_image.pull() docker_image.inside{ + def args = "" + if(debug) + { + args="--debug" + } def RETURN_STATUS def regex = '^\\s*current_version\\s*=\\s*\\K[^\\s]+' - RETURN_STATUS = sh(script: "semver -n", returnStatus: true) + RETURN_STATUS = sh(script: "semver -n ${args}", returnStatus: true) echo "Semver Return Status: ${RETURN_STATUS}" env.SEMVER_STATUS = RETURN_STATUS switch (RETURN_STATUS) { @@ -36,7 +41,7 @@ def call(dockerImage = "rightbrainnetworks/auto-semver:latest") { } env.SEMVER_NEW_VERSION = sh(script: "grep -Po '${regex}' .bumpversion.cfg", returnStdout: true).trim() - env.SEMVER_RESOLVED_VERSION = getVersion('-d') + env.SEMVER_RESOLVED_VERSION = getVersion("-d ${args}") env.VERSION = env.SEMVER_RESOLVED_VERSION } From 95292594411a7576a8652dfd71596bc949213672 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Fri, 21 Feb 2020 17:48:37 -0500 Subject: [PATCH 2/2] Enable debug logging for tests --- semver/tests.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/semver/tests.py b/semver/tests.py index 1000e0e..10b4973 100644 --- a/semver/tests.py +++ b/semver/tests.py @@ -1,5 +1,5 @@ import unittest, os, subprocess, semver - +from semver.logger import logging, logger, console_logger from semver import get_version @@ -73,6 +73,8 @@ def create_git_environment(): subprocess.run(['git', 'remote', 'add', 'origin', os.getcwd()+'/.git']) if __name__ == "__main__": + console_logger.setLevel(logging.DEBUG) + subprocess.run(['rm', '-rf', test_directory]) subprocess.run(['mkdir', test_directory]) os.chdir(test_directory)