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:
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.
21 from os import makedirs, environ
22 from os.path import isdir
23 from shutil import copy, Error, make_archive
25 from utils import get_files
26 from errors import PresentationError
29 # .css file for the html format of the report
30 THEME_OVERRIDES = """/* override table width restrictions */
31 @media screen and (min-width: 767px) {
32 .wy-table-responsive table td, .wy-table-responsive table th {
33 white-space: normal !important;
36 .wy-table-responsive {
40 overflow: visible !important;
43 .rst-content blockquote {
50 # Command to build the html format of the report
51 HTML_BUILDER = 'sphinx-build -v -c . -a ' \
54 '-D release={release} ' \
55 '-D version="{release} report - {date}" ' \
59 # Command to build the pdf format of the report
60 PDF_BUILDER = 'sphinx-build -v -c . -a ' \
63 '-D release={release} ' \
64 '-D version="{release} report - {date}" ' \
69 def generate_report(release, spec):
70 """Generate all formats and versions of the report.
72 :param release: Release string of the product.
73 :param spec: Specification read from the specification file.
75 :type spec: Specification
78 logging.info("Generating the report ...")
81 "html": generate_html_report,
82 "pdf": generate_pdf_report
85 for report_format, versions in spec.output.items():
86 report[report_format](release, spec, versions)
88 archive_input_data(spec)
94 def generate_html_report(release, spec, versions):
95 """Generate html format of the report.
97 :param release: Release string of the product.
98 :param spec: Specification read from the specification file.
99 :param versions: List of versions to generate.
101 :type spec: Specification
105 logging.info(" Generating the html report, give me a few minutes, please "
108 cmd = HTML_BUILDER.format(
110 date=datetime.date.today().strftime('%d-%b-%Y'),
111 working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
112 build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
113 _execute_command(cmd)
115 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
117 css_file.write(THEME_OVERRIDES)
119 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
121 css_file.write(THEME_OVERRIDES)
123 logging.info(" Done.")
126 def generate_pdf_report(release, spec, versions):
127 """Generate html format of the report.
129 :param release: Release string of the product.
130 :param spec: Specification read from the specification file.
131 :param versions: List of versions to generate. Not implemented yet.
133 :type spec: Specification
137 logging.info(" Generating the pdf report, give me a few minutes, please "
140 convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}.pdf"
142 # Convert PyPLOT graphs in HTML format to PDF.
143 plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
144 plots.extend(get_files(spec.environment["paths"]["DIR[STATIC,DPDK]"],
147 file_name = "{0}".format(plot.rsplit(".", 1)[0])
148 cmd = convert_plots.format(html=plot, pdf=file_name)
149 _execute_command(cmd)
151 # Generate the LaTeX documentation
152 build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"]
153 cmd = PDF_BUILDER.format(
155 date=datetime.date.today().strftime('%d-%b-%Y'),
156 working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
158 _execute_command(cmd)
160 # Build pdf documentation
161 archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"]
164 'pdflatex -shell-escape -interaction nonstopmode csit.tex || true'.
165 format(build_dir=build_dir),
167 'pdflatex -interaction nonstopmode csit.tex || true'.
168 format(build_dir=build_dir),
170 'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'.
171 format(build_dir=build_dir,
172 archive_dir=archive_dir,
177 _execute_command(cmd)
179 logging.info(" Done.")
182 def archive_report(spec):
183 """Archive the report.
185 :param spec: Specification read from the specification file.
186 :type spec: Specification
189 logging.info(" Archiving the report ...")
191 make_archive("csit.report",
193 base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
195 logging.info(" Done.")
198 def archive_input_data(spec):
199 """Archive the report.
201 :param spec: Specification read from the specification file.
202 :type spec: Specification
203 :raises PresentationError: If it is not possible to archive the input data.
206 logging.info(" Archiving the input data files ...")
209 extension = spec.debug["input-format"]
211 extension = spec.input["file-format"]
212 data_files = get_files(spec.environment["paths"]["DIR[WORKING,DATA]"],
214 dst = spec.environment["paths"]["DIR[STATIC,ARCH]"]
215 logging.info(" Destination: {0}".format(dst))
221 for data_file in data_files:
222 logging.info(" Copying the file: {0} ...".format(data_file))
225 except (Error, OSError) as err:
226 raise PresentationError("Not possible to archive the input data.",
229 logging.info(" Done.")
232 def _execute_command(cmd):
233 """Execute the command in a subprocess and log the stdout and stderr.
235 :param cmd: Command to execute.
237 :returns: Return code of the executed command.
242 proc = subprocess.Popen(
244 stdout=subprocess.PIPE,
245 stderr=subprocess.PIPE,
249 stdout, stderr = proc.communicate()
254 if proc.returncode != 0:
255 logging.error(" Command execution failed.")
256 return proc.returncode