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:
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.
20 from shutil import make_archive
22 from utils import get_files, execute_command, archive_input_data
25 # .css file for the html format of the report
26 THEME_OVERRIDES = """/* override table width restrictions */
27 @media screen and (min-width: 767px) {
28 .wy-table-responsive table td, .wy-table-responsive table th {
29 white-space: normal !important;
32 .wy-table-responsive {
36 overflow: visible !important;
39 .rst-content blockquote {
45 display: inline-block;
53 .wy-menu-vertical li.current a {
55 border-right: solid 1px #c9c9c9;
58 .wy-menu-vertical li.toctree-l2.current > a {
62 .wy-menu-vertical li.toctree-l2.current li.toctree-l3 > a {
67 .wy-menu-vertical li.toctree-l3.current li.toctree-l4 > a {
72 .wy-menu-vertical li.on a, .wy-menu-vertical li.current > a {
79 border-top-width: medium;
80 border-bottom-width: medium;
81 border-top-style: none;
82 border-bottom-style: none;
83 border-top-color: currentcolor;
84 border-bottom-color: currentcolor;
85 padding-left: 2em -4px;
89 # Command to build the html format of the report
90 HTML_BUILDER = 'sphinx-build -v -c . -a ' \
93 '-D release={release} ' \
94 '-D version="Test Report {date}" ' \
98 # Command to build the pdf format of the report
99 PDF_BUILDER = 'sphinx-build -v -c . -a ' \
102 '-D release={release} ' \
103 '-D version="Test Report {date}" ' \
108 def generate_report(release, spec, report_version):
109 """Generate all formats and versions of the report.
111 :param release: Release string of the product.
112 :param spec: Specification read from the specification file.
113 :param report_version: Version of the report.
115 :type spec: Specification
116 :type report_version: str
119 logging.info("Generating the report ...")
122 "html": generate_html_report,
123 "pdf": generate_pdf_report
126 for report_format, versions in spec.output["format"].items():
127 report[report_format](release, spec, versions, report_version)
129 archive_input_data(spec)
131 logging.info("Done.")
134 def generate_html_report(release, spec, versions, report_version):
135 """Generate html format of the report.
137 :param release: Release string of the product.
138 :param spec: Specification read from the specification file.
139 :param versions: List of versions to generate.
140 :param report_version: Version of the report.
142 :type spec: Specification
144 :type report_version: str
147 logging.info(" Generating the html report, give me a few minutes, please "
150 working_dir = spec.environment["paths"]["DIR[WORKING,SRC]"]
152 cmd = 'cd {working_dir} && mv -f index.html.template index.rst'.\
153 format(working_dir=working_dir)
156 cmd = HTML_BUILDER.format(
158 report_version=report_version,
159 date=datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M UTC'),
160 working_dir=working_dir,
161 build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
164 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
166 css_file.write(THEME_OVERRIDES)
168 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
170 css_file.write(THEME_OVERRIDES)
172 logging.info(" Done.")
175 def generate_pdf_report(release, spec, versions, report_version):
176 """Generate html format of the report.
178 :param release: Release string of the product.
179 :param spec: Specification read from the specification file.
180 :param versions: List of versions to generate. Not implemented yet.
181 :param report_version: Version of the report.
183 :type spec: Specification
185 :type report_version: str
188 logging.info(" Generating the pdf report, give me a few minutes, please "
191 working_dir = spec.environment["paths"]["DIR[WORKING,SRC]"]
193 cmd = 'cd {working_dir} && mv -f index.pdf.template index.rst'.\
194 format(working_dir=working_dir)
197 _convert_all_svg_to_pdf(spec.environment["paths"]["DIR[WORKING,SRC]"])
199 # Convert PyPLOT graphs in HTML format to PDF.
200 convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}"
201 plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
202 plots.extend(get_files(spec.environment["paths"]["DIR[STATIC,DPDK]"],
205 file_name = "{0}.pdf".format(plot.rsplit(".", 1)[0])
206 logging.info("Converting '{0}' to '{1}'".format(plot, file_name))
207 execute_command(convert_plots.format(html=plot, pdf=file_name))
209 # Generate the LaTeX documentation
210 build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"]
211 cmd = PDF_BUILDER.format(
213 report_version=report_version,
214 date=datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M UTC'),
215 working_dir=working_dir,
219 # Build pdf documentation
220 archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"]
223 'pdflatex -shell-escape -interaction nonstopmode csit.tex || true'.
224 format(build_dir=build_dir),
226 'pdflatex -interaction nonstopmode csit.tex || true'.
227 format(build_dir=build_dir),
229 'cp csit.pdf ../{archive_dir}/csit_{release}_{report_version}.pdf &&'
230 'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'.
231 format(build_dir=build_dir,
232 archive_dir=archive_dir,
234 report_version=report_version)
240 logging.info(" Done.")
243 def archive_report(spec):
244 """Archive the report.
246 :param spec: Specification read from the specification file.
247 :type spec: Specification
250 logging.info(" Archiving the report ...")
252 make_archive("csit.report",
254 base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
256 logging.info(" Done.")
259 def _convert_all_svg_to_pdf(path):
260 """Convert all svg files on path "path" to pdf.
262 :param path: Path to the root directory with svg files to convert.
266 cmd = "inkscape -D -z --file={svg} --export-pdf={pdf}"
268 svg_files = get_files(path, "svg", full_path=True)
269 for svg_file in svg_files:
270 pdf_file = "{0}.pdf".format(svg_file.rsplit('.', 1)[0])
271 logging.info("Converting '{0}' to '{1}'".format(svg_file, pdf_file))
272 execute_command(cmd.format(svg=svg_file, pdf=pdf_file))