Martin Kopec | 05c35eb | 2020-08-12 09:17:35 +0000 | [diff] [blame] | 1 | # Copyright 2020 Red Hat, Inc |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | """ |
| 17 | Utility for content checking of a given dry_run.json file. |
| 18 | """ |
| 19 | |
| 20 | import argparse |
| 21 | import json |
| 22 | import sys |
| 23 | |
| 24 | |
| 25 | def get_parser(): |
| 26 | parser = argparse.ArgumentParser(__doc__) |
| 27 | parser.add_argument('--is-empty', action="store_true", dest='is_empty', |
| 28 | default=False, |
| 29 | help="""Are values of a given dry_run.json empty?""") |
| 30 | parser.add_argument('--file', dest='file', default=None, metavar='PATH', |
| 31 | help="A path to a dry_run.json file.") |
| 32 | return parser |
| 33 | |
| 34 | |
| 35 | def parse_arguments(): |
| 36 | parser = get_parser() |
| 37 | args = parser.parse_args() |
| 38 | if not args.file: |
| 39 | sys.stderr.write('Path to a dry_run.json must be specified.\n') |
| 40 | sys.exit(1) |
| 41 | return args |
| 42 | |
| 43 | |
| 44 | def load_json(path): |
| 45 | """Load json content from file addressed by path.""" |
| 46 | try: |
| 47 | with open(path, 'rb') as json_file: |
| 48 | json_data = json.load(json_file) |
| 49 | except Exception as ex: |
| 50 | sys.exit(ex) |
| 51 | return json_data |
| 52 | |
| 53 | |
| 54 | def are_values_empty(dry_run_content): |
| 55 | """Return true if values of dry_run.json are empty.""" |
| 56 | for value in dry_run_content.values(): |
| 57 | if value: |
| 58 | return False |
| 59 | return True |
| 60 | |
| 61 | |
| 62 | def main(): |
| 63 | args = parse_arguments() |
| 64 | content = load_json(args.file) |
| 65 | if args.is_empty: |
| 66 | if not are_values_empty(content): |
| 67 | sys.exit(1) |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | main() |