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 ' \
53 '-D release={release} ' \
54 '-D version="{release} report - {date}" ' \
58 # Command to build the pdf format of the report
59 PDF_BUILDER = 'sphinx-build -v -c . -a ' \
61 '-D release={release} ' \
62 '-D version="{release} report - {date}" ' \
67 def generate_report(release, spec):
68 """Generate all formats and versions of the report.
70 :param release: Release string of the product.
71 :param spec: Specification read from the specification file.
73 :type spec: Specification
76 logging.info("Generating the report ...")
79 "html": generate_html_report,
80 "pdf": generate_pdf_report
83 for report_format, versions in spec.output.items():
84 report[report_format](release, spec, versions)
86 archive_input_data(spec)
92 def generate_html_report(release, spec, versions):
93 """Generate html format of the report.
95 :param release: Release string of the product.
96 :param spec: Specification read from the specification file.
97 :param versions: List of versions to generate.
99 :type spec: Specification
103 logging.info(" Generating the html report, give me a few minutes, please "
106 cmd = HTML_BUILDER.format(
108 date=datetime.date.today().strftime('%d-%b-%Y'),
109 working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
110 build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
111 _execute_command(cmd)
113 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
115 css_file.write(THEME_OVERRIDES)
117 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
119 css_file.write(THEME_OVERRIDES)
121 logging.info(" Done.")
124 def generate_pdf_report(release, spec, versions):
125 """Generate html format of the report.
127 :param release: Release string of the product.
128 :param spec: Specification read from the specification file.
129 :param versions: List of versions to generate. Not implemented yet.
131 :type spec: Specification
135 logging.info(" Generating the pdf report, give me a few minutes, please "
138 convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}.pdf"
140 # Convert PyPLOT graphs in HTML format to PDF.
141 plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
143 file_name = "{0}".format(plot.rsplit(".", 1)[0])
144 cmd = convert_plots.format(html=plot, pdf=file_name)
145 _execute_command(cmd)
147 # Generate the LaTeX documentation
148 build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"]
149 cmd = PDF_BUILDER.format(
151 date=datetime.date.today().strftime('%d-%b-%Y'),
152 working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
154 _execute_command(cmd)
156 # Build pdf documentation
157 archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"]
160 'pdflatex -shell-escape -interaction nonstopmode csit.tex || true'.
161 format(build_dir=build_dir),
163 'pdflatex -interaction nonstopmode csit.tex || true'.
164 format(build_dir=build_dir),
166 'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'.
167 format(build_dir=build_dir,
168 archive_dir=archive_dir,
173 _execute_command(cmd)
175 logging.info(" Done.")
178 def archive_report(spec):
179 """Archive the report.
181 :param spec: Specification read from the specification file.
182 :type spec: Specification
185 logging.info(" Archiving the report ...")
187 make_archive("csit.report",
189 base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
191 logging.info(" Done.")
194 def archive_input_data(spec):
195 """Archive the report.
197 :param spec: Specification read from the specification file.
198 :type spec: Specification
199 :raises PresentationError: If it is not possible to archive the input data.
202 logging.info(" Archiving the input data files ...")
205 extension = spec.debug["input-format"]
207 extension = spec.input["file-format"]
208 data_files = get_files(spec.environment["paths"]["DIR[WORKING,DATA]"],
210 dst = spec.environment["paths"]["DIR[STATIC,ARCH]"]
211 logging.info(" Destination: {0}".format(dst))
217 for data_file in data_files:
218 logging.info(" Copying the file: {0} ...".format(data_file))
221 except (Error, OSError) as err:
222 raise PresentationError("Not possible to archive the input data.",
225 logging.info(" Done.")
228 def _execute_command(cmd):
229 """Execute the command in a subprocess and log the stdout and stderr.
231 :param cmd: Command to execute.
233 :returns: Return code of the executed command.
238 proc = subprocess.Popen(
240 stdout=subprocess.PIPE,
241 stderr=subprocess.PIPE,
245 stdout, stderr = proc.communicate()
250 if proc.returncode != 0:
251 logging.error(" Command execution failed.")
252 return proc.returncode