CSIT-1483: Add processing of output_info.xml as the first choice
[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 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(description=__doc__,
42                                      formatter_class=argparse.
43                                      RawDescriptionHelpFormatter)
44     parser.add_argument("-s", "--specification",
45                         required=True,
46                         type=argparse.FileType('r'),
47                         help="Specification YAML file.")
48     parser.add_argument("-r", "--release",
49                         default="master",
50                         type=str,
51                         help="Release string of the product.")
52     parser.add_argument("-w", "--week",
53                         default="1",
54                         type=str,
55                         help="Calendar week when the report is published.")
56     parser.add_argument("-l", "--logging",
57                         choices=["DEBUG", "INFO", "WARNING",
58                                  "ERROR", "CRITICAL"],
59                         default="ERROR",
60                         help="Logging level.")
61     parser.add_argument("-f", "--force",
62                         action='store_true',
63                         help="Force removing the old build(s) if present.")
64
65     return parser.parse_args()
66
67
68 def main():
69     """Main function."""
70
71     log_levels = {"NOTSET": logging.NOTSET,
72                   "DEBUG": logging.DEBUG,
73                   "INFO": logging.INFO,
74                   "WARNING": logging.WARNING,
75                   "ERROR": logging.ERROR,
76                   "CRITICAL": logging.CRITICAL}
77
78     args = parse_args()
79     logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
80                         datefmt='%Y/%m/%d %H:%M:%S',
81                         level=log_levels[args.logging])
82
83     logging.info("Application started.")
84     try:
85         spec = Specification(args.specification)
86         spec.read_specification()
87     except PresentationError:
88         logging.critical("Finished with error.")
89         return 1
90
91     if spec.output["output"] not in ("report", "CPTA"):
92         logging.critical("The output '{0}' is not supported.".
93                          format(spec.output["output"]))
94         return 1
95
96     ret_code = 1
97     try:
98         env = Environment(spec.environment, args.force)
99         env.set_environment()
100
101         prepare_static_content(spec)
102
103         data = InputData(spec)
104         data.download_and_parse_data(repeat=1)
105
106         generate_tables(spec, data)
107         generate_plots(spec, data)
108         generate_files(spec, data)
109
110         if spec.output["output"] == "report":
111             generate_report(args.release, spec, args.week)
112             logging.info("Successfully finished.")
113         elif spec.output["output"] == "CPTA":
114             sys.stdout.write(generate_cpta(spec, data))
115             try:
116                 alert = Alerting(spec)
117                 alert.generate_alerts()
118             except AlertingError as err:
119                 logging.warning(repr(err))
120             logging.info("Successfully finished.")
121         ret_code = 0
122
123     except AlertingError as err:
124         logging.critical("Finished with an alerting error.")
125         logging.critical(repr(err))
126     except PresentationError as err:
127         logging.critical("Finished with an PAL error.")
128         logging.critical(repr(err))
129     except (KeyError, ValueError) as err:
130         logging.critical("Finished with an error.")
131         logging.critical(repr(err))
132     except Exception as err:
133         logging.critical("Finished with an unexpected error.")
134         logging.critical(repr(err))
135     finally:
136         if spec is not None:
137             clean_environment(spec.environment)
138         return ret_code
139
140
141 if __name__ == '__main__':
142     sys.exit(main())

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.