CSIT-755: Presentation and analytics layer
[csit.git] / resources / tools / presentation / generator_files.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 """Algorithms to generate files.
15 """
16
17
18 import logging
19
20 from utils import get_files, get_rst_title_char
21
22
23 def generate_files(spec, data):
24     """Generate all files specified in the specification file.
25
26     :param spec: Specification read from the specification file.
27     :param data: Data to process.
28     :type spec: Specification
29     :type data: InputData
30     """
31
32     logging.info("Generating the files ...")
33     for file_spec in spec.files:
34         try:
35             eval(file_spec["algorithm"])(file_spec, data)
36         except NameError:
37             logging.error("The algorithm '{0}' is not defined.".
38                           format(file_spec["algorithm"]))
39     logging.info("Done.")
40
41
42 def file_test_results(file_spec, input_data):
43     """Generate the file(s) with algorithm: file_test_results specified in the
44     specification file.
45
46     :param file_spec: File to generate.
47     :param input_data: Data to process.
48     :type file_spec: pandas.Series
49     :type input_data: InputData
50     """
51
52     def tests_in_suite(suite_name, tests):
53         """Check if the suite includes tests.
54
55         :param suite_name: Name of the suite to be checked.
56         :param tests: Set of tests
57         :type suite_name: str
58         :type tests: pandas.Series
59         :returns: True if the suite includes tests.
60         :rtype: bool
61         """
62
63         for key in tests.keys():
64             if suite_name == tests[key]["parent"]:
65                 return True
66         return False
67
68     file_name = "{0}{1}".format(file_spec["output-file"],
69                                 file_spec["output-file-ext"])
70     rst_header = file_spec["file-header"]
71     rst_include_table = ("\n.. csv-table::\n"
72                          "    :header-rows: 1\n"
73                          "    :widths: auto\n"
74                          "    :align: center\n"
75                          "    :file: {file}\n\n")
76
77     logging.info("  Generating the file {0} ...".format(file_name))
78
79     table_lst = get_files(file_spec["dir-tables"], ".csv", full_path=False)
80     if len(table_lst) == 0:
81         logging.error("  No tables to include in '{0}'. Skipping.".
82                       format(file_spec["dir-tables"]))
83         return None
84
85     job = file_spec["data"].keys()[0]
86     build = str(file_spec["data"][job][0])
87
88     logging.info("    Writing file '{0}'".format(file_name))
89
90     suites = input_data.suites(job, build)[file_spec["data-start-level"]:]
91     suites.sort_index(inplace=True)
92
93     with open(file_name, "w") as file_handler:
94         file_handler.write(rst_header)
95         for suite_longname, suite in suites.iteritems():
96             suite_name = suite["name"]
97             file_handler.write("\n{0}\n{1}\n".format(
98                 suite_name, get_rst_title_char(
99                     suite["level"] - file_spec["data-start-level"] - 1) *
100                             len(suite_name)))
101             file_handler.write("\n{0}\n".format(
102                 suite["doc"].replace('|br|', '\n\n -')))
103             if tests_in_suite(suite_name, input_data.tests(job, build)):
104                 for tbl_file in table_lst:
105                     if suite_name in tbl_file:
106                         file_handler.write(rst_include_table.
107                                            format(file=tbl_file))
108
109     logging.info("  Done.")