81 lines
3.0 KiB
Python
81 lines
3.0 KiB
Python
#!/usr/bin/python
|
|
|
|
import re
|
|
import argparse
|
|
import sys
|
|
|
|
parser = argparse.ArgumentParser(description='Semantic Version Validator')
|
|
parser.add_argument('--versionToValidate', '-V',
|
|
help='The version to validate against the semantic versioning rules',
|
|
required=True)
|
|
parser.add_argument('--messageToValidate', '-M',
|
|
help='A message to validate, means: it must not be empty',
|
|
default='',
|
|
required=False)
|
|
parser.add_argument('--validateMessage', '-m',
|
|
help='Consider -M',
|
|
required=False,
|
|
action='store_true',
|
|
default=False)
|
|
parser.add_argument('--printExports', '-e',
|
|
help='Print exports',
|
|
action='store_true',
|
|
default=False,
|
|
required=False)
|
|
parser.add_argument('--exportFormat', '-f',
|
|
help='Print exports in >bash< or >powershell< format, to be used in '
|
|
'backticks or with Invoke-Expression',
|
|
default='bash',
|
|
required=False)
|
|
parser.add_argument('--verbose', '-v',
|
|
help='Verbose output, overrides -q',
|
|
required=False,
|
|
action='store_true',
|
|
default=False)
|
|
args = parser.parse_args()
|
|
|
|
verbose = args.verbose
|
|
versionToValidate = args.versionToValidate
|
|
messageToValidate = args.messageToValidate
|
|
validateMessage = args.validateMessage
|
|
printExports = args.printExports
|
|
exportFormat = args.exportFormat
|
|
|
|
r = re.compile(r'^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$')
|
|
|
|
if verbose:
|
|
print("Version to validate: {}".format(versionToValidate))
|
|
|
|
m = r.match(versionToValidate)
|
|
|
|
if m:
|
|
if verbose:
|
|
for i in r.groupindex:
|
|
print("Found: {}: {}".format(i, m.group(i)))
|
|
if validateMessage:
|
|
if verbose:
|
|
print("Checking message {}".format(messageToValidate))
|
|
if messageToValidate == '':
|
|
if verbose:
|
|
print("Message shall be validate and is invalid")
|
|
sys.exit(1)
|
|
else:
|
|
if printExports:
|
|
if exportFormat == "bash":
|
|
print("export MESSAGE={}".format(messageToValidate))
|
|
elif exportFormat == "powershell":
|
|
print("set MESSAGE {}".format(messageToValidate))
|
|
|
|
if printExports:
|
|
for i in r.groupindex:
|
|
if exportFormat == "bash":
|
|
print("export {}={}".format(i.upper(), '' if m.group(i) is None else m.group(i)))
|
|
elif exportFormat == "powershell":
|
|
print("set {} {}".format(i.upper(), '""' if m.group(i) is None else m.group(i)))
|
|
|
|
sys.exit(0)
|
|
else:
|
|
if verbose:
|
|
print("Version is invalid")
|
|
sys.exit(1)
|