1#!/usr/bin/env python3
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from packaging.version import parse, LegacyVersion
16import argparse
17import sys
18
19# exit with 0 if --new is equal to --old
20# exit with 1 on errors
21# exit with 2 if --new is newer than --old
22# exit with 3 if --new is older than --old
23
24parser = argparse.ArgumentParser()
25parser.add_argument('--old', help='Version currently in use')
26parser.add_argument('--new', help='New version to publish')
27
28args = parser.parse_args()
29if args.old is None or args.new is None:
30    parser.print_help()
31    exit(1)
32
33old, new = parse(args.old), parse(args.new)
34
35# only accept versions that were correctly parsed
36for version in [old, new]:
37    if isinstance(version, LegacyVersion):
38        print("Invalid version parsed: {}".format(version))
39        sys.exit(1)
40
41if new == old:
42    print("No version change")
43    sys.exit(0)
44elif new > old:
45    print("Upgrade detected ({} > {})".format(new, old))
46    sys.exit(2)
47
48print("Downgrade detected ({} < {})".format(new, old))
49sys.exit(3)
50