Support jinja2 in rally.ui.utils

Use pkgutil to get base templates path.
Move ci templates to rally.ui package.
Remove cli functionality and use rally.ui directly in CI code.

Change-Id: If88ba2ed851eb9796ef5fae84398b1c185dbab0c
This commit is contained in:
Sergey Skripnick 2015-11-02 11:47:47 +02:00
parent a58595403d
commit a1faa4a089
8 changed files with 84 additions and 72 deletions

View File

@ -13,50 +13,24 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from __future__ import print_function
import os.path import os.path
import re
import sys
HELP_MESSAGE = ( def get_mako_template(template):
"Usage:\n\t"
"utils.py render <lookup/path/to/template.mako>"
"[<key-1>=<value-1> <key-2>=<value-2> ...]\n\n\t"
"Where key-1,value-1 and key-2,value-2 are key pairs of template.")
def get_template(template_path):
import mako.lookup import mako.lookup
dirs = os.path.join(os.path.dirname(__file__), "templates")
templates_dir = os.path.join(os.path.dirname(__file__), "templates") lookup = mako.lookup.TemplateLookup(directories=[dirs])
return lookup.get_template(template)
lookup_dirs = [
templates_dir,
os.path.abspath(os.path.join(templates_dir, "..", "..", ".."))
]
lookup = mako.lookup.TemplateLookup(directories=lookup_dirs)
try:
return lookup.get_template(template_path)
except mako.exceptions.TopLevelLookupException as e:
raise ValueError(e)
def main(*args): def get_jinja_template(template):
if (len(args) < 2 or args[0] != "render" import jinja2
or not all(re.match("^[^=]+=[^=]+$", arg) for arg in args[2:])): env = jinja2.Environment(loader=jinja2.PackageLoader("rally.ui",
print(HELP_MESSAGE, file=sys.stderr) "templates"))
return 1 return env.get_template(template)
try:
render_kwargs = dict([arg.split("=") for arg in args[2:]])
print(get_template(args[1]).render(**render_kwargs))
except ValueError as e:
print(str(e), file=sys.stderr)
return 1
return 0
if __name__ == "__main__": def get_template(template):
sys.exit(main(*sys.argv[1:])) if template.endswith(".mako"):
return get_mako_template(template)
return get_jinja_template(template)

View File

@ -27,6 +27,8 @@ import tempfile
from six.moves.urllib import parse from six.moves.urllib import parse
from rally.ui import utils
def use_keystone_v3(): def use_keystone_v3():
"""Alter deployment to use keystone v3.""" """Alter deployment to use keystone v3."""
@ -178,11 +180,9 @@ def main():
else: else:
print("Ignoring file %s" % fname) print("Ignoring file %s" % fname)
print("Exit statuses: %r" % statuses) print("Exit statuses: %r" % statuses)
template = utils.get_template("ci/index.mako")
run(["python", rally_root + "/rally/ui/utils.py", "render", with open("rally-plot/extra/index.html", "w") as output:
"tests/ci/rally-gate/index.mako"], output.write(template.render())
gzip=False, stdout="rally-plot/extra/index.html")
return any(statuses) return any(statuses)

View File

@ -63,8 +63,7 @@ rally show keypairs
rally -v --rally-debug task start --task $TASK $TASK_ARGS rally -v --rally-debug task start --task $TASK $TASK_ARGS
mkdir -p rally-plot/extra mkdir -p rally-plot/extra
python $BASE/new/rally/rally/ui/utils.py render\ python $BASE/new/rally/tests/ci/render.py ci/index.mako > rally-plot/extra/index.html
tests/ci/rally-gate/index.mako > rally-plot/extra/index.html
cp $TASK rally-plot/task.txt cp $TASK rally-plot/task.txt
tar -czf rally-plot/plugins.tar.gz -C $RALLY_PLUGINS_DIR . tar -czf rally-plot/plugins.tar.gz -C $RALLY_PLUGINS_DIR .
rally task results | python -m json.tool > rally-plot/results.json rally task results | python -m json.tool > rally-plot/results.json

