PAL: Remove temporary pdf files
[csit.git] / resources / tools / presentation / generator_report.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Report generation.
15 """
16
17 import logging
18 import datetime
19
20 from shutil import make_archive
21
22 from pal_utils import get_files, execute_command, archive_input_data
23
24
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;
30     }
31
32     .wy-table-responsive {
33         font-size: small;
34         margin-bottom: 24px;
35         max-width: 100%;
36         overflow: visible !important;
37     }
38 }
39 .rst-content blockquote {
40     margin-left: 0px;
41     line-height: 18px;
42     margin-bottom: 0px;
43 }
44 .wy-menu-vertical a {
45     display: inline-block;
46     line-height: 18px;
47     padding: 0 2em;
48     display: block;
49     position: relative;
50     font-size: 90%;
51     color: #d9d9d9
52 }
53 .wy-menu-vertical li.current a {
54     color: gray;
55     border-right: solid 1px #c9c9c9;
56     padding: 0 3em;
57 }
58 .wy-menu-vertical li.toctree-l2.current > a {
59     background: #c9c9c9;
60     padding: 0 3em;
61 }
62 .wy-menu-vertical li.toctree-l2.current li.toctree-l3 > a {
63     display: block;
64     background: #c9c9c9;
65     padding: 0 4em;
66 }
67 .wy-menu-vertical li.toctree-l3.current li.toctree-l4 > a {
68     display: block;
69     background: #bdbdbd;
70     padding: 0 5em;
71 }
72 .wy-menu-vertical li.on a, .wy-menu-vertical li.current > a {
73     color: #404040;
74     padding: 0 2em;
75     font-weight: bold;
76     position: relative;
77     background: #fcfcfc;
78     border: none;
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;
86 }
87 """
88
89 # Command to build the html format of the report
90 HTML_BUILDER = u'sphinx-build -v -c . -a ' \
91                u'-b html -E ' \
92                u'-t html ' \
93                u'-D release={release} ' \
94                u'-D version="Test Report {date}" ' \
95                u'{working_dir} ' \
96                u'{build_dir}/'
97
98 # Command to build the pdf format of the report
99 PDF_BUILDER = u'sphinx-build -v -c . -a ' \
100               u'-b latex -E ' \
101               u'-t latex ' \
102               u'-D release={release} ' \
103               u'-D version="Test Report {date}" ' \
104               u'{working_dir} ' \
105               u'{build_dir}'
106
107
108 def generate_report(release, spec, report_week):
109     """Generate all formats and versions of the report.
110
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.
114     :type release: str
115     :type spec: Specification
116     :type report_week: str
117     """
118
119     logging.info(u"Generating the report ...")
120
121     report = {
122         u"html": generate_html_report,
123         u"pdf": generate_pdf_report
124     }
125
126     for report_format in spec.output[u"format"]:
127         report[report_format](release, spec, report_week)
128
129     archive_input_data(spec)
130
131     logging.info(u"Done.")
132
133
134 def generate_html_report(release, spec, report_version):
135     """Generate html format of the report.
136
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.
140     :type release: str
141     :type spec: Specification
142     :type report_version: str
143     """
144
145     _ = report_version
146
147     logging.info(u"  Generating the html report, give me a few minutes, please "
148                  u"...")
149
150     working_dir = spec.environment[u"paths"][u"DIR[WORKING,SRC]"]
151
152     execute_command(f"cd {working_dir} && mv -f index.html.template index.rst")
153
154     cmd = HTML_BUILDER.format(
155         release=release,
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]"])
159     execute_command(cmd)
160
161     with open(spec.environment[u"paths"][u"DIR[CSS_PATCH_FILE]"], u"wt") as \
162             css_file:
163         css_file.write(THEME_OVERRIDES)
164
165     with open(spec.environment[u"paths"][u"DIR[CSS_PATCH_FILE2]"], u"wt") as \
166             css_file:
167         css_file.write(THEME_OVERRIDES)
168
169     logging.info(u"  Done.")
170
171
172 def generate_pdf_report(release, spec, report_week):
173     """Generate html format of the report.
174
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.
178     :type release: str
179     :type spec: Specification
180     :type report_week: str
181     """
182
183     logging.info(u"  Generating the pdf report, give me a few minutes, please "
184                  u"...")
185
186     working_dir = spec.environment[u"paths"][u"DIR[WORKING,SRC]"]
187
188     execute_command(f"cd {working_dir} && mv -f index.pdf.template index.rst")
189
190     _convert_all_svg_to_pdf(spec.environment[u"paths"][u"DIR[WORKING,SRC]"])
191
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")
195     plots.extend(
196         get_files(spec.environment[u"paths"][u"DIR[STATIC,DPDK]"], u"html")
197     )
198     pdf_plots = list()
199     for plot in plots:
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))
204
205     # Generate the LaTeX documentation
206     build_dir = spec.environment[u"paths"][u"DIR[BUILD,LATEX]"]
207     cmd = PDF_BUILDER.format(
208         release=release,
209         date=datetime.datetime.utcnow().strftime(u'%Y-%m-%d %H:%M UTC'),
210         working_dir=working_dir,
211         build_dir=build_dir)
212     execute_command(cmd)
213
214     # Build pdf documentation
215     archive_dir = spec.environment[u"paths"][u"DIR[STATIC,ARCH]"]
216     cmds = [
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'
224     ]
225
226     for cmd in cmds:
227         execute_command(cmd)
228
229     # Delete temporary pdf files:
230     for plot in pdf_plots:
231         execute_command(f"rm {plot}")
232
233     logging.info(u"  Done.")
234
235
236 def archive_report(spec):
237     """Archive the report.
238
239     :param spec: Specification read from the specification file.
240     :type spec: Specification
241     """
242
243     logging.info(u"  Archiving the report ...")
244
245     make_archive(
246         u"csit.report",
247         u"gztar",
248         base_dir=spec.environment[u"paths"][u"DIR[BUILD,HTML]"]
249     )
250
251     logging.info(u"  Done.")
252
253
254 def _convert_all_svg_to_pdf(path):
255     """Convert all svg files on path "path" to pdf.
256
257     :param path: Path to the root directory with svg files to convert.
258     :type path: str
259     """
260
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}")
265         execute_command(
266             f"inkscape -D -z --file={svg_file} --export-pdf={pdf_file}"
267         )