[FIX] fix issue lp:1003819
author <AlexandreFayolle@camptocamp> <>
Tue, 24 Jul 2012 13:58:42 +0000 (15:58 +0200)
committer <AlexandreFayolle@camptocamp> <>
Tue, 24 Jul 2012 13:58:42 +0000 (15:58 +0200)
* process the information that wkhtmltopdf can write on stderr
* cleanup the code handling file deletion to remove duplication
* provide information to the user about what went wrong

bzr revid: alexandre.fayolle@camptocamp.com-20120724135842-ldwsspsk51i4ne1b

addons/report_webkit/webkit_report.py

index 0498069..df07586 100644 (file)
@@ -107,7 +107,7 @@ class WebKitParser(report_sxw):
         tmp_dir = tempfile.gettempdir()
         out_filename = tempfile.mktemp(suffix=".pdf", prefix="webkit.tmp.")
         files = []
-        file_to_del = []
+        file_to_del = [out_filename]
         if comm_path:
             command = [comm_path]
         else:
@@ -160,22 +160,33 @@ class WebKitParser(report_sxw):
             file_to_del.append(html_file.name)
             command.append(html_file.name)
         command.append(out_filename)
+        stderr_fd, stderr_path = tempfile.mkstemp(text=True)
+        file_to_del.append(stderr_path)
         try:
-            status = subprocess.call(command, stderr=subprocess.PIPE) # ignore stderr
+            status = subprocess.call(command, stderr=stderr_fd)
+            os.close(stderr_fd) # ensure flush before reading
+            stderr_fd = None # avoid closing again in finally block
+            fobj = open(stderr_path, 'r')
+            error_message = fobj.read()
+            fobj.close()
+            if not error_message:
+                error_message = _('No diagnosis message was provided')
+            else:
+                error_message = _('The following diagnosis message was provided:\n') + error_message
             if status :
-                raise except_osv(
-                                _('Webkit raise an error' ),
-                                status
-                            )
-        except Exception:
-            for f_to_del in file_to_del :
-                os.unlink(f_to_del)
-
-        pdf = file(out_filename, 'rb').read()
-        for f_to_del in file_to_del :
-            os.unlink(f_to_del)
-
-        os.unlink(out_filename)
+                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()
+        finally:
+            if stderr_fd is not None:
+                os.close(stderr_fd)
+            for f_to_del in file_to_del:
+                try:
+                    os.unlink(f_to_del)
+                except (OSError, IOError), exc:
+                    _logger.error('cannot remove file %s: %s', f_to_del, exc)
         return pdf
 
     def translate_call(self, src):