Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import argparse |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 2 | import os |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 3 | import sys |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 4 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 5 | from cfg_checker.common.exception import ConfigException, \ |
| 6 | CommandTypeNotSupportedException, CheckerException |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 7 | |
| 8 | |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 9 | class MyParser(argparse.ArgumentParser): |
| 10 | def error(self, message): |
| 11 | sys.stderr.write('Error: {0}\n\n'.format(message)) |
| 12 | self.print_help() |
| 13 | |
| 14 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 15 | def get_skip_args(args): |
| 16 | if hasattr(args, "skip_nodes"): |
| 17 | _skip = getattr(args, "skip_nodes") |
| 18 | if _skip: |
| 19 | _skip = _skip.split(',') |
| 20 | else: |
| 21 | _skip = None |
| 22 | if hasattr(args, "skip_nodes_file"): |
| 23 | _skip_file = getattr(args, "skip_nodes_file") |
| 24 | else: |
| 25 | _skip_file = None |
| 26 | return _skip, _skip_file |
| 27 | |
| 28 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 29 | def get_arg(args, str_arg): |
| 30 | _attr = getattr(args, str_arg) |
| 31 | if _attr: |
| 32 | return _attr |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 33 | else: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 34 | _c = args.command if hasattr(args, 'command') else '' |
| 35 | _t = args.type if hasattr(args, 'type') else '' |
| 36 | raise ConfigException( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 37 | "Argument '{}' not found executing: mcp-check {} {}".format( |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 38 | str_arg, |
| 39 | _c, |
| 40 | _t |
| 41 | ) |
| 42 | ) |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def get_path_arg(path): |
| 46 | if os.path.exists(path): |
| 47 | return path |
| 48 | else: |
| 49 | raise ConfigException("'{}' not exists".format(path)) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 50 | |
| 51 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 52 | def get_network_map_type_and_filename(args): |
| 53 | if args.html or args.text: |
| 54 | if args.html and args.text: |
| 55 | raise ConfigException("Multuple report types not supported") |
| 56 | if args.html is not None: |
| 57 | return 'html', args.html |
| 58 | if args.text is not None: |
| 59 | return 'text', args.text |
| 60 | else: |
| 61 | return 'console', None |
| 62 | |
| 63 | |
| 64 | def get_package_report_type_and_filename(args): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 65 | if args.html or args.csv: |
| 66 | if args.html and args.csv: |
| 67 | raise ConfigException("Multuple report types not supported") |
| 68 | if args.html is not None: |
| 69 | return 'html', args.html |
| 70 | if args.csv is not None: |
| 71 | return 'csv', args.csv |
| 72 | else: |
| 73 | raise ConfigException("Report type and filename not set") |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 74 | |
| 75 | |
| 76 | def check_supported_env(target_env, args, config): |
| 77 | def _raise_type_not_supported(): |
| 78 | raise CommandTypeNotSupportedException( |
| 79 | "'{}' -> '{}' is not supported on any of " |
| 80 | "the currently detected environments: " |
| 81 | "{}".format( |
| 82 | args.command, |
| 83 | args.type, |
| 84 | ",".join(config.detected_envs)) |
| 85 | ) |
| 86 | |
| 87 | def _check_target_vs_used(_tenv): |
| 88 | if not hasattr(args, 'use_env'): |
| 89 | return _tenv |
| 90 | elif args.use_env == _tenv or not args.use_env: |
| 91 | return _tenv |
| 92 | else: |
| 93 | raise CommandTypeNotSupportedException( |
| 94 | "'{}' -> '{}' is not supported " |
| 95 | "for selected env of '{}'".format( |
| 96 | args.command, |
| 97 | args.type, |
| 98 | args.use_env |
| 99 | ) |
| 100 | ) |
| 101 | |
| 102 | if isinstance(target_env, list): |
| 103 | _set = set(config.detected_envs).intersection(set(target_env)) |
| 104 | if len(_set) < 1: |
| 105 | _raise_type_not_supported() |
| 106 | if len(_set) > 1: |
| 107 | if not hasattr(args, 'use_env'): |
| 108 | raise CheckerException( |
| 109 | "Multiple envs detected: {}, use --use-env option".format( |
| 110 | ",".join(list(_set)) |
| 111 | ) |
| 112 | ) |
| 113 | elif args.use_env and args.use_env in _set: |
| 114 | return args.use_env |
| 115 | else: |
| 116 | _raise_type_not_supported() |
| 117 | else: |
| 118 | return _check_target_vs_used(list(_set)[0]) |
| 119 | elif isinstance(target_env, str): |
| 120 | if target_env not in config.detected_envs: |
| 121 | _raise_type_not_supported() |
| 122 | else: |
| 123 | return _check_target_vs_used(target_env) |
| 124 | else: |
| 125 | raise CheckerException( |
| 126 | "Unexpected target env type '{}' in '{}' -> '{}'".format( |
| 127 | target_env, |
| 128 | args.command, |
| 129 | args.type, |
| 130 | ) |
| 131 | ) |