Report: Data, plots, datetime
[csit.git] / resources / tools / presentation / generator_report.py
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:
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 """
45
46 # Command to build the html format of the report
47 HTML_BUILDER = 'sphinx-build -v -c . -a ' \
48                '-b html -E ' \
49                '-t html ' \
50                '-D release={release} ' \
51                '-D version="{release} report - {date}" ' \
52                '{working_dir} ' \
53                '{build_dir}/'
54
55 # Command to build the pdf format of the report
56 PDF_BUILDER = 'sphinx-build -v -c . -a ' \
57               '-b latex -E ' \
58               '-t latex ' \
59               '-D release={release} ' \
60               '-D version="{release} report - {date}" ' \
61               '{working_dir} ' \
62               '{build_dir}'
63
64
65 def generate_report(release, spec):
66     """Generate all formats and versions of the report.
67
68     :param release: Release string of the product.
69     :param spec: Specification read from the specification file.
70     :type release: str
71     :type spec: Specification
72     """
73
74     logging.info("Generating the report ...")
75
76     report = {
77         "html": generate_html_report,
78         "pdf": generate_pdf_report
79     }
80
81     for report_format, versions in spec.output["format"].items():
82         report[report_format](release, spec, versions)
83
84     archive_input_data(spec)
85     archive_report(spec)
86
87     logging.info("Done.")
88
89
90 def generate_html_report(release, spec, versions):
91     """Generate html format of the report.
92
93     :param release: Release string of the product.
94     :param spec: Specification read from the specification file.
95     :param versions: List of versions to generate.
96     :type release: str
97     :type spec: Specification
98     :type versions: list
99     """
100
101     logging.info("  Generating the html report, give me a few minutes, please "
102                  "...")
103
104     cmd = HTML_BUILDER.format(
105         release=release,
106         date=datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M UTC'),
107         working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
108         build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
109     execute_command(cmd)
110
111     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
112             css_file:
113         css_file.write(THEME_OVERRIDES)
114
115     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
116             css_file:
117         css_file.write(THEME_OVERRIDES)
118
119     logging.info("  Done.")
120
121
122 def generate_pdf_report(release, spec, versions):
123     """Generate html format of the report.
124
125     :param release: Release string of the product.
126     :param spec: Specification read from the specification file.
127     :param versions: List of versions to generate. Not implemented yet.
128     :type release: str
129     :type spec: Specification
130     :type versions: list
131     """
132
133     logging.info("  Generating the pdf report, give me a few minutes, please "
134                  "...")
135
136     convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}.pdf"
137
138     # Convert PyPLOT graphs in HTML format to PDF.
139     plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
140     plots.extend(get_files(spec.environment["paths"]["DIR[STATIC,DPDK]"],
141                            "html"))
142     for plot in plots:
143         file_name = "{0}".format(plot.rsplit(".", 1)[0])
144         cmd = convert_plots.format(html=plot, pdf=file_name)
145         execute_command(cmd)
146
147     # Generate the LaTeX documentation
148     build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"]
149     cmd = PDF_BUILDER.format(
150         release=release,
151         date=datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M UTC'),
152         working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
153         build_dir=build_dir)
154     execute_command(cmd)
155
156     # Build pdf documentation
157     archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"]
158     cmds = [
159         'cd {build_dir} && '
160         'pdflatex -shell-escape -interaction nonstopmode csit.tex || true'.
161         format(build_dir=build_dir),
162         'cd {build_dir} && '
163         'pdflatex -interaction nonstopmode csit.tex || true'.
164         format(build_dir=build_dir),
165         'cd {build_dir} && '
166         'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'.
167         format(build_dir=build_dir,
168                archive_dir=archive_dir,
169                release=release)
170     ]
171
172     for cmd in cmds:
173         execute_command(cmd)
174
175     logging.info("  Done.")
176
177
178 def archive_report(spec):
179     """Archive the report.
180
181     :param spec: Specification read from the specification file.
182     :type spec: Specification
183     """
184
185     logging.info("  Archiving the report ...")
186
187     make_archive("csit.report",
188                  "gztar",
189                  base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
190
191     logging.info("  Done.")

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.