From 494f1686c3127658b38c845da3ffe0f6b634fa28 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Tue, 13 May 2014 17:56:10 +0200 Subject: [PATCH 1/2] Use NamedTemporaryFile instead of file and of deprecated mktemp. That way we ensure 2 files created at the exact same time will have a unique name --- addons/report_webkit/webkit_report.py | 44 +++++++++++---------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/addons/report_webkit/webkit_report.py b/addons/report_webkit/webkit_report.py index c6a30f5e579..20c71e3acbd 100644 --- a/addons/report_webkit/webkit_report.py +++ b/addons/report_webkit/webkit_report.py @@ -105,9 +105,10 @@ class WebKitParser(report_sxw): """Call webkit in order to generate pdf""" if not webkit_header: webkit_header = report_xml.webkit_header - tmp_dir = tempfile.gettempdir() - out_filename = tempfile.mktemp(suffix=".pdf", prefix="webkit.tmp.") - file_to_del = [out_filename] + out_filename = tempfile.NamedTemporaryFile(suffix=".pdf", + prefix="webkit.tmp.", + delete=False) + file_to_del = [out_filename.name] if comm_path: command = [comm_path] else: @@ -117,25 +118,15 @@ class WebKitParser(report_sxw): # default to UTF-8 encoding. Use to override. command.extend(['--encoding', 'utf-8']) if header : - head_file = file( os.path.join( - tmp_dir, - str(time.time()) + '.head.html' - ), - 'w' - ) - head_file.write(self._sanitize_html(header)) - head_file.close() + with tempfile.NamedTemporaryFile(suffix=".head.html", + delete=False) as head_file: + head_file.write(self._sanitize_html(header)) file_to_del.append(head_file.name) command.extend(['--header-html', head_file.name]) if footer : - foot_file = file( os.path.join( - tmp_dir, - str(time.time()) + '.foot.html' - ), - 'w' - ) - foot_file.write(self._sanitize_html(footer)) - foot_file.close() + with tempfile.NamedTemporaryFile(suffix=".foot.html", + delete=False) as foot_file: + foot_file.write(self._sanitize_html(footer)) file_to_del.append(foot_file.name) command.extend(['--footer-html', foot_file.name]) @@ -153,13 +144,13 @@ class WebKitParser(report_sxw): command.extend(['--page-size', str(webkit_header.format).replace(',', '.')]) count = 0 for html in html_list : - html_file = file(os.path.join(tmp_dir, str(time.time()) + str(count) +'.body.html'), 'w') - count += 1 - html_file.write(self._sanitize_html(html)) - html_file.close() + with tempfile.NamedTemporaryFile(suffix="%d.body.html" %count, + delete=False) as html_file: + count += 1 + html_file.write(self._sanitize_html(html)) file_to_del.append(html_file.name) command.append(html_file.name) - command.append(out_filename) + command.append(out_filename.name) stderr_fd, stderr_path = tempfile.mkstemp(text=True) file_to_del.append(stderr_path) try: @@ -176,9 +167,8 @@ class WebKitParser(report_sxw): if status : raise except_osv(_('Webkit error' ), _("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message)) - pdf_file = open(out_filename, 'rb') - pdf = pdf_file.read() - pdf_file.close() + with out_filename as pdf_file: + pdf = pdf_file.read() finally: if stderr_fd is not None: os.close(stderr_fd) From 4ed343455c3fb396e2a77630ec604cbeee5ce37e Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Wed, 14 May 2014 13:22:42 +0200 Subject: [PATCH 2/2] Fix incompatibility with Windows using tempfile. --- addons/report_webkit/webkit_report.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/addons/report_webkit/webkit_report.py b/addons/report_webkit/webkit_report.py index 20c71e3acbd..e6ed123b8ad 100644 --- a/addons/report_webkit/webkit_report.py +++ b/addons/report_webkit/webkit_report.py @@ -105,10 +105,9 @@ class WebKitParser(report_sxw): """Call webkit in order to generate pdf""" if not webkit_header: webkit_header = report_xml.webkit_header - out_filename = tempfile.NamedTemporaryFile(suffix=".pdf", - prefix="webkit.tmp.", - delete=False) - file_to_del = [out_filename.name] + fd, out_filename = tempfile.mkstemp(suffix=".pdf", + prefix="webkit.tmp.") + file_to_del = [out_filename] if comm_path: command = [comm_path] else: @@ -150,7 +149,7 @@ class WebKitParser(report_sxw): html_file.write(self._sanitize_html(html)) file_to_del.append(html_file.name) command.append(html_file.name) - command.append(out_filename.name) + command.append(out_filename) stderr_fd, stderr_path = tempfile.mkstemp(text=True) file_to_del.append(stderr_path) try: @@ -167,8 +166,9 @@ class WebKitParser(report_sxw): if status : raise except_osv(_('Webkit error' ), _("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message)) - with out_filename as pdf_file: + with open(out_filename, 'rb') as pdf_file: pdf = pdf_file.read() + os.close(fd) finally: if stderr_fd is not None: os.close(stderr_fd)