From 74bd5d7b456e0b1c6ac690cde0e9f1e6d07d253b Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Sat, 9 Feb 2008 03:42:00 +0100 Subject: [PATCH] Ajout du traceback pour debogger + facilement darcs-hash:20080209024200-af139-c87e0534b390a7f84229eefbcd405a9360c68a39.gz --- bcfg2/plugins/Python.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bcfg2/plugins/Python.py b/bcfg2/plugins/Python.py index aab4b384..75b68830 100644 --- a/bcfg2/plugins/Python.py +++ b/bcfg2/plugins/Python.py @@ -27,6 +27,7 @@ __all__ = ["Python"] import logging, lxml.etree, posixpath, re, os, sys from cStringIO import StringIO import Bcfg2.Server.Plugin +import traceback sys.path.append('/usr/scripts/bcfg2') import pygen @@ -55,6 +56,15 @@ else: # Dico nom de fichier -> code includes = {} +def log_traceback(fname, section, exn): + logger.error('Python %s error: %s: %s: %s' % (section, fname, str(exn.__class__).split('.', 2)[1], str(exn))) + s = StringIO() + sys.stderr = s + traceback.print_exc() + sys.stderr = sys.__stderr__ + for line in s.getvalue().splitlines(): + logger.error('Python %s error: -> %s' % (section, line)) + def dump(env, incfile): exec(includes[incfile], env) @@ -68,7 +78,7 @@ def load_file(filename): try: return pygen.load(filename, os.path.dirname(filename) + "/." + os.path.basename(filename) + ".COMPILED") except Exception, e: - logger.error('Python compilation error: %s: %s' % (str(e.__class__).split('.', 2)[1], str(e))) + log_traceback(filename, 'compilation', e) return None class PythonProperties(Bcfg2.Server.Plugin.SingleXMLFileBacked): @@ -115,7 +125,7 @@ class Python(Bcfg2.Server.Plugin.Plugin): def BuildEntry(self, entry, metadata): '''Construit le fichier''' - code = self.codes[entry.get('name')] + code = self.codes[entry.get('name')] fname = entry.get('realname', entry.get('name')) debug("building config file: %s" % fname, 'blue') env = pygen.Environment() @@ -126,22 +136,13 @@ class Python(Bcfg2.Server.Plugin.Plugin): env["info"] = { 'owner': 'root', 'group': 'root', 'perms': 0644 } - env.included = set([]) + env.included = set([]) try: include(env, "common") - entry.text = pygen.generate(code, env) + entry.text = pygen.generate(code, env).decode("UTF-8") debug(entry.text) except Exception, e: - import traceback - from cStringIO import StringIO - s = StringIO() - sys.stderr = s - traceback.print_exc() - sys.stderr = sys.__stderr__ - traceback = s.getvalue() - logger.error('Python exec error: %s: %s' % (str(e.__class__).split('.', 2)[1], str(e))) - for line in traceback.splitlines(): - logger.error('Python exec error: -> %s' % line) + log_traceback(fname, 'exec', e) raise Bcfg2.Server.Plugin.PluginExecutionError entry.attrib['owner'] = env["info"]['owner'] entry.attrib['group'] = env["info"]['group']