View File

@ -93,8 +93,8 @@ function main {
gzip -9 ${OUTPUT_FILE} gzip -9 ${OUTPUT_FILE}
done done
python $BASE/new/rally/rally/ui/utils.py render\ python $BASE/new/rally/tests/ci/render.py ci/index_verify.mako \
tests/ci/rally-gate/index_verify.mako ${RESULTS[*]}> ${RESULTS_DIR}/extra/index.html ${RESULTS[*]}> ${RESULTS_DIR}/extra/index.html
if [[ ${RESULTS[*]} == *"fail"* ]] if [[ ${RESULTS[*]} == *"fail"* ]]
then then

37
tests/ci/render.py Normal file
View File

@ -0,0 +1,37 @@
# Copyright 2015: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import print_function
import re
import sys
from rally.ui import utils
HELP_MESSAGE = (
"Usage:\n\t"
"render.py ci/template.mako"
"[<key-1>=<value-1> <key-2>=<value-2> ...]\n\n\t"
"Where key-1,value-1 and key-2,value-2 are key pairs of template.")
if __name__ == "__main__":
args = sys.argv
if (len(args) < 1 or not all(re.match("^[^=]+=[^=]+$",
arg) for arg in args[2:])):
print(HELP_MESSAGE, file=sys.stderr)
sys.exit(1)
render_kwargs = dict([arg.split("=") for arg in args[2:]])
print(utils.get_template(args[1]).render(**render_kwargs))

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mako
import mock import mock
from rally.ui import utils from rally.ui import utils
@ -22,29 +21,32 @@ from tests.unit import test
class ModuleTestCase(test.TestCase): class ModuleTestCase(test.TestCase):
def test_get_template(self): def test_get_mako_template(self):
self.assertIsInstance(utils.get_template("task/report.mako"), try:
mako.template.Template) import mako
except ImportError:
self.skip("No mako module. Skipping test.")
template = utils.get_mako_template("ci/index.mako")
self.assertIsInstance(template, mako.template.Template)
def test_get_template_raises(self): def test_get_jinja_template(self):
self.assertRaises(ValueError, utils.get_template, "absent_template") try:
import jinja2
except ImportError:
self.skip("Jinja not installed. Skipping test.")
self.assertRaises(jinja2.exceptions.TemplateNotFound,
utils.get_jinja_template, "nonexistent")
@mock.patch("rally.ui.utils.get_template") @mock.patch("rally.ui.utils.get_mako_template")
def test_main(self, mock_get_template): def test_get_template_mako(self, mock_get_mako_template):
self.assertEqual(0, utils.main("render", "somepath", "a=1", "b=2")) mock_get_mako_template.return_value = "fake_template"
template = utils.get_mako_template("template.mako")
self.assertEqual("fake_template", template)
mock_get_mako_template.assert_called_once_with("template.mako")
mock_get_template.assert_called_once_with("somepath") @mock.patch("rally.ui.utils.get_jinja_template")
mock_get_template.return_value.render.assert_called_once_with( def test_get_template_jinja(self, mock_get_jinja_template):
a="1", b="2" mock_get_jinja_template.return_value = "fake_template"
) template = utils.get_jinja_template("template.html")
self.assertEqual("fake_template", template)
@mock.patch("rally.ui.utils.print", create=True) mock_get_jinja_template.assert_called_once_with("template.html")
@mock.patch("rally.ui.utils.sys.stderr")
def test_main_bad_input(self, mock_stderr, mock_print):
self.assertTrue(utils.HELP_MESSAGE.startswith("Usage:"))
for args in ([], ["not_a_render"], ["render"],
["render", "expected_arg", "unexpected_arg"]):
self.assertEqual(1, utils.main(*args))
mock_print.assert_called_once_with(utils.HELP_MESSAGE,
file=mock_stderr)
mock_print.reset_mock()