X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Ftools%2Fpresentation%2Fgenerator_report.py;h=142c264bcd28ca6874d9dafb07b554ceb4136ea5;hp=8a3f8334a7f6be51138a6c7fb59cc2e60ad8f278;hb=4d6dcbbbb9103f15fb55d525f14964c2323c2a03;hpb=eecad36d7d2275fa47fbcab40dbcf56108ab0a51 diff --git a/resources/tools/presentation/generator_report.py b/resources/tools/presentation/generator_report.py index 8a3f8334a7..142c264bcd 100644 --- a/resources/tools/presentation/generator_report.py +++ b/resources/tools/presentation/generator_report.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Cisco and/or its affiliates. +# Copyright (c) 2018 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -14,20 +14,16 @@ """Report generation. """ -import subprocess import logging import datetime -from os import makedirs, environ -from os.path import isdir -from shutil import copy, Error, make_archive +from shutil import make_archive -from utils import get_files -from errors import PresentationError +from pal_utils import get_files, execute_command, archive_input_data # .css file for the html format of the report -THEME_OVERRIDES = """/* override table width restrictions */ +THEME_OVERRIDES = u"""/* override table width restrictions */ @media screen and (min-width: 767px) { .wy-table-responsive table td, .wy-table-responsive table th { white-space: normal !important; @@ -45,208 +41,227 @@ THEME_OVERRIDES = """/* override table width restrictions */ line-height: 18px; margin-bottom: 0px; } +.wy-menu-vertical a { + display: inline-block; + line-height: 18px; + padding: 0 2em; + display: block; + position: relative; + font-size: 90%; + color: #d9d9d9 +} +.wy-menu-vertical li.current a { + color: gray; + border-right: solid 1px #c9c9c9; + padding: 0 3em; +} +.wy-menu-vertical li.toctree-l2.current > a { + background: #c9c9c9; + padding: 0 3em; +} +.wy-menu-vertical li.toctree-l2.current li.toctree-l3 > a { + display: block; + background: #c9c9c9; + padding: 0 4em; +} +.wy-menu-vertical li.toctree-l3.current li.toctree-l4 > a { + display: block; + background: #bdbdbd; + padding: 0 5em; +} +.wy-menu-vertical li.on a, .wy-menu-vertical li.current > a { + color: #404040; + padding: 0 2em; + font-weight: bold; + position: relative; + background: #fcfcfc; + border: none; + border-top-width: medium; + border-bottom-width: medium; + border-top-style: none; + border-bottom-style: none; + border-top-color: currentcolor; + border-bottom-color: currentcolor; + padding-left: 2em -4px; +} """ # Command to build the html format of the report -HTML_BUILDER = 'sphinx-build -v -c . -a ' \ - '-b html -E ' \ - '-D release={release} ' \ - '-D version="{release} report - {date}" ' \ - '{working_dir} ' \ - '{build_dir}/' +HTML_BUILDER = u'sphinx-build -v -c . -a ' \ + u'-b html -E ' \ + u'-t html ' \ + u'-D release={release} ' \ + u'-D version="Test Report {date}" ' \ + u'{working_dir} ' \ + u'{build_dir}/' # Command to build the pdf format of the report -PDF_BUILDER = 'sphinx-build -v -c . -a ' \ - '-b latex -E ' \ - '-D release={release} ' \ - '-D version="{release} report - {date}" ' \ - '{working_dir} ' \ - '{build_dir}' +PDF_BUILDER = u'sphinx-build -v -c . -a ' \ + u'-b latex -E ' \ + u'-t latex ' \ + u'-D release={release} ' \ + u'-D version="Test Report {date}" ' \ + u'{working_dir} ' \ + u'{build_dir}' -def generate_report(release, spec): +def generate_report(release, spec, report_week): """Generate all formats and versions of the report. :param release: Release string of the product. :param spec: Specification read from the specification file. + :param report_week: Calendar week when the report is published. :type release: str :type spec: Specification + :type report_week: str """ - logging.info("Generating the report ...") + logging.info(u"Generating the report ...") report = { - "html": generate_html_report, - "pdf": generate_pdf_report + u"html": generate_html_report, + u"pdf": generate_pdf_report } - for report_format, versions in spec.output.items(): - report[report_format](release, spec, versions) + for report_format in spec.output[u"format"]: + report[report_format](release, spec, report_week) archive_input_data(spec) - archive_report(spec) - logging.info("Done.") + logging.info(u"Done.") -def generate_html_report(release, spec, versions): +def generate_html_report(release, spec, report_version): """Generate html format of the report. :param release: Release string of the product. :param spec: Specification read from the specification file. - :param versions: List of versions to generate. + :param report_version: Version of the report. :type release: str :type spec: Specification - :type versions: list + :type report_version: str """ - logging.info(" Generating the html report, give me a few minutes, please " - "...") + _ = report_version + + logging.info(u" Generating the html report, give me a few minutes, please " + u"...") + + working_dir = spec.environment[u"paths"][u"DIR[WORKING,SRC]"] + + execute_command(f"cd {working_dir} && mv -f index.html.template index.rst") cmd = HTML_BUILDER.format( release=release, - date=datetime.date.today().strftime('%d-%b-%Y'), - working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"], - build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"]) - _execute_command(cmd) + date=datetime.datetime.utcnow().strftime(u'%Y-%m-%d %H:%M UTC'), + working_dir=working_dir, + build_dir=spec.environment[u"paths"][u"DIR[BUILD,HTML]"]) + execute_command(cmd) - with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \ + with open(spec.environment[u"paths"][u"DIR[CSS_PATCH_FILE]"], u"wt") as \ css_file: css_file.write(THEME_OVERRIDES) - with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \ + with open(spec.environment[u"paths"][u"DIR[CSS_PATCH_FILE2]"], u"wt") as \ css_file: css_file.write(THEME_OVERRIDES) - logging.info(" Done.") + logging.info(u" Done.") -def generate_pdf_report(release, spec, versions): +def generate_pdf_report(release, spec, report_week): """Generate html format of the report. :param release: Release string of the product. :param spec: Specification read from the specification file. - :param versions: List of versions to generate. Not implemented yet. + :param report_week: Calendar week when the report is published. :type release: str :type spec: Specification - :type versions: list + :type report_week: str """ - logging.info(" Generating the pdf report, give me a few minutes, please " - "...") + logging.info(u" Generating the pdf report, give me a few minutes, please " + u"...") + + working_dir = spec.environment[u"paths"][u"DIR[WORKING,SRC]"] + + execute_command(f"cd {working_dir} && mv -f index.pdf.template index.rst") - convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}.pdf" + _convert_all_svg_to_pdf(spec.environment[u"paths"][u"DIR[WORKING,SRC]"]) # Convert PyPLOT graphs in HTML format to PDF. - plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html") + convert_plots = u"xvfb-run -a wkhtmltopdf {html} {pdf}" + plots = get_files(spec.environment[u"paths"][u"DIR[STATIC,VPP]"], u"html") + plots.extend( + get_files(spec.environment[u"paths"][u"DIR[STATIC,DPDK]"], u"html") + ) + pdf_plots = list() for plot in plots: - file_name = "{0}".format(plot.rsplit(".", 1)[0]) - cmd = convert_plots.format(html=plot, pdf=file_name) - _execute_command(cmd) + file_name = f"{plot.rsplit(u'.', 1)[0]}.pdf" + pdf_plots.append(file_name) + logging.info(f"Converting {plot} to {file_name}") + execute_command(convert_plots.format(html=plot, pdf=file_name)) # Generate the LaTeX documentation - build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"] + build_dir = spec.environment[u"paths"][u"DIR[BUILD,LATEX]"] cmd = PDF_BUILDER.format( release=release, - date=datetime.date.today().strftime('%d-%b-%Y'), - working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"], + date=datetime.datetime.utcnow().strftime(u'%Y-%m-%d %H:%M UTC'), + working_dir=working_dir, build_dir=build_dir) - _execute_command(cmd) + execute_command(cmd) # Build pdf documentation - archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"] + archive_dir = spec.environment[u"paths"][u"DIR[STATIC,ARCH]"] cmds = [ - 'cd {build_dir} && ' - 'pdflatex -shell-escape -interaction nonstopmode csit.tex || true'. - format(build_dir=build_dir), - 'cd {build_dir} && ' - 'pdflatex -interaction nonstopmode csit.tex || true'. - format(build_dir=build_dir), - 'cd {build_dir} && ' - 'cp csit.pdf ../{archive_dir}/csit_{release}.pdf'. - format(build_dir=build_dir, - archive_dir=archive_dir, - release=release) + f'cd {build_dir} && ' + f'pdflatex -shell-escape -interaction nonstopmode csit.tex || true', + f'cd {build_dir} && ' + f'pdflatex -interaction nonstopmode csit.tex || true', + f'cd {build_dir} && ' + f'cp csit.pdf ../{archive_dir}/csit_{release}.{report_week}.pdf &&' + f'cp csit.pdf ../{archive_dir}/csit_{release}.pdf' ] for cmd in cmds: - _execute_command(cmd) + execute_command(cmd) - logging.info(" Done.") + # Delete temporary pdf files: + for plot in pdf_plots: + execute_command(f"rm {plot}") + logging.info(u" Done.") -def archive_report(spec): - """Archive the report. - :param spec: Specification read from the specification file. - :type spec: Specification - """ - - logging.info(" Archiving the report ...") - - make_archive("csit.report", - "gztar", - base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"]) - - logging.info(" Done.") - - -def archive_input_data(spec): +def archive_report(spec): """Archive the report. :param spec: Specification read from the specification file. :type spec: Specification - :raises PresentationError: If it is not possible to archive the input data. """ - logging.info(" Archiving the input data files ...") - - if spec.is_debug: - extension = spec.debug["input-format"] - else: - extension = spec.input["file-format"] - data_files = get_files(spec.environment["paths"]["DIR[WORKING,DATA]"], - extension=extension) - dst = spec.environment["paths"]["DIR[STATIC,ARCH]"] - logging.info(" Destination: {0}".format(dst)) - - try: - if not isdir(dst): - makedirs(dst) - - for data_file in data_files: - logging.info(" Copying the file: {0} ...".format(data_file)) - copy(data_file, dst) + logging.info(u" Archiving the report ...") - except (Error, OSError) as err: - raise PresentationError("Not possible to archive the input data.", - str(err)) + make_archive( + u"csit.report", + u"gztar", + base_dir=spec.environment[u"paths"][u"DIR[BUILD,HTML]"] + ) - logging.info(" Done.") + logging.info(u" Done.") -def _execute_command(cmd): - """Execute the command in a subprocess and log the stdout and stderr. +def _convert_all_svg_to_pdf(path): + """Convert all svg files on path "path" to pdf. - :param cmd: Command to execute. - :type cmd: str - :returns: Return code of the executed command. - :rtype: int + :param path: Path to the root directory with svg files to convert. + :type path: str """ - env = environ.copy() - proc = subprocess.Popen( - [cmd], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=True, - env=env) - - stdout, stderr = proc.communicate() - - logging.info(stdout) - logging.info(stderr) - - if proc.returncode != 0: - logging.error(" Command execution failed.") - return proc.returncode + svg_files = get_files(path, u"svg", full_path=True) + for svg_file in svg_files: + pdf_file = f"{svg_file.rsplit(u'.', 1)[0]}.pdf" + logging.info(f"Converting {svg_file} to {pdf_file}") + execute_command( + f"inkscape -D -z --file={svg_file} --export-pdf={pdf_file}" + )