Merge lp:~camptocamp/ocb-addons/7.0-fix-1319095 into lp:ocb-addons

Proposed by Yannick Vaucher @ Camptocamp
Status: Merged
Approved by: Leonardo Pistone
Approved revision: no longer in the source branch.
Merged at revision: 10208
Proposed branch: lp:~camptocamp/ocb-addons/7.0-fix-1319095
Merge into: lp:ocb-addons
Diff against target: 74 lines (+15/-25)
1 file modified
report_webkit/webkit_report.py (+15/-25)
To merge this branch: bzr merge lp:~camptocamp/ocb-addons/7.0-fix-1319095
Reviewer Review Type Date Requested Status
Pedro Manuel Baeza code review Approve
Alexandre Fayolle - camptocamp code review, no test Approve
Nicolas Bessi - Camptocamp (community) no test, code review Approve
Florent Pending
Review via email: mp+219476@code.launchpad.net

Description of the change

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

Another hot fix from Florent was made here https://code.launchpad.net/~florent.x/openobject-addons/trunk-1290820-report_webkit/+merge/210387

I believe this one is a bit cleaner as it refactor a bit the code. However it doesn't keep timestamp in file name. But do we need timestamp in file name ?

If so I can still add it in prefix or suffix.

To post a comment you must log in.
Revision history for this message
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote :

Thanks for the fix.

LGTM

This patch will not be backportable as is to version 6.1, 6.0 as it is not Python 2.5 compliant

Regards

Nicolas

review: Approve (no test, code review)
Revision history for this message
Alexandre Fayolle - camptocamp (alexandre-fayolle-c2c) wrote :

This fix will not work on Windows.

out_filename is opened on line 11 of the file, and then the pdf converter will try to open a file with the same name while the file is still open in OpenERP which will fail on windows (https://docs.python.org/2.7/library/tempfile.html?highlight=namedtemporaryfile#tempfile.NamedTemporaryFile)

review: Needs Fixing (code review, no test)
Revision history for this message
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote :

Good catch

Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

Thanks for the review and the explanations.

This has been fixed.

Revision history for this message
Alexandre Fayolle - camptocamp (alexandre-fayolle-c2c) wrote :

LGTM

review: Approve (code review, no test)
Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

LGTM.

Regards.

review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'report_webkit/webkit_report.py'
2--- report_webkit/webkit_report.py 2014-04-16 07:43:09 +0000
3+++ report_webkit/webkit_report.py 2014-05-14 11:21:39 +0000
4@@ -105,8 +105,8 @@
5 """Call webkit in order to generate pdf"""
6 if not webkit_header:
7 webkit_header = report_xml.webkit_header
8- tmp_dir = tempfile.gettempdir()
9- out_filename = tempfile.mktemp(suffix=".pdf", prefix="webkit.tmp.")
10+ fd, out_filename = tempfile.mkstemp(suffix=".pdf",
11+ prefix="webkit.tmp.")
12 file_to_del = [out_filename]
13 if comm_path:
14 command = [comm_path]
15@@ -117,25 +117,15 @@
16 # default to UTF-8 encoding. Use <meta charset="latin-1"> to override.
17 command.extend(['--encoding', 'utf-8'])
18 if header :
19- head_file = file( os.path.join(
20- tmp_dir,
21- str(time.time()) + '.head.html'
22- ),
23- 'w'
24- )
25- head_file.write(self._sanitize_html(header))
26- head_file.close()
27+ with tempfile.NamedTemporaryFile(suffix=".head.html",
28+ delete=False) as head_file:
29+ head_file.write(self._sanitize_html(header))
30 file_to_del.append(head_file.name)
31 command.extend(['--header-html', head_file.name])
32 if footer :
33- foot_file = file( os.path.join(
34- tmp_dir,
35- str(time.time()) + '.foot.html'
36- ),
37- 'w'
38- )
39- foot_file.write(self._sanitize_html(footer))
40- foot_file.close()
41+ with tempfile.NamedTemporaryFile(suffix=".foot.html",
42+ delete=False) as foot_file:
43+ foot_file.write(self._sanitize_html(footer))
44 file_to_del.append(foot_file.name)
45 command.extend(['--footer-html', foot_file.name])
46
47@@ -153,10 +143,10 @@
48 command.extend(['--page-size', str(webkit_header.format).replace(',', '.')])
49 count = 0
50 for html in html_list :
51- html_file = file(os.path.join(tmp_dir, str(time.time()) + str(count) +'.body.html'), 'w')
52- count += 1
53- html_file.write(self._sanitize_html(html))
54- html_file.close()
55+ with tempfile.NamedTemporaryFile(suffix="%d.body.html" %count,
56+ delete=False) as html_file:
57+ count += 1
58+ html_file.write(self._sanitize_html(html))
59 file_to_del.append(html_file.name)
60 command.append(html_file.name)
61 command.append(out_filename)
62@@ -176,9 +166,9 @@
63 if status :
64 raise except_osv(_('Webkit error' ),
65 _("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message))
66- pdf_file = open(out_filename, 'rb')
67- pdf = pdf_file.read()
68- pdf_file.close()
69+ with open(out_filename, 'rb') as pdf_file:
70+ pdf = pdf_file.read()
71+ os.close(fd)
72 finally:
73 if stderr_fd is not None:
74 os.close(stderr_fd)