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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Algorithms to generate files.
20 from utils import get_files, get_rst_title_char
22 RST_INCLUDE_TABLE = ("\n.. only:: html\n\n"
27 " :file: {file_html}\n"
28 "\n.. only:: latex\n\n"
29 "\n .. raw:: latex\n\n"
30 " \csvautolongtable{{{file_latex}}}\n\n")
33 def generate_files(spec, data):
34 """Generate all files specified in the specification file.
36 :param spec: Specification read from the specification file.
37 :param data: Data to process.
38 :type spec: Specification
42 logging.info("Generating the files ...")
43 for file_spec in spec.files:
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))
52 def _tests_in_suite(suite_name, tests):
53 """Check if the suite includes tests.
55 :param suite_name: Name of the suite to be checked.
56 :param tests: Set of tests
58 :type tests: pandas.Series
59 :returns: True if the suite includes tests.
63 for key in tests.keys():
64 if suite_name == tests[key]["parent"]:
69 def file_test_results(file_spec, input_data):
70 """Generate the file(s) with algorithm: file_test_results specified in the
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
79 file_name = "{0}{1}".format(file_spec["output-file"],
80 file_spec["output-file-ext"])
81 rst_header = file_spec["file-header"]
83 logging.info(" Generating the file {0} ...".format(file_name))
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"]))
91 job = file_spec["data"].keys()[0]
92 build = str(file_spec["data"][job][0])
94 logging.info(" Writing file '{0}'".format(file_name))
96 suites = input_data.suites(job, build)[file_spec["data-start-level"]:]
97 suites.sort_index(inplace=True)
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) *
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:
113 RST_INCLUDE_TABLE.format(
115 file_html=tbl_file.split("/")[-1]))
117 logging.info(" Done.")
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.
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
130 file_name = "{0}{1}".format(file_spec["output-file"],
131 file_spec["output-file-ext"])
132 rst_header = file_spec["file-header"]
134 logging.info(" Generating the file {0} ...".format(file_name))
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"]))
142 logging.info(" Writing file '{0}'".format(file_name))
144 logging.info(" Creating the data set for the {0} '{1}'.".
145 format(file_spec.get("type", ""), file_spec.get("title", "")))
146 tests = input_data.filter_data(file_spec)
147 tests = input_data.merge_data(tests)
149 logging.info(" Creating the data set for the {0} '{1}'.".
150 format(file_spec.get("type", ""), file_spec.get("title", "")))
151 suites = input_data.filter_data(file_spec, data_set="suites")
152 suites = input_data.merge_data(suites)
153 suites.sort_index(inplace=True)
155 with open(file_name, "w") as file_handler:
156 file_handler.write(rst_header)
157 for suite_longname, suite in suites.iteritems():
158 if "ndrchk" in suite_longname or "pdrchk" in suite_longname:
160 if len(suite_longname.split(".")) <= file_spec["data-start-level"]:
162 suite_name = suite["name"]
163 file_handler.write("\n{0}\n{1}\n".format(
164 suite_name, get_rst_title_char(
165 suite["level"] - file_spec["data-start-level"] - 1) *
167 file_handler.write("\n{0}\n".format(
168 suite["doc"].replace('|br|', '\n\n -')))
169 if _tests_in_suite(suite_name, tests):
170 for tbl_file in table_lst:
171 if suite_name in tbl_file:
173 RST_INCLUDE_TABLE.format(
175 file_html=tbl_file.split("/")[-1]))
177 logging.info(" Done.")