Commit 0c5782c7 authored by Nicolas Dandrimont's avatar Nicolas Dandrimont

[bcfg2/*/Python.py] Ajout de facilités de logging

darcs-hash:20091102205722-ffbb2-4fd8cd1cdc940cb39273b44b4d2a7dbdd24654b4.gz
parent a4a6b5c4
......@@ -73,10 +73,10 @@ def include(env, incfile):
env.included.add(incfile)
exec(includes[incfile], env)
def load_file(filename):
def load_file(filename, logger):
'''Charge un script et affiche un message d'erreur en cas d'exception'''
try:
return pygen.load(filename, os.path.dirname(filename) + "/." + os.path.basename(filename) + ".COMPILED")
return pygen.load(filename, os.path.dirname(filename) + "/." + os.path.basename(filename) + ".COMPILED", logger)
except Exception, e:
log_traceback(filename, 'compilation', e)
return None
......@@ -139,7 +139,7 @@ class Python(Bcfg2.Server.Plugin.Plugin,Bcfg2.Server.Plugin.Generator):
env.included = set([])
try:
include(env, "common")
text = pygen.generate(code, env)
text = pygen.generate(code, env, logger)
except Exception, e:
log_traceback(fname, 'exec', e)
raise Bcfg2.Server.Plugin.PluginExecutionError
......@@ -181,7 +181,7 @@ class Python(Bcfg2.Server.Plugin.Plugin,Bcfg2.Server.Plugin.Generator):
identifier = path[len(self.include)+1:-3]
if action in ['exists', 'created', 'changed']:
debug("adding include file: %s" % identifier, 'green')
includes[identifier] = load_file(path)
includes[identifier] = load_file(path, logger)
elif action == 'deleted':
debug("deleting include file: %s" % identifier, 'red')
del includes[identifier]
......@@ -194,10 +194,10 @@ class Python(Bcfg2.Server.Plugin.Plugin,Bcfg2.Server.Plugin.Generator):
identifier = path[len(self.data):]
if action in ['exists', 'created']:
debug("adding config file: %s" % identifier, 'green')
self.codes[identifier] = load_file(path)
self.codes[identifier] = load_file(path, logger)
self.Entries['ConfigFile'][identifier] = self.BuildEntry
elif action == 'changed':
self.codes[identifier] = load_file(path)
self.codes[identifier] = load_file(path, logger)
elif action == 'deleted':
debug("deleting config file: %s" % identifier, 'red')
del self.codes[identifier]
......
......@@ -105,7 +105,7 @@ __re_special_line = re.compile(r"^([ \t]*)(@|%)(.*)$", re.MULTILINE)
__re_affectation = re.compile(r"([a-zA-Z_][a-zA-Z_0-9]*)[ \t]*=")
__re_space_sep = re.compile(r"([^ \t]*)[ \t]+=?(.*)")
def compileSource(source, filename=""):
def compileSource(source, filename="", logger = None):
'''Compile un script'''
# On commence par remplacer les lignes de la forme
# @xxx par out("xxx")
......@@ -144,12 +144,14 @@ def compileSource(source, filename=""):
newsource.write(m.group(2))
newsource.write("))\n")
newsource.write(source[start:])
if logger:
logger.info(newsource.getvalue())
return compile(newsource.getvalue(), filename, "exec")
def generate(code, environment=None):
def generate(code, environment=None, logger = None):
'''Évalue un script'''
if type(code) == str:
code = compileSource(code)
code = compileSource(code, logger = logger)
if not environment:
environment = Environment()
environment.stream = StringIO()
......@@ -165,7 +167,7 @@ def generate(code, environment=None):
sys.stdout = save_stdout
return environment.stream.getvalue()
def load(fname, cfname=None):
def load(fname, cfname=None, logger=None):
'''Charge un script et le compile, en créant/utilisant un fichier de
cache'''
if not cfname:
......@@ -173,7 +175,7 @@ def load(fname, cfname=None):
if os.path.exists(cfname) and os.stat(fname).st_mtime <= os.stat(cfname).st_mtime:
code = marshal.load(file(cfname))
else:
code = compileSource(file(fname).read(), fname)
code = compileSource(file(fname).read(), fname, logger)
cfile = open(cfname, "w")
marshal.dump(code, cfile)
cfile.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment