[web] get rid of spurious '[Errno 2] No such file or directory' on load stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 18 Mar 2010 23:22:40 +0100
branchstable
changeset 4952 f32dcf3925d4
parent 4950 bca0873c0d6e
child 4961 03e083faefbf
child 4968 79d80cfaab6f
[web] get rid of spurious '[Errno 2] No such file or directory' on load This was caused by can_do_pdf_conversion() which tries to use '/usr/bin/fop'. When fop was not available, an OSError was raised by suprocess.Popen. This changeset first checks for /usr/bin/fop presence, then tries to run fop. If an error occurs, log it instead of printing it. NOTE for later: why do we test explicitly for "/usr/bin/fop" rather than simply "fop" ?
utils.py
web/views/basetemplates.py
--- a/utils.py	Thu Mar 18 17:37:55 2010 +0100
+++ b/utils.py	Thu Mar 18 23:22:40 2010 +0100
@@ -7,6 +7,7 @@
 """
 __docformat__ = "restructuredtext en"
 
+import os
 import sys
 import decimal
 import datetime
@@ -285,31 +286,35 @@
                                                  self.body.getvalue())
 
 
-def can_do_pdf_conversion(__answer=[None]):
-    """pdf conversion depends on
-    * pysixt (python package)
-    * fop 0.9x
-    """
-    if __answer[0] is not None:
-        return __answer[0]
+def _pdf_conversion_availability():
     try:
         import pysixt
     except ImportError:
-        __answer[0] = False
         return False
     from subprocess import Popen, STDOUT
-    import os
+    if not os.path.isfile('/usr/bin/fop'):
+        return False
     try:
         Popen(['/usr/bin/fop', '-q'],
               stdout=open(os.devnull, 'w'),
               stderr=STDOUT)
     except OSError, e:
-        print e
-        __answer[0] = False
+        getLogger('cubicweb').info('fop not usable (%s)', e)
         return False
-    __answer[0] = True
     return True
 
+def can_do_pdf_conversion(__answer_cache=[]):
+    """pdf conversion depends on
+    * pysixt (python package)
+    * fop 0.9x
+
+    NOTE: actual check is done by _pdf_conversion_availability and
+    result is cached
+    """
+    if not __answer_cache: # first time, not in cache
+        __answer_cache.append(_pdf_conversion_availability())
+    return __answer_cache[0]
+
 try:
     # may not be there if cubicweb-web not installed
     from simplejson import dumps, JSONEncoder
--- a/web/views/basetemplates.py	Thu Mar 18 17:37:55 2010 +0100
+++ b/web/views/basetemplates.py	Thu Mar 18 23:22:40 2010 +0100
@@ -270,7 +270,7 @@
 
 if can_do_pdf_conversion():
     try:
-      from xml.etree.cElementTree import ElementTree
+        from xml.etree.cElementTree import ElementTree
     except ImportError: #python2.4
         from elementtree import ElementTree
     from subprocess import Popen as sub