blob: d97392e56e55229db0c5b57a1d0e7a06f16f182e [file] [log] [blame]
Fabio Utzig14301ab2020-04-16 17:20:38 -03001#!/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
Fabio Utzig46e554e2023-01-03 20:13:05 -030015from packaging.version import parse, InvalidVersion
Fabio Utzig14301ab2020-04-16 17:20:38 -030016import argparse
17import sys
18
Fabio Utzig46e554e2023-01-03 20:13:05 -030019try:
20 from packaging.version import LegacyVersion
21except ImportError:
22 LegacyVersion = () # trick isinstance!
23
Fabio Utzig14301ab2020-04-16 17:20:38 -030024# exit with 0 if --new is equal to --old
25# exit with 1 on errors
26# exit with 2 if --new is newer than --old
27# exit with 3 if --new is older than --old
28
29parser = argparse.ArgumentParser()
30parser.add_argument('--old', help='Version currently in use')
31parser.add_argument('--new', help='New version to publish')
32
33args = parser.parse_args()
34if args.old is None or args.new is None:
35 parser.print_help()
36 exit(1)
37
Fabio Utzig46e554e2023-01-03 20:13:05 -030038# packaging>=22 only supports PEP-440 version numbers, and a non-valid version
39# will throw InvalidVersion. Previous packaging releases would create a
40# LegacyVersion object if the given version string failed to parse as PEP-440,
41# and since we use versions closer to semver, we want to fail in that case.
Fabio Utzig14301ab2020-04-16 17:20:38 -030042
Fabio Utzig46e554e2023-01-03 20:13:05 -030043versions = []
44for version in [args.old, args.new]:
45 try:
46 versions.append(parse(version))
47 except InvalidVersion:
48 print("Invalid version parsed: {}".format(version))
49 sys.exit(1)
50
51old, new = versions[0], versions[1]
Fabio Utzig14301ab2020-04-16 17:20:38 -030052for version in [old, new]:
53 if isinstance(version, LegacyVersion):
54 print("Invalid version parsed: {}".format(version))
55 sys.exit(1)
56
57if new == old:
58 print("No version change")
59 sys.exit(0)
60elif new > old:
61 print("Upgrade detected ({} > {})".format(new, old))
62 sys.exit(2)
63
64print("Downgrade detected ({} < {})".format(new, old))
65sys.exit(3)