1 # Copyright (c) 2021 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 pal_utils import get_files, execute_command, archive_input_data
25 # .css file for the html format of the report
26 THEME_OVERRIDES = u"""/* 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 = u'sphinx-build -v -c sphinx_conf/report -a ' \
93 u'-D release={release} ' \
94 u'-D version="Test Report {date}" ' \
98 # Command to build the pdf format of the report
99 PDF_BUILDER = u'sphinx-build -v -c sphinx_conf/report -a ' \
102 u'-D release={release} ' \
103 u'-D version="Test Report {date}" ' \
108 def generate_report(release, spec, report_week):
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_week: Calendar week when the report is published.
115 :type spec: Specification
116 :type report_week: str
119 logging.info(u"Generating the report ...")
122 u"html": generate_html_report,
123 u"pdf": generate_pdf_report
126 for report_format in spec.output[u"format"]:
127 report[report_format](release, spec, report_week)
129 archive_input_data(spec)
131 logging.info(u"Done.")
134 def generate_html_report(release, spec, 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 report_version: Version of the report.
141 :type spec: Specification
142 :type report_version: str
147 logging.info(u" Generating the html report, give me a few minutes, please "
150 working_dir = spec.environment[u"paths"][u"DIR[WORKING,SRC]"]
152 execute_command(f"cd {working_dir} && mv -f index.html.template index.rst")
154 cmd = HTML_BUILDER.format(
156 date=datetime.datetime.utcnow().strftime(u'%Y-%m-%d %H:%M UTC'),
157 working_dir=working_dir,
158 build_dir=spec.environment[u"paths"][u"DIR[BUILD,HTML]"])
161 with open(spec.environment[u"paths"][u"DIR[CSS_PATCH_FILE]"], u"wt") as \
163 css_file.write(THEME_OVERRIDES)
165 with open(spec.environment[u"paths"][u"DIR[CSS_PATCH_FILE2]"], u"wt") as \
167 css_file.write(THEME_OVERRIDES)
169 logging.info(u" Done.")
172 def generate_pdf_report(release, spec, report_week):
173 """Generate html format of the report.
175 :param release: Release string of the product.
176 :param spec: Specification read from the specification file.
177 :param report_week: Calendar week when the report is published.
179 :type spec: Specification
180 :type report_week: str
183 logging.info(u" Generating the pdf report, give me a few minutes, please "
186 working_dir = spec.environment[u"paths"][u"DIR[WORKING,SRC]"]
188 execute_command(f"cd {working_dir} && mv -f index.pdf.template index.rst")
190 _convert_all_svg_to_pdf(spec.environment[u"paths"][u"DIR[WORKING,SRC]"])
192 # Convert PyPLOT graphs in HTML format to PDF.
193 convert_plots = u"xvfb-run -a wkhtmltopdf {html} {pdf}"
194 plots = get_files(spec.environment[u"paths"][u"DIR[STATIC,VPP]"], u"html")
196 get_files(spec.environment[u"paths"][u"DIR[STATIC,DPDK]"], u"html")
200 file_name = f"{plot.rsplit(u'.', 1)[0]}.pdf"
201 pdf_plots.append(file_name)
202 logging.info(f"Converting {plot} to {file_name}")
203 execute_command(convert_plots.format(html=plot, pdf=file_name))
205 # Generate the LaTeX documentation
206 build_dir = spec.environment[u"paths"][u"DIR[BUILD,LATEX]"]
207 cmd = PDF_BUILDER.format(
209 date=datetime.datetime.utcnow().strftime(u'%Y-%m-%d %H:%M UTC'),
210 working_dir=working_dir,
214 # Build pdf documentation
215 archive_dir = spec.environment[u"paths"][u"DIR[STATIC,ARCH]"]
217 f'cd {build_dir} && '
218 f'pdflatex -shell-escape -interaction nonstopmode csit.tex || true',
219 f'cd {build_dir} && '
220 f'pdflatex -interaction nonstopmode csit.tex || true',
221 f'cd {build_dir} && '
222 f'cp csit.pdf ../{archive_dir}/csit_{release}.{report_week}.pdf &&'
223 f'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'
229 # Delete temporary pdf files:
230 for plot in pdf_plots:
231 execute_command(f"rm {plot}")
233 logging.info(u" Done.")
236 def archive_report(spec):
237 """Archive the report.
239 :param spec: Specification read from the specification file.
240 :type spec: Specification
243 logging.info(u" Archiving the report ...")
248 base_dir=spec.environment[u"paths"][u"DIR[BUILD,HTML]"]
251 logging.info(u" Done.")
254 def _convert_all_svg_to_pdf(path):
255 """Convert all svg files on path "path" to pdf.
257 :param path: Path to the root directory with svg files to convert.
261 svg_files = get_files(path, u"svg", full_path=True)
262 for svg_file in svg_files:
263 pdf_file = f"{svg_file.rsplit(u'.', 1)[0]}.pdf"
264 logging.info(f"Converting {svg_file} to {pdf_file}")
266 f"inkscape -D -z --file={svg_file} --export-pdf={pdf_file}"