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"]
72 rst_include_table = ("\n.. only:: html\n\n"
77 " :file: {file_html}\n"
78 "\n.. only:: latex\n\n"
79 "\n .. raw:: latex\n\n"
80 " \csvautolongtable{{{file_latex}}}\n\n")
82 logging.info(" Generating the file {0} ...".format(file_name))
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"]))
90 job = file_spec["data"].keys()[0]
91 build = str(file_spec["data"][job][0])
93 logging.info(" Writing file '{0}'".format(file_name))
95 suites = input_data.suites(job, build)[file_spec["data-start-level"]:]
96 suites.sort_index(inplace=True)
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) *
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:
112 rst_include_table.format(
114 file_html=tbl_file.split("/")[-1]))
116 logging.info(" Done.")