1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """CSIT Presentation and Analytics Layer.
21 from pal_errors import PresentationError
22 from specification_parser import Specification
23 from environment import Environment, clean_environment
24 from static_content import prepare_static_content
25 from input_data_parser import InputData
26 from generator_tables import generate_tables
27 from generator_plots import generate_plots
28 from generator_files import generate_files
29 from generator_report import generate_report
30 from generator_cpta import generate_cpta
31 from generator_alerts import Alerting, AlertingError
32 from convert_xml_json import convert_xml_to_json
35 OUTPUTS = (u"none", u"report", u"trending", u"convert-xml-to-json")
39 """Parse arguments from cmd line.
41 :returns: Parsed arguments.
42 :rtype: ArgumentParser
45 parser = argparse.ArgumentParser(
47 formatter_class=argparse.RawDescriptionHelpFormatter
50 u"-s", u"--specification",
53 help=u"Specification YAML file."
59 help=u"Release string of the product."
65 help=u"Calendar week when the report is published."
70 u"NOTSET", u"DEBUG", u"INFO", u"WARNING", u"ERROR", u"CRITICAL"
73 help=u"Logging level."
78 help=u"Force removing the old build(s) if present."
81 u"-o", u"--print-all-oper-data",
83 help=u"Print all operational data to console. Be careful, the output "
84 u"can be really long."
87 u"-i", u"--input-file",
90 help=u"XML file generated by RobotFramework which will be processed "
91 u"instead of downloading the data from Nexus and/or Jenkins. In "
92 u"this case, the section 'input' in the specification file is "
96 u"-d", u"--input-directory",
99 help=u"Directory with XML file(s) generated by RobotFramework or with "
100 u"sub-directories with XML file(s) which will be processed "
101 u"instead of downloading the data from Nexus and/or Jenkins. In "
102 u"this case, the section 'input' in the specification file is "
106 return parser.parse_args()
113 u"NOTSET": logging.NOTSET,
114 u"DEBUG": logging.DEBUG,
115 u"INFO": logging.INFO,
116 u"WARNING": logging.WARNING,
117 u"ERROR": logging.ERROR,
118 u"CRITICAL": logging.CRITICAL
123 format=u"%(asctime)s: %(levelname)s: %(message)s",
124 datefmt=u"%Y/%m/%d %H:%M:%S",
125 level=log_levels[args.logging]
128 logging.info(u"Application started.")
131 spec = Specification(args.specification)
132 spec.read_specification()
133 except PresentationError as err:
134 logging.critical(u"Finished with error.")
135 logging.critical(repr(err))
138 if spec.output[u"output"] not in OUTPUTS:
140 f"The output {spec.output[u'output']} is not supported."
146 env = Environment(spec.environment, args.force)
147 env.set_environment()
149 prepare_static_content(spec)
151 data = InputData(spec, spec.output[u"output"])
153 data.process_local_file(args.input_file)
154 elif args.input_directory:
155 data.process_local_directory(args.input_directory)
157 data.download_and_parse_data(repeat=1)
159 if args.print_all_oper_data:
160 data.print_all_oper_data()
162 generate_tables(spec, data)
163 generate_plots(spec, data)
164 generate_files(spec, data)
166 if spec.output[u"output"] == u"report":
167 generate_report(args.release, spec, args.week)
168 elif spec.output[u"output"] == u"trending":
169 sys.stdout.write(generate_cpta(spec, data))
171 alert = Alerting(spec)
172 alert.generate_alerts()
173 except AlertingError as err:
174 logging.warning(repr(err))
175 elif spec.output[u"output"] == u"convert-xml-to-json":
176 convert_xml_to_json(spec, data)
178 logging.info("No output will be generated.")
180 logging.info(u"Successfully finished.")
183 except AlertingError as err:
184 logging.critical(f"Finished with an alerting error.\n{repr(err)}")
185 except PresentationError as err:
186 logging.critical(f"Finished with a PAL error.\n{str(err)}")
187 except (KeyError, ValueError) as err:
188 logging.critical(f"Finished with an error.\n{repr(err)}")
191 clean_environment(spec.environment)
195 if __name__ == u"__main__":