Merge lp:~camptocamp/c2c-financial-addons/6.0-fix1003563 into lp:c2c-financial-addons/6.0

Proposed by Alexandre Fayolle - camptocamp
Status: Merged
Merged at revision: 105
Proposed branch: lp:~camptocamp/c2c-financial-addons/6.0-fix1003563
Merge into: lp:c2c-financial-addons/6.0
Diff against target: 92 lines (+32/-20)
1 file modified
account_financial_report_webkit/report/webkit_parser_header_fix.py (+32/-20)
To merge this branch: bzr merge lp:~camptocamp/c2c-financial-addons/6.0-fix1003563
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp Approve
Nicolas Bessi - Camptocamp Pending
Review via email: mp+116490@code.launchpad.net

Description of the change

To post a comment you must log in.
Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

Approved as well as the 6.1 version

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account_financial_report_webkit/report/webkit_parser_header_fix.py'
2--- account_financial_report_webkit/report/webkit_parser_header_fix.py 2011-10-14 09:20:15 +0000
3+++ account_financial_report_webkit/report/webkit_parser_header_fix.py 2012-07-24 14:39:22 +0000
4@@ -34,6 +34,7 @@
5 import netsvc
6 import pooler
7 import tools
8+import logging
9 import addons
10
11 from mako.template import Template
12@@ -44,6 +45,7 @@
13 from report_webkit.webkit_report import mako_template
14 from report_webkit.report_helper import WebKitHelper
15
16+_logger = logging.getLogger('financial.reports.webkit')
17
18 # Class used only as a workaround to bug :
19 # http://code.google.com/p/wkhtmltopdf/issues/detail?id=656
20@@ -69,6 +71,7 @@
21 # ],
22 # })
23
24+
25 class HeaderFooterTextWebKitParser(webkit_report.WebKitParser):
26
27 def generate_pdf(self, comm_path, report_xml, header, footer, html_list, webkit_header=False):
28@@ -76,10 +79,9 @@
29 if not webkit_header:
30 webkit_header = report_xml.webkit_header
31 tmp_dir = tempfile.gettempdir()
32- out = report_xml.name+str(time.time())+'.pdf'
33- out = os.path.join(tmp_dir, out.replace(' ',''))
34+ out_filename = tempfile.mktemp(suffix=".pdf", prefix="webkit.tmp.")
35 files = []
36- file_to_del = []
37+ file_to_del = [out_filename]
38 if comm_path:
39 command = [comm_path]
40 else:
41@@ -114,24 +116,34 @@
42 html_file.close()
43 file_to_del.append(html_file.name)
44 command.append(html_file.name)
45- command.append(out)
46- generate_command = ' '.join(command)
47+ command.append(out_filename)
48+ stderr_fd, stderr_path = tempfile.mkstemp(text=True)
49+ file_to_del.append(stderr_path)
50 try:
51- status = subprocess.call(command, stderr=subprocess.PIPE) # ignore stderr
52- if status :
53- raise except_osv(
54- _('Webkit raise an error' ),
55- status
56- )
57- except Exception:
58- for f_to_del in file_to_del :
59- os.unlink(f_to_del)
60-
61- pdf = file(out, 'rb').read()
62- for f_to_del in file_to_del :
63- os.unlink(f_to_del)
64-
65- os.unlink(out)
66+ status = subprocess.call(command, stderr=stderr_fd)
67+ os.close(stderr_fd) # force flush
68+ stderr_fd = None # avoid closing again in finally
69+ fobj = open(stderr_path, 'r')
70+ error_message = fobj.read()
71+ fobj.close()
72+ if not error_message:
73+ error_message = _('No diagnosis message was provided')
74+ else:
75+ error_message = _('The following diagnosis message was provided:\n') + error_message
76+ if status:
77+ raise except_osv(_('Webkit error' ),
78+ _("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message))
79+ pdf_file = open(out_filename, 'rb')
80+ pdf = pdf_file.read()
81+ pdf_file.close()
82+ finally:
83+ if stderr_fd is not None:
84+ os.close(stderr_fd)
85+ for f_to_del in file_to_del:
86+ try:
87+ os.unlink(f_to_del)
88+ except (OSError, IOError), exc:
89+ _logger.error('cannot remove file %s: %s', f_to_del, exc)
90 return pdf
91
92 # override needed to keep the attachments' storing procedure

Subscribers

People subscribed via source and target branches

to status/vote changes: