Commit de6a67ef authored by Jameson Rollins's avatar Jameson Rollins

make version module executable to expose version functions

This allows for executing the guardian.version module to convert
version strings to/from ints, and to produce a new release version.
parent c5996550
......@@ -43,14 +43,11 @@ _release-commit: test version
git add $(VFILE)
git commit -m "Release $(VERSION)"
git tag -m "Release" $(VERSION)
release-major: VERSION = $(shell PYTHONPATH=lib/guardian python -c \
"import version as v; print v._release('$(SVERS)', 'major')")
release-major: VERSION = $(shell PYTHONPATH=lib python -m guardian.version -r major $(SVERS))
release-major: _release-commit
release-minor: VERSION = $(shell PYTHONPATH=lib/guardian python -c \
"import version as v; print v._release('$(SVERS)', 'minor')")
release-minor: VERSION = $(shell PYTHONPATH=lib python -m guardian.version -r minor $(SVERS))
release-minor: _release-commit
release-rev: VERSION = $(shell PYTHONPATH=lib/guardian python -c \
"import version as v; print v._release('$(SVERS)', 'rev')")
release-rev: VERSION = $(shell PYTHONPATH=lib python -m guardian.version -r rev $(SVERS))
release-rev: _release-commit
release: release-rev
......
......@@ -15,15 +15,18 @@
#
# FIXME: what should we do if the version string has a suffix?
def check_vers(major, minor, rev):
if not (major < 16**1 and minor < 16**2 and rev < 16**2):
raise ValueError("invalid version number: out of range")
def vers_to_str(major, minor, rev):
"""return version string major.minor.rev"""
check_vers(major, minor, rev)
return '{0}.{1}.{2}'.format(major, minor, rev)
def vers_to_hex(major, minor, rev):
"""return version hex from major,minor,rev"""
check_vers(major, minor, rev)
......@@ -33,11 +36,13 @@ def vers_to_hex(major, minor, rev):
rev,
)
def vers_to_int(major, minor, rev, suffix=''):
"""return version int from major,minor,rev"""
check_vers(major, minor, rev)
return int(vers_to_hex(major, minor, rev), 16)
def vers_from_str(s):
"""return version info from version string: major,minor,rev,suffix
......@@ -52,9 +57,10 @@ def vers_from_str(s):
try:
major, minor, rev = vsplit[0].split('.')
except ValueError:
raise ValueError("Could not parse version string: {}".format(s))
raise ValueError("Could not parse version string '{}' (should consist of major.minor.rev)".format(s))
return int(major), int(minor), int(rev), suffix
def vers_from_int(n):
"""return version major,minor,rev from version int"""
h = hex(n)[2:].zfill(8)
......@@ -63,14 +69,17 @@ def vers_from_int(n):
rev = int(h[6:8], 16)
return major, minor, rev
def vers_str2int(s):
"""return version int from version string"""
return vers_to_int(*vers_from_str(s))
def vers_int2str(s):
"""return version string from version int"""
return vers_to_str(*vers_from_int(s))
def _release(s, t=None):
"""Return new release version for given version string and release type
......@@ -85,4 +94,37 @@ def _release(s, t=None):
rev = 0
elif t == 'rev':
rev += 1
return vers_to_str(major,minor,rev)
return vers_to_str(major, minor, rev)
if __name__ == '__main__':
import argparse
from . import __version__
parser = argparse.ArgumentParser(
description="""
With no arguments outputs the current version. With a version string
or int argument, convert to int or string. With --release option,
determine appropriate new release version from the specified version,
or from the current version if not specified.""",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
'VERS', nargs='?',
help="version string or int")
vgroup = parser.add_mutually_exclusive_group(required=False)
vgroup.add_argument(
'--release', '-r', choices=['major', 'minor', 'rev'],
help="create new release version")
args = parser.parse_args()
if args.release:
vers = args.VERS or __version__
print(_release(vers, args.release))
elif args.VERS:
try:
print(vers_int2str(int(args.VERS)))
except ValueError:
print(vers_str2int(args.VERS))
else:
print(__version__)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment