summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Lint/Jinja2.py
diff options
context:
space:
mode:
authorGordon Messmer <gordon@dragonsdawn.net>2014-09-11 11:22:03 -0700
committerGordon Messmer <gordon@dragonsdawn.net>2014-09-11 11:22:03 -0700
commit4462816a4a2c26ef7fc94f51b6485feb1ab44c27 (patch)
tree09c73c42e98d95f3ea28910d13c01ce3077003dc /src/lib/Bcfg2/Server/Lint/Jinja2.py
parent92f64c0aa166eca93cdf56e7e2e870100c3cb5bc (diff)
downloadbcfg2-4462816a4a2c26ef7fc94f51b6485feb1ab44c27.tar.gz
bcfg2-4462816a4a2c26ef7fc94f51b6485feb1ab44c27.tar.bz2
bcfg2-4462816a4a2c26ef7fc94f51b6485feb1ab44c27.zip
First pass at Jinja2 support for Cfg.
Diffstat (limited to 'src/lib/Bcfg2/Server/Lint/Jinja2.py')
-rwxr-xr-xsrc/lib/Bcfg2/Server/Lint/Jinja2.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/lib/Bcfg2/Server/Lint/Jinja2.py b/src/lib/Bcfg2/Server/Lint/Jinja2.py
new file mode 100755
index 000000000..3112d2a6e
--- /dev/null
+++ b/src/lib/Bcfg2/Server/Lint/Jinja2.py
@@ -0,0 +1,40 @@
+""" Check Jinja2 templates for syntax errors. """
+
+import sys
+import Bcfg2.Server.Lint
+from jinja2 import Template, TemplateSyntaxError
+from Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator import CfgJinja2Generator
+
+
+class Jinja2(Bcfg2.Server.Lint.ServerPlugin):
+ """ Check Jinja2 templates for syntax errors. """
+
+ def Run(self):
+ if 'Cfg' in self.core.plugins:
+ self.check_cfg()
+
+ @classmethod
+ def Errors(cls):
+ return {"jinja2-syntax-error": "error",
+ "unknown-jinja2-error": "error"}
+
+ def check_template(self, entry):
+ """ Generic check for all jinja2 templates (XML and text) """
+ try:
+ Template(entry.data.decode(entry.encoding))
+ except TemplateSyntaxError:
+ err = sys.exc_info()[1]
+ self.LintError("jinja2-syntax-error",
+ "Jinja2 syntax error in %s: %s" % (entry.name, err))
+ except:
+ err = sys.exc_info()[1]
+ self.LintError("unknown-jinja2-error",
+ "Unknown Jinja2 error in %s: %s" % (entry.name, err))
+
+ def check_cfg(self):
+ """ Check jinja2 templates in Cfg for syntax errors. """
+ for entryset in self.core.plugins['Cfg'].entries.values():
+ for entry in entryset.entries.values():
+ if (self.HandlesFile(entry.name) and
+ isinstance(entry, CfgJinja2Generator)):
+ self.check_template(entry)