~camptocamp/openobject-addons/7.0-fix-1319095

« back to all changes in this revision

Viewing changes to report_webkit/webkit_report.py

  • Committer: Yannick Vaucher
  • Date: 2014-05-13 15:56:10 UTC
  • Revision ID: yannick.vaucher@camptocamp.com-20140513155610-zw0av4esnvdvsdj6
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

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
        """Call webkit in order to generate pdf"""
106
106
        if not webkit_header:
107
107
            webkit_header = report_xml.webkit_header
108
 
        tmp_dir = tempfile.gettempdir()
109
 
        out_filename = tempfile.mktemp(suffix=".pdf", prefix="webkit.tmp.")
110
 
        file_to_del = [out_filename]
 
108
        out_filename = tempfile.NamedTemporaryFile(suffix=".pdf",
 
109
                                                   prefix="webkit.tmp.",
 
110
                                                   delete=False)
 
111
        file_to_del = [out_filename.name]
111
112
        if comm_path:
112
113
            command = [comm_path]
113
114
        else:
117
118
        # default to UTF-8 encoding.  Use <meta charset="latin-1"> to override.
118
119
        command.extend(['--encoding', 'utf-8'])
119
120
        if header :
120
 
            head_file = file( os.path.join(
121
 
                                  tmp_dir,
122
 
                                  str(time.time()) + '.head.html'
123
 
                                 ),
124
 
                                'w'
125
 
                            )
126
 
            head_file.write(self._sanitize_html(header))
127
 
            head_file.close()
 
121
            with tempfile.NamedTemporaryFile(suffix=".head.html",
 
122
                                             delete=False) as head_file:
 
123
                head_file.write(self._sanitize_html(header))
128
124
            file_to_del.append(head_file.name)
129
125
            command.extend(['--header-html', head_file.name])
130
126
        if footer :
131
 
            foot_file = file(  os.path.join(
132
 
                                  tmp_dir,
133
 
                                  str(time.time()) + '.foot.html'
134
 
                                 ),
135
 
                                'w'
136
 
                            )
137
 
            foot_file.write(self._sanitize_html(footer))
138
 
            foot_file.close()
 
127
            with tempfile.NamedTemporaryFile(suffix=".foot.html",
 
128
                                             delete=False) as foot_file:
 
129
                foot_file.write(self._sanitize_html(footer))
139
130
            file_to_del.append(foot_file.name)
140
131
            command.extend(['--footer-html', foot_file.name])
141
132
 
153
144
            command.extend(['--page-size', str(webkit_header.format).replace(',', '.')])
154
145
        count = 0
155
146
        for html in html_list :
156
 
            html_file = file(os.path.join(tmp_dir, str(time.time()) + str(count) +'.body.html'), 'w')
157
 
            count += 1
158
 
            html_file.write(self._sanitize_html(html))
159
 
            html_file.close()
 
147
            with tempfile.NamedTemporaryFile(suffix="%d.body.html" %count,
 
148
                                             delete=False) as html_file:
 
149
                count += 1
 
150
                html_file.write(self._sanitize_html(html))
160
151
            file_to_del.append(html_file.name)
161
152
            command.append(html_file.name)
162
 
        command.append(out_filename)
 
153
        command.append(out_filename.name)
163
154
        stderr_fd, stderr_path = tempfile.mkstemp(text=True)
164
155
        file_to_del.append(stderr_path)
165
156
        try:
176
167
            if status :
177
168
                raise except_osv(_('Webkit error' ),
178
169
                                 _("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message))
179
 
            pdf_file = open(out_filename, 'rb')
180
 
            pdf = pdf_file.read()
181
 
            pdf_file.close()
 
170
            with out_filename as pdf_file:
 
171
                pdf = pdf_file.read()
182
172
        finally:
183
173
            if stderr_fd is not None:
184
174
                os.close(stderr_fd)