blob: 84bd6f149562dab1bcb06568d0d15263ee68587f [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):
Alex41485522019-04-12 17:26:18 -050030 _attr = getattr(args, str_arg)
31 if _attr:
32 return _attr
Alex Savatieievc9055712019-03-01 14:43:56 -060033 else:
Alex5cace3b2021-11-10 16:40:37 -060034 if nofail:
35 return None
Alex41485522019-04-12 17:26:18 -050036 _c = args.command if hasattr(args, 'command') else ''
37 _t = args.type if hasattr(args, 'type') else ''
38 raise ConfigException(
Alexe0c5b9e2019-04-23 18:51:23 -050039 "Argument '{}' not found executing: mcp-check {} {}".format(
Alex41485522019-04-12 17:26:18 -050040 str_arg,
41 _c,
42 _t
43 )
44 )
Alex Savatieievc9055712019-03-01 14:43:56 -060045
46
47def get_path_arg(path):
48 if os.path.exists(path):
49 return path
50 else:
51 raise ConfigException("'{}' not exists".format(path))
Alex41485522019-04-12 17:26:18 -050052
53
Alexbab1efe2019-04-23 18:51:23 -050054def get_network_map_type_and_filename(args):
55 if args.html or args.text:
56 if args.html and args.text:
57 raise ConfigException("Multuple report types not supported")
58 if args.html is not None:
59 return 'html', args.html
60 if args.text is not None:
61 return 'text', args.text
62 else:
63 return 'console', None
64
65
66def get_package_report_type_and_filename(args):
Alex41485522019-04-12 17:26:18 -050067 if args.html or args.csv:
68 if args.html and args.csv:
69 raise ConfigException("Multuple report types not supported")
70 if args.html is not None:
71 return 'html', args.html
72 if args.csv is not None:
73 return 'csv', args.csv
74 else:
75 raise ConfigException("Report type and filename not set")
Alex9a4ad212020-10-01 18:04:25 -050076
77
78def check_supported_env(target_env, args, config):
79 def _raise_type_not_supported():
80 raise CommandTypeNotSupportedException(
81 "'{}' -> '{}' is not supported on any of "
82 "the currently detected environments: "
83 "{}".format(
84 args.command,
85 args.type,
86 ",".join(config.detected_envs))
87 )
88
89 def _check_target_vs_used(_tenv):
Alexccb72e02021-01-20 16:38:03 -060090 if not hasattr(args, 'force_env_type'):
Alex9a4ad212020-10-01 18:04:25 -050091 return _tenv
Alexccb72e02021-01-20 16:38:03 -060092 elif args.force_env_type == _tenv or not args.force_env_type:
Alex9a4ad212020-10-01 18:04:25 -050093 return _tenv
94 else:
95 raise CommandTypeNotSupportedException(
96 "'{}' -> '{}' is not supported "
97 "for selected env of '{}'".format(
98 args.command,
99 args.type,
Alexccb72e02021-01-20 16:38:03 -0600100 args.force_env_type
Alex9a4ad212020-10-01 18:04:25 -0500101 )
102 )
103
104 if isinstance(target_env, list):
105 _set = set(config.detected_envs).intersection(set(target_env))
106 if len(_set) < 1:
107 _raise_type_not_supported()
108 if len(_set) > 1:
Alexccb72e02021-01-20 16:38:03 -0600109 if not hasattr(args, 'force_env_type'):
Alex9a4ad212020-10-01 18:04:25 -0500110 raise CheckerException(
111 "Multiple envs detected: {}, use --use-env option".format(
112 ",".join(list(_set))
113 )
114 )
Alexccb72e02021-01-20 16:38:03 -0600115 elif args.force_env_type and args.force_env_type in _set:
116 return args.force_env_type
Alex9a4ad212020-10-01 18:04:25 -0500117 else:
118 _raise_type_not_supported()
119 else:
120 return _check_target_vs_used(list(_set)[0])
121 elif isinstance(target_env, str):
122 if target_env not in config.detected_envs:
123 _raise_type_not_supported()
124 else:
125 return _check_target_vs_used(target_env)
126 else:
127 raise CheckerException(
128 "Unexpected target env type '{}' in '{}' -> '{}'".format(
129 target_env,
130 args.command,
131 args.type,
132 )
133 )