~camptocamp/c2c-financial-addons/trunk

« back to all changes in this revision

Viewing changes to account_financial_report_webkit/report/webkit_parser_header_fix.py

  • Committer: Alexandre Fayolle @ camptocamp
  • Date: 2012-07-25 13:39:40 UTC
  • mfrom: (29.1.1 c2c-financial-addons-trunk)
  • Revision ID: alexandre.fayolle@camptocamp.com-20120725133940-usogouc93npwr3nq
[MRG] from lp:~c2c/c2c-financial-addons/trunk-fix1003563

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
        tmp_dir = tempfile.gettempdir()
81
81
        out_filename = tempfile.mktemp(suffix=".pdf", prefix="webkit.tmp.")
82
82
        files = []
83
 
        file_to_del = []
 
83
        file_to_del = [out_filename]
84
84
        if comm_path:
85
85
            command = [comm_path]
86
86
        else:
116
116
            file_to_del.append(html_file.name)
117
117
            command.append(html_file.name)
118
118
        command.append(out_filename)
 
119
        stderr_fd, stderr_path = tempfile.mkstemp(text=True)
 
120
        file_to_del.append(stderr_path)
119
121
        try:
120
 
            status = subprocess.call(command, stderr=subprocess.PIPE) # ignore stderr
121
 
            if status :
122
 
                raise except_osv(
123
 
                                _('Webkit raise an error' ),
124
 
                                status
125
 
                            )
126
 
        except Exception:
127
 
            for f_to_del in file_to_del :
128
 
                os.unlink(f_to_del)
129
 
 
130
 
        pdf = file(out_filename, 'rb').read()
131
 
        for f_to_del in file_to_del :
132
 
            os.unlink(f_to_del)
133
 
 
134
 
        os.unlink(out_filename)
 
122
            status = subprocess.call(command, stderr=stderr_fd)
 
123
            os.close(stderr_fd) # force flush
 
124
            stderr_fd = None    # avoid closing again in finally
 
125
            fobj = open(stderr_path, 'r')
 
126
            error_message = fobj.read()
 
127
            fobj.close()
 
128
            if not error_message:
 
129
                error_message = _('No diagnosis message was provided')
 
130
            else:
 
131
                error_message = _('The following diagnosis message was provided:\n') + error_message
 
132
            if status:
 
133
                raise except_osv(_('Webkit error' ),
 
134
                                 _("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message))
 
135
            pdf_file = open(out_filename, 'rb')
 
136
            pdf = pdf_file.read()
 
137
            pdf_file.close()
 
138
        finally:
 
139
            if stderr_fd is not None:
 
140
                os.close(stderr_fd)
 
141
            for f_to_del in file_to_del:
 
142
                try:
 
143
                    os.unlink(f_to_del)
 
144
                except (OSError, IOError), exc:
 
145
                    _logger.error('cannot remove file %s: %s', f_to_del, exc)
135
146
        return pdf
136
147
 
137
148
    # override needed to keep the attachments' storing procedure