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 | |
| 15 | from packaging.version import parse, LegacyVersion |
| 16 | import argparse |
| 17 | import 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 | |
| 24 | parser = argparse.ArgumentParser() |
| 25 | parser.add_argument('--old', help='Version currently in use') |
| 26 | parser.add_argument('--new', help='New version to publish') |
| 27 | |
| 28 | args = parser.parse_args() |
| 29 | if args.old is None or args.new is None: |
| 30 | parser.print_help() |
| 31 | exit(1) |
| 32 | |
| 33 | old, new = parse(args.old), parse(args.new) |
| 34 | |
| 35 | # only accept versions that were correctly parsed |
| 36 | for version in [old, new]: |
| 37 | if isinstance(version, LegacyVersion): |
| 38 | print("Invalid version parsed: {}".format(version)) |
| 39 | sys.exit(1) |
| 40 | |
| 41 | if new == old: |
| 42 | print("No version change") |
| 43 | sys.exit(0) |
| 44 | elif new > old: |
| 45 | print("Upgrade detected ({} > {})".format(new, old)) |
| 46 | sys.exit(2) |
| 47 | |
| 48 | print("Downgrade detected ({} < {})".format(new, old)) |
| 49 | sys.exit(3) |