add support for Jinja2 in task file
[yardstick.git] / yardstick / common / task_template.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # yardstick: this file is copied from rally and slightly modified
9 ##############################################################################
10 import re
11 import jinja2
12 import jinja2.meta
13
14
15 class TaskTemplate(object):
16     @classmethod
17     def render(cls, task_template, **kwargs):
18         """Render jinja2 task template to Yardstick input task.
19
20         :param task_template: string that contains template
21         :param kwargs: Dict with template arguments
22         :returns:rendered template str
23         """
24
25         from six.moves import builtins
26
27         ast = jinja2.Environment().parse(task_template)
28         required_kwargs = jinja2.meta.find_undeclared_variables(ast)
29
30         missing = set(required_kwargs) - set(kwargs) - set(dir(builtins))
31         real_missing = [mis for mis in missing
32                         if is_really_missing(mis, task_template)]
33
34         if real_missing:
35             multi_msg = ("Please specify next template task arguments:%s")
36             single_msg = ("Please specify template task argument:%s")
37             raise TypeError((len(real_missing) > 1 and multi_msg or single_msg)
38                             % ", ".join(real_missing))
39         return jinja2.Template(task_template).render(**kwargs)
40
41
42 def is_really_missing(mis, task_template):
43     # Removing variables that have default values from
44     # missing. Construction that won't be properly
45     # check is {% set x = x or 1}
46     if re.search(mis.join(["{%\s*set\s+", "\s*=\s*", "[^\w]+"]),
47                  task_template):
48         return False
49     # Also check for a default filter which can show up as
50     # a missing variable
51     if re.search(mis + "\s*\|\s*default\(", task_template):
52         return False
53     return True