Skip to content

Instantly share code, notes, and snippets.

@DrSLDR DrSLDR/tick.py
Created Jan 28, 2017

Embed
What would you like to do?
Utility for simultaneously updating version numbers throughout several files
"""
Ticker utility
Speaks SemVer (Semantic Versioning). Ticks the numbers as ordered.
"""
import re
import argparse
FILELIST = ['README.md']
PAR = argparse.ArgumentParser(description='Ticks Semantic Versioning ' +
'markers. Build count is always increased.')
LGR = PAR.add_mutually_exclusive_group(required=False)
LGR.add_argument('-M', '--major', action='store_true',
help='Increase major version')
LGR.add_argument('-m', '--minor', action='store_true',
help='Increase minor version')
LGR.add_argument('-p', '--patch', action='store_true',
help='Increase patch number')
PAR.add_argument('-v', '--verbose', action='store_true',
help='Be louder')
ARGS = PAR.parse_args()
REGEX = re.compile(r'(\d+)\.(\d+)\.(\d+)\+(\d+)')
for fname in FILELIST:
if ARGS.verbose:
print('In ' + fname)
f = open(fname)
data = f.read()
m = REGEX.search(data)
if m:
if ARGS.verbose:
print('Found version string: ' + m.group(0))
blocking = False
major = m.group(1)
if ARGS.major:
major = str(int(major) + 1)
blocking = True
if ARGS.verbose:
print('Ticking major version to ' + major)
major = str(major)
minor = m.group(2)
if not blocking and ARGS.minor:
minor = str(int(minor) + 1)
blocking = True
if ARGS.verbose:
print('Ticking minor version to ' + minor)
elif blocking:
minor = "0"
if ARGS.verbose:
print('Resetting minor version to 0')
minor = str(minor)
patch = m.group(3)
if not blocking and ARGS.patch:
patch = str(int(patch) + 1)
if ARGS.verbose:
print('Ticking patch number to ' + patch)
elif blocking:
patch = "0"
if ARGS.verbose:
print('Resetting patch number to 0')
patch = str(patch)
build = str(int(m.group(4)) + 1)
if ARGS.verbose:
print('Ticking build counter to ' + build)
verstr = major + "." + minor + "." + patch + "+" + build
start, end = m.span(0)
data = data[:max(end-len(verstr), start)] + verstr + data[end:]
f.close()
f = open(fname, 'w')
f.write(data)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.