dd70c70ce707492a339add20dc91f4a16dd34dac
[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
72     rst_include_table = ("\n.. only:: html\n\n"
73                          "    .. csv-table::\n"
74                          "        :header-rows: 1\n"
75                          "        :widths: auto\n"
76                          "        :align: center\n"
77                          "        :file: {file_html}\n"
78                          "\n.. only:: latex\n\n"
79                          "\n  .. raw:: latex\n\n"
80                          "      \csvautolongtable{{{file_latex}}}\n\n")
81
82     logging.info("  Generating the file {0} ...".format(file_name))
83
84     table_lst = get_files(file_spec["dir-tables"], ".csv", full_path=True)
85     if len(table_lst) == 0:
86         logging.error("  No tables to include in '{0}'. Skipping.".
87                       format(file_spec["dir-tables"]))
88         return None
89
90     job = file_spec["data"].keys()[0]
91     build = str(file_spec["data"][job][0])
92
93     logging.info("    Writing file '{0}'".format(file_name))
94
95     suites = input_data.suites(job, build)[file_spec["data-start-level"]:]
96     suites.sort_index(inplace=True)
97
98     with open(file_name, "w") as file_handler:
99         file_handler.write(rst_header)
100         for suite_longname, suite in suites.iteritems():
101             suite_name = suite["name"]
102             file_handler.write("\n{0}\n{1}\n".format(
103                 suite_name, get_rst_title_char(
104                     suite["level"] - file_spec["data-start-level"] - 1) *
105                             len(suite_name)))
106             file_handler.write("\n{0}\n".format(
107                 suite["doc"].replace('|br|', '\n\n -')))
108             if tests_in_suite(suite_name, input_data.tests(job, build)):
109                 for tbl_file in table_lst:
110                     if suite_name in tbl_file:
111                         file_handler.write(
112                             rst_include_table.format(
113                                 file_latex=tbl_file,
114                                 file_html=tbl_file.split("/")[-1]))
115
116     logging.info("  Done.")