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
23 def generate_files(spec, data):
24 """Generate all files specified in the specification file.
26 :param spec: Specification read from the specification file.
27 :param data: Data to process.
28 :type spec: Specification
32 logging.info("Generating the files ...")
33 for file_spec in spec.files:
35 eval(file_spec["algorithm"])(file_spec, data)
37 logging.error("The algorithm '{0}' is not defined.".
38 format(file_spec["algorithm"]))
42 def file_test_results(file_spec, input_data):
43 """Generate the file(s) with algorithm: file_test_results specified in the
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
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"]:
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"
77 logging.info(" Generating the file {0} ...".format(file_name))
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"]))
85 job = file_spec["data"].keys()[0]
86 build = str(file_spec["data"][job][0])
88 logging.info(" Writing file '{0}'".format(file_name))
90 suites = input_data.suites(job, build)[file_spec["data-start-level"]:]
91 suites.sort_index(inplace=True)
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) *
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))
109 logging.info(" Done.")