PAL: Change date format according to iso8601
[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 utils import get_files, execute_command, archive_input_data
23
24
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;
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 = 'sphinx-build -v -c . -a ' \
91                '-b html -E ' \
92                '-t html ' \
93                '-D release={release} ' \
94                '-D version="Report v{report_version} - {date}" ' \
95                '{working_dir} ' \
96                '{build_dir}/'
97
98 # Command to build the pdf format of the report
99 PDF_BUILDER = 'sphinx-build -v -c . -a ' \
100               '-b latex -E ' \
101               '-t latex ' \
102               '-D release={release} ' \
103               '-D version="Report v{report_version} - {date}" ' \
104               '{working_dir} ' \
105               '{build_dir}'
106
107
108 def generate_report(release, spec, report_version):
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_version: Version of the report.
114     :type release: str
115     :type spec: Specification
116     :type report_version: str
117     """
118
119     logging.info("Generating the report ...")
120
121     report = {
122         "html": generate_html_report,
123         "pdf": generate_pdf_report
124     }
125
126     for report_format, versions in spec.output["format"].items():
127         report[report_format](release, spec, versions, report_version)
128
129     archive_input_data(spec)
130     archive_report(spec)
131
132     logging.info("Done.")
133
134
135 def generate_html_report(release, spec, versions, report_version):
136     """Generate html format of the report.
137
138     :param release: Release string of the product.
139     :param spec: Specification read from the specification file.
140     :param versions: List of versions to generate.
141     :param report_version: Version of the report.
142     :type release: str
143     :type spec: Specification
144     :type versions: list
145     :type report_version: str
146     """
147
148     logging.info("  Generating the html report, give me a few minutes, please "
149                  "...")
150
151     working_dir = spec.environment["paths"]["DIR[WORKING,SRC]"]
152
153     cmd = 'cd {working_dir} && mv -f index.html.template index.rst'.\
154         format(working_dir=working_dir)
155     execute_command(cmd)
156
157     cmd = HTML_BUILDER.format(
158         release=release,
159         report_version=report_version,
160         date=datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%MZ'),
161         working_dir=working_dir,
162         build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
163     execute_command(cmd)
164
165     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
166             css_file:
167         css_file.write(THEME_OVERRIDES)
168
169     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
170             css_file:
171         css_file.write(THEME_OVERRIDES)
172
173     logging.info("  Done.")
174
175
176 def generate_pdf_report(release, spec, versions, report_version):
177     """Generate html format of the report.
178
179     :param release: Release string of the product.
180     :param spec: Specification read from the specification file.
181     :param versions: List of versions to generate. Not implemented yet.
182     :param report_version: Version of the report.
183     :type release: str
184     :type spec: Specification
185     :type versions: list
186     :type report_version: str
187     """
188
189     logging.info("  Generating the pdf report, give me a few minutes, please "
190                  "...")
191
192     working_dir = spec.environment["paths"]["DIR[WORKING,SRC]"]
193
194     cmd = 'cd {working_dir} && mv -f index.pdf.template index.rst'.\
195         format(working_dir=working_dir)
196     execute_command(cmd)
197
198     _convert_all_svg_to_pdf(spec.environment["paths"]["DIR[WORKING,SRC]"])
199
200     # Convert PyPLOT graphs in HTML format to PDF.
201     convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}"
202     plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
203     plots.extend(get_files(spec.environment["paths"]["DIR[STATIC,DPDK]"],
204                            "html"))
205     for plot in plots:
206         file_name = "{0}.pdf".format(plot.rsplit(".", 1)[0])
207         logging.info("Converting '{0}' to '{1}'".format(plot, file_name))
208         execute_command(convert_plots.format(html=plot, pdf=file_name))
209
210     # Generate the LaTeX documentation
211     build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"]
212     cmd = PDF_BUILDER.format(
213         release=release,
214         report_version=report_version,
215         date=datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%MZ'),
216         working_dir=working_dir,
217         build_dir=build_dir)
218     execute_command(cmd)
219
220     # Build pdf documentation
221     archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"]
222     cmds = [
223         'cd {build_dir} && '
224         'pdflatex -shell-escape -interaction nonstopmode csit.tex || true'.
225         format(build_dir=build_dir),
226         'cd {build_dir} && '
227         'pdflatex -interaction nonstopmode csit.tex || true'.
228         format(build_dir=build_dir),
229         'cd {build_dir} && '
230         'cp csit.pdf ../{archive_dir}/csit_{release}_v{report_version}.pdf &&'
231         'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'.
232         format(build_dir=build_dir,
233                archive_dir=archive_dir,
234                release=release,
235                report_version=report_version)
236     ]
237
238     for cmd in cmds:
239         execute_command(cmd)
240
241     logging.info("  Done.")
242
243
244 def archive_report(spec):
245     """Archive the report.
246
247     :param spec: Specification read from the specification file.
248     :type spec: Specification
249     """
250
251     logging.info("  Archiving the report ...")
252
253     make_archive("csit.report",
254                  "gztar",
255                  base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
256
257     logging.info("  Done.")
258
259
260 def _convert_all_svg_to_pdf(path):
261     """Convert all svg files on path "path" to pdf.
262
263     :param path: Path to the root directory with svg files to convert.
264     :type path: str
265     """
266
267     cmd = "inkscape -D -z --file={svg} --export-pdf={pdf}"
268
269     svg_files = get_files(path, "svg", full_path=True)
270     for svg_file in svg_files:
271         pdf_file = "{0}.pdf".format(svg_file.rsplit('.', 1)[0])
272         logging.info("Converting '{0}' to '{1}'".format(svg_file, pdf_file))
273         execute_command(cmd.format(svg=svg_file, pdf=pdf_file))