Report: Data, plots, datetime
[csit.git] / resources / tools / presentation / generator_report.py
index 8a3f833..07103db 100644 (file)
 """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 utils import get_files, execute_command, archive_input_data
 
 
 # .css file for the html format of the report
@@ -50,6 +46,7 @@ THEME_OVERRIDES = """/* override table width restrictions */
 # Command to build the html format of the report
 HTML_BUILDER = 'sphinx-build -v -c . -a ' \
                '-b html -E ' \
+               '-t html ' \
                '-D release={release} ' \
                '-D version="{release} report - {date}" ' \
                '{working_dir} ' \
@@ -58,6 +55,7 @@ HTML_BUILDER = 'sphinx-build -v -c . -a ' \
 # Command to build the pdf format of the report
 PDF_BUILDER = 'sphinx-build -v -c . -a ' \
               '-b latex -E ' \
+              '-t latex ' \
               '-D release={release} ' \
               '-D version="{release} report - {date}" ' \
               '{working_dir} ' \
@@ -80,7 +78,7 @@ def generate_report(release, spec):
         "pdf": generate_pdf_report
     }
 
-    for report_format, versions in spec.output.items():
+    for report_format, versions in spec.output["format"].items():
         report[report_format](release, spec, versions)
 
     archive_input_data(spec)
@@ -105,10 +103,10 @@ def generate_html_report(release, spec, versions):
 
     cmd = HTML_BUILDER.format(
         release=release,
-        date=datetime.date.today().strftime('%d-%b-%Y'),
+        date=datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M UTC'),
         working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
         build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
-    _execute_command(cmd)
+    execute_command(cmd)
 
     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
             css_file:
@@ -138,20 +136,22 @@ def generate_pdf_report(release, spec, versions):
     convert_plots = "xvfb-run -a wkhtmltopdf {html} {pdf}.pdf"
 
     # Convert PyPLOT graphs in HTML format to PDF.
-    plots  = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
+    plots = get_files(spec.environment["paths"]["DIR[STATIC,VPP]"], "html")
+    plots.extend(get_files(spec.environment["paths"]["DIR[STATIC,DPDK]"],
+                           "html"))
     for plot in plots:
         file_name = "{0}".format(plot.rsplit(".", 1)[0])
         cmd = convert_plots.format(html=plot, pdf=file_name)
-        _execute_command(cmd)
+        execute_command(cmd)
 
     # Generate the LaTeX documentation
     build_dir = spec.environment["paths"]["DIR[BUILD,LATEX]"]
     cmd = PDF_BUILDER.format(
         release=release,
-        date=datetime.date.today().strftime('%d-%b-%Y'),
+        date=datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M UTC'),
         working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
         build_dir=build_dir)
-    _execute_command(cmd)
+    execute_command(cmd)
 
     # Build pdf documentation
     archive_dir = spec.environment["paths"]["DIR[STATIC,ARCH]"]
@@ -170,7 +170,7 @@ def generate_pdf_report(release, spec, versions):
     ]
 
     for cmd in cmds:
-        _execute_command(cmd)
+        execute_command(cmd)
 
     logging.info("  Done.")
 
@@ -189,64 +189,3 @@ def archive_report(spec):
                  base_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
 
     logging.info("  Done.")
-
-
-def archive_input_data(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)
-
-    except (Error, OSError) as err:
-        raise PresentationError("Not possible to archive the input data.",
-                                str(err))
-
-    logging.info("    Done.")
-
-
-def _execute_command(cmd):
-    """Execute the command in a subprocess and log the stdout and stderr.
-
-    :param cmd: Command to execute.
-    :type cmd: str
-    :returns: Return code of the executed command.
-    :rtype: int
-    """
-
-    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