blob: 08046174c9930a16dd4dfb5c8ec44597173e9365 [file] [log] [blame]
Alex265f45e2019-04-23 18:51:23 -05001import argparse
Alex Savatieievc9055712019-03-01 14:43:56 -06002import os
Alex265f45e2019-04-23 18:51:23 -05003import sys
Alex Savatieievc9055712019-03-01 14:43:56 -06004
Alex9a4ad212020-10-01 18:04:25 -05005from cfg_checker.common.exception import ConfigException, \
6 CommandTypeNotSupportedException, CheckerException
Alex Savatieievc9055712019-03-01 14:43:56 -06007
8
Alex265f45e2019-04-23 18:51:23 -05009class MyParser(argparse.ArgumentParser):
10 def error(self, message):
11 sys.stderr.write('Error: {0}\n\n'.format(message))
12 self.print_help()
13
14
Alexe9908f72020-05-19 16:04:53 -050015def 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
Alex5cace3b2021-11-10 16:40:37 -060029def get_arg(args, str_arg, nofail=False):
Alexbfa947c2021-11-11 18:14:28 -060030 if hasattr(args, str_arg):
31 return getattr(args, str_arg)
Alex Savatieievc9055712019-03-01 14:43:56 -060032 else:
Alex5cace3b2021-11-10 16:40:37 -060033 if nofail:
34 return None
Alex41485522019-04-12 17:26:18 -050035 _c = args.command if hasattr(args, 'command') else ''
36 _t = args.type if hasattr(args, 'type') else ''
37 raise ConfigException(
Alexe0c5b9e2019-04-23 18:51:23 -050038 "Argument '{}' not found executing: mcp-check {} {}".format(
Alex41485522019-04-12 17:26:18 -050039 str_arg,
40 _c,
41 _t
42 )
43 )
Alex Savatieievc9055712019-03-01 14:43:56 -060044
45
46def get_path_arg(path):
47 if os.path.exists(path):
48 return path
49 else:
50 raise ConfigException("'{}' not exists".format(path))
Alex41485522019-04-12 17:26:18 -050051
52
Alexbab1efe2019-04-23 18:51:23 -050053def 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
65def get_package_report_type_and_filename(args):
Alex41485522019-04-12 17:26:18 -050066 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")
Alex9a4ad212020-10-01 18:04:25 -050075
76
77def 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):
Alexccb72e02021-01-20 16:38:03 -060089 if not hasattr(args, 'force_env_type'):
Alex9a4ad212020-10-01 18:04:25 -050090 return _tenv
Alexccb72e02021-01-20 16:38:03 -060091 elif args.force_env_type == _tenv or not args.force_env_type:
Alex9a4ad212020-10-01 18:04:25 -050092 return _tenv
93 else:
94 raise CommandTypeNotSupportedException(
95 "'{}' -> '{}' is not supported "
96 "for selected env of '{}'".format(
97 args.command,
98 args.type,
Alexccb72e02021-01-20 16:38:03 -060099 args.force_env_type
Alex9a4ad212020-10-01 18:04:25 -0500100 )
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:
Alexccb72e02021-01-20 16:38:03 -0600108 if not hasattr(args, 'force_env_type'):
Alex9a4ad212020-10-01 18:04:25 -0500109 raise CheckerException(
110 "Multiple envs detected: {}, use --use-env option".format(
111 ",".join(list(_set))
112 )
113 )
Alexccb72e02021-01-20 16:38:03 -0600114 elif args.force_env_type and args.force_env_type in _set:
115 return args.force_env_type
Alex9a4ad212020-10-01 18:04:25 -0500116 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 )