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