Commit 1656d103 authored by Craig Weber's avatar Craig Weber

Add support for pre-releases and development releases according to pep440

parent e40190b1
......@@ -55,11 +55,11 @@ class VersionTagTest(unittest.TestCase):
silent_call('git', 'tag', 'r1.2.3')
silent_call('git', 'commit', '--allow-empty', '-m', 'another commit')
self.assertTrue( versiontag.get_version().startswith('r1.2.3-1-') )
self.assertEqual(versiontag.get_version(pypi=True), '1.2.3-1')
self.assertEqual(versiontag.get_version(pypi=True), '1.2.3.post1')
silent_call('git', 'commit', '--allow-empty', '-m', 'another commit')
self.assertTrue(versiontag.get_version().startswith('r1.2.3-2-'))
self.assertEqual(versiontag.get_version(pypi=True), '1.2.3-2')
self.assertEqual(versiontag.get_version(pypi=True), '1.2.3.post2')
silent_call('git', 'tag', 'r1.2.4')
self.assertTrue( versiontag.get_version().startswith('r1.2.4') )
......@@ -87,3 +87,19 @@ class VersionTagTest(unittest.TestCase):
self.assertEqual(versiontag.get_version(), 'r0.0.0')
self.assertEqual(versiontag.get_version(pypi=True), '0.0.0')
def test_pypi_normalize(self):
# Final releases
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3'), '1.2.3')
# Post releases
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3-12-abcdef'), '1.2.3.post12')
# Pre-releases
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3-a1'), '1.2.3a1')
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3-b1'), '1.2.3b1')
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3-rc1'), '1.2.3rc1')
# Dev releases
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3-dev3'), '1.2.3.dev3')
self.assertEqual(versiontag.convert_to_pypi_version('r1.2.3-a1-dev3'), '1.2.3a1.dev3')
......@@ -35,6 +35,37 @@ def cache_git_tag():
return version
def convert_to_pypi_version(version):
# See https://www.python.org/dev/peps/pep-0440/
# Convert to pypi valid version:
# * Normal Releases: r1.0.1 => 1.0.1
# * Dev Releases: r1.0.1-dev1 => 1.0.1.dev1
# * Alpha Releases: r1.0.1-a1 => 1.0.1a1
# * Beta Releases: r1.0.1-b4 => 1.0.1b4
# * RC Releases: r1.0.1-rc2 => 1.0.1rc2
# * Post Releases: r1.0.1-12-geaea7b6 => 1.0.1.post12
v = re.search('^[r,v]{0,1}(?P<final>[0-9\.]+)(\-(?P<pre>(a|b|rc)[0-9]+))?(\-(?P<dev>dev[0-9]+))?(\-(?P<post>[0-9]+))?(\-.+)?$', version)
if not v:
return __default_version__
# https://www.python.org/dev/peps/pep-0440/#final-releases
version = v.group('final')
# https://www.python.org/dev/peps/pep-0440/#pre-releases
if v.group('pre'):
version += v.group('pre')
# https://www.python.org/dev/peps/pep-0440/#developmental-releases
if v.group('dev'):
version += '.%s' % v.group('dev')
# https://www.python.org/dev/peps/pep-0440/#post-releases
if v.group('post'):
version += '.post%s' % v.group('post')
return version
def get_version(pypi=False):
version = __default_version__
......@@ -49,13 +80,10 @@ def get_version(pypi=False):
except:
pass
if pypi:
version = convert_to_pypi_version(version)
if version == __default_version__:
logger.warning("versiontag could not determine package version using cwd %s. Returning default: %s" % (os.getcwd(), __default_version__))
if pypi:
# Convert to pypi valid version:
# 1. Drop the r prefix we use on git tags: r1.0.1-12-geaea7b6 => 1.0.1-12-geaea7b6
# 2. Drop the commit hash from the end: 1.0.1-12-geaea7b6 => 1.0.1-12
v = re.search('^[r,v]{0,1}(([0-9\.]+)(\-[0-9]+)?)(\-.+)?$', version)
version = v.group(1) if v else __default_version__
return 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