PAL: Update Sphinx to the latest versions
[csit.git] / resources / tools / presentation / pal.py
1 # Copyright (c) 2019 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """CSIT Presentation and analytics layer.
15 """
16
17 import sys
18 import argparse
19 import logging
20
21 from pal_errors import PresentationError
22 from environment import Environment, clean_environment
23 from specification_parser import Specification
24 from input_data_parser import InputData
25 from generator_tables import generate_tables
26 from generator_plots import generate_plots
27 from generator_files import generate_files
28 from static_content import prepare_static_content
29 from generator_report import generate_report
30 from generator_cpta import generate_cpta
31 from generator_alerts import Alerting, AlertingError
32
33
34 def parse_args():
35     """Parse arguments from cmd line.
36
37     :returns: Parsed arguments.
38     :rtype: ArgumentParser
39     """
40
41     parser = argparse.ArgumentParser(
42         description=__doc__,
43         formatter_class=argparse.RawDescriptionHelpFormatter
44     )
45     parser.add_argument(
46         u"-s", u"--specification",
47         required=True,
48         type=argparse.FileType(u'r'),
49         help=u"Specification YAML file."
50     )
51     parser.add_argument(
52         u"-r", u"--release",
53         default=u"master",
54         type=str,
55         help=u"Release string of the product."
56     )
57     parser.add_argument(
58         u"-w", u"--week",
59         default=u"1",
60         type=str,
61         help=u"Calendar week when the report is published."
62     )
63     parser.add_argument(
64         u"-l", u"--logging",
65         choices=[u"DEBUG", u"INFO", u"WARNING", u"ERROR", u"CRITICAL"],
66         default=u"ERROR",
67         help=u"Logging level."
68     )
69     parser.add_argument(
70         u"-f", u"--force",
71         action=u"store_true",
72         help=u"Force removing the old build(s) if present."
73     )
74     parser.add_argument(
75         u"-o", u"--print-all-oper-data",
76         action=u"store_true",
77         help=u"Print all operational data to console. Be careful, the output "
78              u"can be really long."
79     )
80     parser.add_argument(
81         u"-i", u"--input-file",
82         type=str,
83         default=u"",
84         help=u"XML file generated by RobotFramework which will be processed "
85              u"instead of downloading the data from Nexus and/or Jenkins. In "
86              u"this case, the section 'input' in the specification file is "
87              u"ignored."
88     )
89     parser.add_argument(
90         u"-d", u"--input-directory",
91         type=str,
92         default=u"",
93         help=u"Directory with XML file(s) generated by RobotFramework or with "
94              u"sub-directories with XML file(s) which will be processed "
95              u"instead of downloading the data from Nexus and/or Jenkins. In "
96              u"this case, the section 'input' in the specification file is "
97              u"ignored."
98     )
99
100     return parser.parse_args()
101
102
103 def main():
104     """Main function."""
105
106     log_levels = {u"NOTSET": logging.NOTSET,
107                   u"DEBUG": logging.DEBUG,
108                   u"INFO": logging.INFO,
109                   u"WARNING": logging.WARNING,
110                   u"ERROR": logging.ERROR,
111                   u"CRITICAL": logging.CRITICAL}
112
113     args = parse_args()
114     logging.basicConfig(format=u"%(asctime)s: %(levelname)s: %(message)s",
115                         datefmt=u"%Y/%m/%d %H:%M:%S",
116                         level=log_levels[args.logging])
117
118     logging.info(u"Application started.")
119     try:
120         spec = Specification(args.specification)
121         spec.read_specification()
122     except PresentationError:
123         logging.critical(u"Finished with error.")
124         return 1
125
126     if spec.output[u"output"] not in (u"none", u"report", u"trending"):
127         logging.critical(
128             f"The output {spec.output[u'output']} is not supported."
129         )
130         return 1
131
132     ret_code = 1
133     try:
134         env = Environment(spec.environment, args.force)
135         env.set_environment()
136
137         prepare_static_content(spec)
138
139         data = InputData(spec)
140         if args.input_file:
141             data.process_local_file(args.input_file)
142         elif args.input_directory:
143             data.process_local_directory(args.input_directory)
144         else:
145             data.download_and_parse_data(repeat=1)
146
147         if args.print_all_oper_data:
148             data.print_all_oper_data()
149
150         generate_tables(spec, data)
151         generate_plots(spec, data)
152         generate_files(spec, data)
153
154         if spec.output[u"output"] == u"report":
155             generate_report(args.release, spec, args.week)
156         elif spec.output[u"output"] == u"trending":
157             sys.stdout.write(generate_cpta(spec, data))
158             try:
159                 alert = Alerting(spec)
160                 alert.generate_alerts()
161             except AlertingError as err:
162                 logging.warning(repr(err))
163         else:
164             logging.info("No output will be generated.")
165
166         logging.info(u"Successfully finished.")
167         ret_code = 0
168
169     except AlertingError as err:
170         logging.critical(f"Finished with an alerting error.\n{repr(err)}")
171     except PresentationError as err:
172         logging.critical(f"Finished with a PAL error.\n{str(err)}")
173     except (KeyError, ValueError) as err:
174         logging.critical(f"Finished with an error.\n{repr(err)}")
175     finally:
176         if spec is not None:
177             clean_environment(spec.environment)
178     return ret_code
179
180
181 if __name__ == u"__main__":
182     sys.exit(main())