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