2018-06-06 15:56:45 +02:00
|
|
|
import argparse
|
2018-06-05 23:10:32 +02:00
|
|
|
import subprocess
|
2018-05-23 21:41:35 +02:00
|
|
|
try:
|
|
|
|
from configparser import ConfigParser
|
|
|
|
except ImportError:
|
|
|
|
# Python < 3
|
|
|
|
from ConfigParser import ConfigParser
|
2018-06-06 15:56:45 +02:00
|
|
|
try:
|
|
|
|
from subprocess import DEVNULL # py3k
|
|
|
|
except ImportError:
|
|
|
|
import os
|
|
|
|
DEVNULL = open(os.devnull, 'wb')
|
2017-02-23 20:39:43 +01:00
|
|
|
|
2019-11-22 21:30:58 +01:00
|
|
|
def get_tag_version():
|
2019-11-22 22:57:30 +01:00
|
|
|
config = ConfigParser()
|
|
|
|
config.read('./.bumpversion.cfg')
|
|
|
|
tag_expression = config.get('bumpversion','tag_name').replace('{new_version}','[0-9]*.[0-9]*.[0-9]*')
|
|
|
|
|
2019-11-22 22:10:01 +01:00
|
|
|
version = "0.0.0"
|
2019-11-22 20:51:32 +01:00
|
|
|
|
2019-11-27 18:35:41 +01:00
|
|
|
tagged_versions = subprocess.Popen(['git','tag','--sort=taggerdate', '-l',tag_expression],
|
2019-11-22 20:51:32 +01:00
|
|
|
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]
|
2019-11-22 21:30:58 +01:00
|
|
|
return version
|
|
|
|
|
|
|
|
def get_version(dot=False):
|
|
|
|
version = get_tag_version()
|
2019-11-22 20:51:32 +01:00
|
|
|
|
2018-06-05 23:10:32 +02:00
|
|
|
# Get the commit hash of the version
|
|
|
|
v_hash = subprocess.Popen(['git', 'rev-list', '-n', '1', version], stdout=subprocess.PIPE,
|
2018-06-06 15:56:45 +02:00
|
|
|
stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
|
2018-06-05 23:10:32 +02:00
|
|
|
# Get the current commit hash
|
|
|
|
c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
|
2018-06-06 15:56:45 +02:00
|
|
|
stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
|
2018-06-05 23:10:32 +02:00
|
|
|
# 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:
|
|
|
|
b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE,
|
2018-06-06 15:56:45 +02:00
|
|
|
stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
|
|
|
|
if dot:
|
|
|
|
b = b.replace('/','.')
|
2018-06-05 23:10:32 +02:00
|
|
|
return b
|
2017-02-23 20:39:43 +01:00
|
|
|
return version
|
|
|
|
|
|
|
|
|
2018-05-23 21:09:14 +02:00
|
|
|
def main():
|
2018-06-06 15:56:45 +02:00
|
|
|
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')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
print(get_version(args.dot))
|
2018-05-23 21:09:14 +02:00
|
|
|
|
2017-02-23 20:39:43 +01:00
|
|
|
if __name__ == '__main__':
|
2018-05-23 21:09:14 +02:00
|
|
|
try: main()
|
|
|
|
except: raise
|