Report: Remove *CHK tests from report
[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 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:
47             logging.error("The algorithm '{0}' is not defined.".
48                           format(file_spec["algorithm"]))
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 algorithm: file_test_results specified in the
71     specification file.
72
73     :param file_spec: File to generate.
74     :param input_data: Data to process.
75     :type file_spec: pandas.Series
76     :type input_data: InputData
77     """
78
79     file_name = "{0}{1}".format(file_spec["output-file"],
80                                 file_spec["output-file-ext"])
81     rst_header = file_spec["file-header"]
82
83     logging.info("  Generating the file {0} ...".format(file_name))
84
85     table_lst = get_files(file_spec["dir-tables"], ".csv", full_path=True)
86     if len(table_lst) == 0:
87         logging.error("  No tables to include in '{0}'. Skipping.".
88                       format(file_spec["dir-tables"]))
89         return None
90
91     job = file_spec["data"].keys()[0]
92     build = str(file_spec["data"][job][0])
93
94     logging.info("    Writing file '{0}'".format(file_name))
95
96     suites = input_data.suites(job, build)[file_spec["data-start-level"]:]
97     suites.sort_index(inplace=True)
98
99     with open(file_name, "w") as file_handler:
100         file_handler.write(rst_header)
101         for suite_longname, suite in suites.iteritems():
102             suite_name = suite["name"]
103             file_handler.write("\n{0}\n{1}\n".format(
104                 suite_name, get_rst_title_char(
105                     suite["level"] - file_spec["data-start-level"] - 1) *
106                             len(suite_name)))
107             file_handler.write("\n{0}\n".format(
108                 suite["doc"].replace('|br|', '\n\n -')))
109             if _tests_in_suite(suite_name, input_data.tests(job, build)):
110                 for tbl_file in table_lst:
111                     if suite_name in tbl_file:
112                         file_handler.write(
113                             RST_INCLUDE_TABLE.format(
114                                 file_latex=tbl_file,
115                                 file_html=tbl_file.split("/")[-1]))
116
117     logging.info("  Done.")
118
119
120 def file_merged_test_results(file_spec, input_data):
121     """Generate the file(s) with algorithm: file_merged_test_results specified
122     in the specification file.
123
124     :param file_spec: File to generate.
125     :param input_data: Data to process.
126     :type file_spec: pandas.Series
127     :type input_data: InputData
128     """
129
130     file_name = "{0}{1}".format(file_spec["output-file"],
131                                 file_spec["output-file-ext"])
132     rst_header = file_spec["file-header"]
133
134     logging.info("  Generating the file {0} ...".format(file_name))
135
136     table_lst = get_files(file_spec["dir-tables"], ".csv", full_path=True)
137     if len(table_lst) == 0:
138         logging.error("  No tables to include in '{0}'. Skipping.".
139                       format(file_spec["dir-tables"]))
140         return None
141
142     logging.info("    Writing file '{0}'".format(file_name))
143
144     tests = input_data.filter_data(file_spec)
145     tests = input_data.merge_data(tests)
146
147     suites = input_data.filter_data(file_spec, data_set="suites")
148     suites = input_data.merge_data(suites)
149     suites.sort_index(inplace=True)
150
151     with open(file_name, "w") as file_handler:
152         file_handler.write(rst_header)
153         for suite_longname, suite in suites.iteritems():
154             if "ndrchk" in suite_longname or "pdrchk" in suite_longname:
155                 continue
156             if len(suite_longname.split(".")) <= file_spec["data-start-level"]:
157                 continue
158             suite_name = suite["name"]
159             file_handler.write("\n{0}\n{1}\n".format(
160                 suite_name, get_rst_title_char(
161                     suite["level"] - file_spec["data-start-level"] - 1) *
162                             len(suite_name)))
163             file_handler.write("\n{0}\n".format(
164                 suite["doc"].replace('|br|', '\n\n -')))
165             if _tests_in_suite(suite_name, tests):
166                 for tbl_file in table_lst:
167                     if suite_name in tbl_file:
168                         file_handler.write(
169                             RST_INCLUDE_TABLE.format(
170                                 file_latex=tbl_file,
171                                 file_html=tbl_file.split("/")[-1]))
172
173     logging.info("  Done.")

©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.