make version comparison more robust

We have some projects with "invalid" version tags, so we need to make
the comparison code more robust to handle them.

Change-Id: I56ab0f1f92ab3a2954a553318d760348cf01069e
This commit is contained in:
Doug Hellmann 2015-09-08 16:56:57 +00:00
parent b5d6bc712d
commit c380c8b84b
2 changed files with 25 additions and 2 deletions

View File

@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import itertools
def try_int(val):
try:
@ -29,6 +31,21 @@ def format_version(v):
return '.'.join(str(p) for p in v)
def _compute_next(actual):
"Given one field of a version, compute the next value."
try:
expected = actual + 1
except TypeError:
# The patch level is a string, not an int, meaning it is
# likely an alpha or beta release. Pull the prefix digits off
# and convert those to an int, defaulting to 0 if there is no
# prefix.
prefix = ''.join(itertools.takewhile(lambda x: x.isdigit(),
actual))
expected = int(prefix or 0) + 1
return expected
def sanity_check_version(new_version, existing_versions):
warnings = []
if not existing_versions:
@ -57,7 +74,7 @@ def sanity_check_version(new_version, existing_versions):
if same_minor is not None:
print('last version in minor series %r' %
format_version(same_minor))
expected = same_minor[2] + 1
expected = _compute_next(same_minor[2])
actual = new_version[2]
if actual > expected:
warnings.append(
@ -68,7 +85,7 @@ def sanity_check_version(new_version, existing_versions):
print('last version in major series %r' %
format_version(same_major))
if new_version > same_major:
expected = same_major[1] + 1
expected = _compute_next(same_major[1])
actual = new_version[1]
if actual > expected:
warnings.append(

View File

@ -99,6 +99,12 @@ class RulesTest(base.BaseTestCase):
{'new_version': [1, 0, 0],
'existing_versions': [[0, 1, 0], [1, 0, 0]],
'expected': ["version '1.0.0' already exists in repository"]}),
# From a bad tag in python-neutronclient
('improper alpha version tag',
{'new_version': [3, 0, 0],
'existing_versions': [[3, 0, 'a1']],
'expected': []}),
]
def test(self):