# HG changeset patch # User Adrien Di Mascio # Date 1268950960 -3600 # Node ID f32dcf3925d45ff6e0896ff9a11cfdbe1822684f # Parent bca0873c0d6ece524c2581c4098a5906f26c61ba [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" ? diff -r bca0873c0d6e -r f32dcf3925d4 utils.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 diff -r bca0873c0d6e -r f32dcf3925d4 web/views/basetemplates.py --- 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