From f35d467a40b1b3a49a85635b5160c8bc998cf4bf Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Wed, 2 Sep 2020 09:09:35 +0200 Subject: [PATCH] Fix export issue PNG came out wrong because of concurrency (?) issues where the SVG file was converted before being fully generated. The solution was to close the temporary file between each operation. --- lokrez/export.py | 81 +++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/lokrez/export.py b/lokrez/export.py index 0dec772..0d8b558 100644 --- a/lokrez/export.py +++ b/lokrez/export.py @@ -51,51 +51,56 @@ def generate_outfile( cachedir.mkdir(parents=True, exist_ok=True) - with tempfile.NamedTemporaryFile( + tmpsvg = tempfile.NamedTemporaryFile( suffix=".svg", mode = "w", + delete = False, dir = str(cachedir), - ) as tmpsvg: - try: - log.info( - "Exporting to {} using inkscape" \ - .format(outfilename.suffix), + ) + tmpsvg.close() + try: + log.info( + "Exporting to {} using inkscape" \ + .format(outfilename.suffix), + ) + import subprocess + + jj2_tpl.stream(context).dump( tmpsvg.name ) + + inkscape_process = subprocess.Popen( + [ + "inkscape", + tmpsvg.name, + "--export-filename", + str(outfilename), + ], + stdout = subprocess.PIPE, + stderr = subprocess.STDOUT, + universal_newlines = True, + ) + + for line_out in iter(inkscape_process.stdout.readline, ""): + log.debug(line_out) + + inkscape_process.stdout.close() + + rv = inkscape_process.wait() + + if rv != 0: + raise Exception( + "Bad inkscape return code '{}'" \ + .format(inkscape_process.returncode) ) - import subprocess - - jj2_tpl.stream(context).dump( tmpsvg ) - - inkscape_process = subprocess.Popen( - [ - "inkscape", - tmpsvg.name, - "--export-filename", - str(outfilename), - ], - stdout = subprocess.PIPE, - stderr = subprocess.STDOUT, - universal_newlines = True, - ) - - for line_out in iter(inkscape_process.stdout.readline, ""): - log.debug(line_out) - - inkscape_process.stdout.close() - - rv = inkscape_process.wait() - - if rv != 0: - raise Exception( - "Bad inkscape return code '{}'" \ - .format(inkscape_process.returncode) - ) - return outfilename + return outfilename - except Exception as e: - log.warning("Failed to export with inkscape") - log.debug(e, exc_info=True) + except Exception as e: + log.warning("Failed to export with inkscape") + log.debug(e, exc_info=True) + + finally: + os.unlink(tmpsvg.name) # To png, pdf or ps with cairosvg # -------------------------------------------------------------------------