Report: Detect outliers for the comparative tables
[csit.git] / resources / tools / presentation / pal.py
1 # Copyright (c) 2018 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_files import download_data_files, unzip_files
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 static_content import prepare_static_content
30 from generator_report import generate_report
31 from generator_CPTA import generate_cpta
32
33 from pprint import pprint
34
35
36 def parse_args():
37     """Parse arguments from cmd line.
38
39     :returns: Parsed arguments.
40     :rtype: ArgumentParser
41     """
42
43     parser = argparse.ArgumentParser(description=__doc__,
44                                      formatter_class=argparse.
45                                      RawDescriptionHelpFormatter)
46     parser.add_argument("-s", "--specification",
47                         required=True,
48                         type=argparse.FileType('r'),
49                         help="Specification YAML file.")
50     parser.add_argument("-r", "--release",
51                         default="master",
52                         type=str,
53                         help="Release string of the product.")
54     parser.add_argument("-l", "--logging",
55                         choices=["DEBUG", "INFO", "WARNING",
56                                  "ERROR", "CRITICAL"],
57                         default="ERROR",
58                         help="Logging level.")
59     parser.add_argument("-f", "--force",
60                         action='store_true',
61                         help="Force removing the old build(s) if present.")
62
63     return parser.parse_args()
64
65
66 def main():
67     """Main function."""
68
69     log_levels = {"NOTSET": logging.NOTSET,
70                   "DEBUG": logging.DEBUG,
71                   "INFO": logging.INFO,
72                   "WARNING": logging.WARNING,
73                   "ERROR": logging.ERROR,
74                   "CRITICAL": logging.CRITICAL}
75
76     args = parse_args()
77     logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
78                         datefmt='%Y/%m/%d %H:%M:%S',
79                         level=log_levels[args.logging])
80
81     logging.info("Application started.")
82     try:
83         spec = Specification(args.specification)
84         spec.read_specification()
85     except PresentationError:
86         logging.critical("Finished with error.")
87         return 1
88
89     ret_code = 0
90     try:
91         env = Environment(spec.environment, args.force)
92         env.set_environment()
93
94         if spec.is_debug:
95             if spec.debug["input-format"] == "zip":
96                 unzip_files(spec)
97         else:
98             download_data_files(spec)
99
100         prepare_static_content(spec)
101
102         data = InputData(spec)
103         data.read_data()
104
105         generate_tables(spec, data)
106         generate_plots(spec, data)
107         generate_files(spec, data)
108
109         if spec.output["output"] == "report":
110             generate_report(args.release, spec)
111             logging.info("Successfully finished.")
112         elif spec.output["output"] == "CPTA":
113             sys.stdout.write(generate_cpta(spec, data))
114             logging.info("Successfully finished.")
115         else:
116             logging.critical("The output '{0}' is not supported.".
117                              format(spec.output["output"]))
118             ret_code = 1
119
120     except (KeyError, ValueError, PresentationError) as err:
121         logging.info("Finished with an error.")
122         logging.critical(str(err))
123         ret_code = 1
124     except Exception as err:
125         logging.info("Finished with an unexpected error.")
126         logging.critical(str(err))
127         ret_code = 1
128     finally:
129         if spec is not None and not spec.is_debug:
130             clean_environment(spec.environment)
131         return ret_code
132
133
134 if __name__ == '__main__':
135     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.