blob: 7ac431c7a3c530f142727d07110264a35956fce7 [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
Alex41485522019-04-12 17:26:18 -050029def get_arg(args, str_arg):
30 _attr = getattr(args, str_arg)
31 if _attr:
32 return _attr
Alex Savatieievc9055712019-03-01 14:43:56 -060033 else:
Alex41485522019-04-12 17:26:18 -050034 _c = args.command if hasattr(args, 'command') else ''
35 _t = args.type if hasattr(args, 'type') else ''
36 raise ConfigException(
Alexe0c5b9e2019-04-23 18:51:23 -050037 "Argument '{}' not found executing: mcp-check {} {}".format(
Alex41485522019-04-12 17:26:18 -050038 str_arg,
39 _c,
40 _t
41 )
42 )
Alex Savatieievc9055712019-03-01 14:43:56 -060043
44
45def get_path_arg(path):
46 if os.path.exists(path):
47 return path
48 else:
49 raise ConfigException("'{}' not exists".format(path))
Alex41485522019-04-12 17:26:18 -050050
51
Alexbab1efe2019-04-23 18:51:23 -050052def 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
64def get_package_report_type_and_filename(args):
Alex41485522019-04-12 17:26:18 -050065 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")
Alex9a4ad212020-10-01 18:04:25 -050074
75
76def 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 )