e2bcf7835dbc10cdcd2ab5c7ab06b62c1eca6896
[csit.git] / resources / tools / presentation / generator_files.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 """Algorithms to generate files.
15 """
16
17
18 import logging
19
20 from utils import get_files, get_rst_title_char
21
22 RST_INCLUDE_TABLE = ("\n.. only:: html\n\n"
23                      "    .. csv-table::\n"
24                      "        :header-rows: 1\n"
25                      "        :widths: auto\n"
26                      "        :align: center\n"
27                      "        :file: {file_html}\n"
28                      "\n.. only:: latex\n\n"
29                      "\n  .. raw:: latex\n\n"
30                      "      \csvautolongtable{{{file_latex}}}\n\n")
31
32
33 def generate_files(spec, data):
34     """Generate all files specified in the specification file.
35
36     :param spec: Specification read from the specification file.
37     :param data: Data to process.
38     :type spec: Specification
39     :type data: InputData
40     """
41
42     logging.info("Generating the files ...")
43     for file_spec in spec.files:
44         try:
45             eval(file_spec["algorithm"])(file_spec, data)
46         except NameError as err:
47             logging.error("Probably algorithm '{alg}' is not defined: {err}".
48                           format(alg=file_spec["algorithm"], err=repr(err)))
49     logging.info("Done.")
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
69 def file_test_results(file_spec, input_data):
70     """Generate the file(s) with algorithms
71     - file_test_results
72     specified in the specification file.
73
74     :param file_spec: File to generate.
75     :param input_data: Data to process.
76     :type file_spec: pandas.Series
77     :type input_data: InputData
78     """
79
80     file_name = "{0}{1}".format(file_spec["output-file"],
81                                 file_spec["output-file-ext"])
82     rst_header = file_spec["file-header"]
83
84     logging.info("  Generating the file {0} ...".format(file_name))
85
86     table_lst = get_files(file_spec["dir-tables"], ".csv", full_path=True)
87     if len(table_lst) == 0:
88         logging.error("  No tables to include in '{0}'. Skipping.".
89                       format(file_spec["dir-tables"]))
90         return None
91
92     logging.info("    Writing file '{0}'".format(file_name))
93
94     logging.info("    Creating the 'tests' data set for the {0} '{1}'.".
95                  format(file_spec.get("type", ""), file_spec.get("title", "")))
96     tests = input_data.filter_data(file_spec)
97     tests = input_data.merge_data(tests)
98
99     logging.info("    Creating the 'suites' data set for the {0} '{1}'.".
100                  format(file_spec.get("type", ""), file_spec.get("title", "")))
101     file_spec["filter"] = "all"
102     suites = input_data.filter_data(file_spec, data_set="suites")
103     suites = input_data.merge_data(suites)
104     suites.sort_index(inplace=True)
105
106     with open(file_name, "w") as file_handler:
107         file_handler.write(rst_header)
108         for suite_longname, suite in suites.iteritems():
109             # TODO: Remove when NDRPDRDISC tests are not used:
110             if "ndrchk" in suite_longname or "pdrchk" in suite_longname:
111                 continue
112             if len(suite_longname.split(".")) <= file_spec["data-start-level"]:
113                 continue
114             file_handler.write("\n{0}\n{1}\n".format(
115                 suite["name"], get_rst_title_char(
116                     suite["level"] - file_spec["data-start-level"] - 1) *
117                             len(suite["name"])))
118             file_handler.write("\n{0}\n".format(
119                 suite["doc"].replace('|br|', '\n\n -')))
120             if _tests_in_suite(suite["name"], tests):
121                 for tbl_file in table_lst:
122                     if suite["name"] in tbl_file:
123                         file_handler.write(
124                             RST_INCLUDE_TABLE.format(
125                                 file_latex=tbl_file,
126                                 file_html=tbl_file.split("/")[-1]))
127     logging.info("  Done.")