Fabio Utzig | 14301ab | 2020-04-16 17:20:38 -0300 | [diff] [blame] | 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 | |
Fabio Utzig | 46e554e | 2023-01-03 20:13:05 -0300 | [diff] [blame] | 15 | from packaging.version import parse, InvalidVersion |
Fabio Utzig | 14301ab | 2020-04-16 17:20:38 -0300 | [diff] [blame] | 16 | import argparse |
| 17 | import sys |
| 18 | |
Fabio Utzig | 46e554e | 2023-01-03 20:13:05 -0300 | [diff] [blame] | 19 | try: |
| 20 | from packaging.version import LegacyVersion |
| 21 | except ImportError: |
| 22 | LegacyVersion = () # trick isinstance! |
| 23 | |
Fabio Utzig | 14301ab | 2020-04-16 17:20:38 -0300 | [diff] [blame] | 24 | # 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 | |
| 29 | parser = argparse.ArgumentParser() |
| 30 | parser.add_argument('--old', help='Version currently in use') |
| 31 | parser.add_argument('--new', help='New version to publish') |
| 32 | |
| 33 | args = parser.parse_args() |
| 34 | if args.old is None or args.new is None: |
| 35 | parser.print_help() |
| 36 | exit(1) |
| 37 | |
Fabio Utzig | 46e554e | 2023-01-03 20:13:05 -0300 | [diff] [blame] | 38 | # 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 Utzig | 14301ab | 2020-04-16 17:20:38 -0300 | [diff] [blame] | 42 | |
Fabio Utzig | 46e554e | 2023-01-03 20:13:05 -0300 | [diff] [blame] | 43 | versions = [] |
| 44 | for 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 | |
| 51 | old, new = versions[0], versions[1] |
Fabio Utzig | 14301ab | 2020-04-16 17:20:38 -0300 | [diff] [blame] | 52 | for version in [old, new]: |
| 53 | if isinstance(version, LegacyVersion): |
| 54 | print("Invalid version parsed: {}".format(version)) |
| 55 | sys.exit(1) |
| 56 | |
| 57 | if new == old: |
| 58 | print("No version change") |
| 59 | sys.exit(0) |
| 60 | elif new > old: |
| 61 | print("Upgrade detected ({} > {})".format(new, old)) |
| 62 | sys.exit(2) |
| 63 | |
| 64 | print("Downgrade detected ({} < {})".format(new, old)) |
| 65 | sys.exit(3) |