From 319e3b4609c65fc3d4f9190f16a0ef45f185947e Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Thu, 30 Apr 2015 21:05:22 +0000 Subject: [PATCH] Move highest_semver.py from oslo-incubator Add a script to print the highest SemVer tag when given a list as input. Change-Id: Icb19338743cd013e855c10d5de25c3ac5b7ec873 --- README.rst | 7 ++++++ highest_semver.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 highest_semver.py diff --git a/README.rst b/README.rst index 27f5b85..29f79af 100644 --- a/README.rst +++ b/README.rst @@ -445,3 +445,10 @@ Examples: To clean up Nova kilo blueprints: ./autokick.py nova kilo + +highest_semver.py +----------------- + +Reads a list of version tags from standard input and prints the +"highest" value as output, ignoring tags that don't look like valid +versions. diff --git a/highest_semver.py b/highest_semver.py new file mode 100755 index 0000000..f331970 --- /dev/null +++ b/highest_semver.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Read a list of version tags from stdin and pick the one with the +highest semver value. +""" + +from __future__ import print_function + +import fileinput +import sys + + +tags = [] +for line in fileinput.input(sys.argv[1:]): + line = line.strip() + if not line: + continue + parts = line.split('.') + try: + v = tuple(int(val) for val in parts) + except ValueError: + # This tag is probably an alpha, so ignore it + continue + if len(v) == 3: + v = v + ('zzz',) # artifically sort the value higher than alphas + # Ignore versions where the beginning doesn't look like a number, + # such as 'havana-eol' + if not isinstance(v[0], int): + continue + # Ignore date-based entries + if v[0] > 100: + continue + tags.append(v) + +if tags: + # We only want to print something if we actually have any tags to + # pick from. Otherwise we probably have a library that has never + # been released, so there is no valid version. + version = max(tags) + if version[-1] == 'zzz': + version = version[:-1] + print('.'.join(str(t) for t in version))