Skip to content

Commit

Permalink
Use NamedTemporaryFile instead of file and of deprecated mktemp. That…
Browse files Browse the repository at this point in the history
… way we ensure 2 files created at the exact same time will have a unique name
  • Loading branch information
yvaucher committed May 13, 2014
1 parent c16d0fc commit 494f168
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions addons/report_webkit/webkit_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ def generate_pdf(self, comm_path, report_xml, header, footer, html_list, webkit_
"""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:
Expand All @@ -117,25 +118,15 @@ def generate_pdf(self, comm_path, report_xml, header, footer, html_list, webkit_
# default to UTF-8 encoding. Use <meta charset="latin-1"> 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])

Expand All @@ -153,13 +144,13 @@ def generate_pdf(self, comm_path, report_xml, header, footer, html_list, webkit_
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:
Expand All @@ -176,9 +167,8 @@ def generate_pdf(self, comm_path, report_xml, header, footer, html_list, webkit_
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)
Expand Down

0 comments on commit 494f168

Please sign in to comment.