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:
parent
a58595403d
commit
a1faa4a089
@ -13,50 +13,24 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
import os.path
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
HELP_MESSAGE = (
|
||||
"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):
|
||||
def get_mako_template(template):
|
||||
import mako.lookup
|
||||
|
||||
templates_dir = os.path.join(os.path.dirname(__file__), "templates")
|
||||
|
||||
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)
|
||||
dirs = os.path.join(os.path.dirname(__file__), "templates")
|
||||
lookup = mako.lookup.TemplateLookup(directories=[dirs])
|
||||
return lookup.get_template(template)
|
||||
|
||||
|
||||
def main(*args):
|
||||
if (len(args) < 2 or args[0] != "render"
|
||||
or not all(re.match("^[^=]+=[^=]+$", arg) for arg in args[2:])):
|
||||
print(HELP_MESSAGE, file=sys.stderr)
|
||||
return 1
|
||||
|
||||
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
|
||||
def get_jinja_template(template):
|
||||
import jinja2
|
||||
env = jinja2.Environment(loader=jinja2.PackageLoader("rally.ui",
|
||||
"templates"))
|
||||
return env.get_template(template)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(*sys.argv[1:]))
|
||||
def get_template(template):
|
||||
if template.endswith(".mako"):
|
||||
return get_mako_template(template)
|
||||
return get_jinja_template(template)
|
||||
|
@ -27,6 +27,8 @@ import tempfile
|
||||
|
||||
from six.moves.urllib import parse
|
||||
|
||||
from rally.ui import utils
|
||||
|
||||
|
||||
def use_keystone_v3():
|
||||
"""Alter deployment to use keystone v3."""
|
||||
@ -178,11 +180,9 @@ def main():
|
||||
else:
|
||||
print("Ignoring file %s" % fname)
|
||||
print("Exit statuses: %r" % statuses)
|
||||
|
||||
run(["python", rally_root + "/rally/ui/utils.py", "render",
|
||||
"tests/ci/rally-gate/index.mako"],
|
||||
gzip=False, stdout="rally-plot/extra/index.html")
|
||||
|
||||
template = utils.get_template("ci/index.mako")
|
||||
with open("rally-plot/extra/index.html", "w") as output:
|
||||
output.write(template.render())
|
||||
return any(statuses)
|
||||
|
||||
|
||||
|
@ -63,8 +63,7 @@ rally show keypairs
|
||||
rally -v --rally-debug task start --task $TASK $TASK_ARGS
|
||||
|
||||
mkdir -p rally-plot/extra
|
||||
python $BASE/new/rally/rally/ui/utils.py render\
|
||||
tests/ci/rally-gate/index.mako > rally-plot/extra/index.html
|
||||
python $BASE/new/rally/tests/ci/render.py ci/index.mako > rally-plot/extra/index.html
|
||||
cp $TASK rally-plot/task.txt
|
||||
tar -czf rally-plot/plugins.tar.gz -C $RALLY_PLUGINS_DIR .
|
||||
rally task results | python -m json.tool > rally-plot/results.json
|
||||
|
@ -93,8 +93,8 @@ function main {
|
||||
gzip -9 ${OUTPUT_FILE}
|
||||
done
|
||||
|
||||
python $BASE/new/rally/rally/ui/utils.py render\
|
||||
tests/ci/rally-gate/index_verify.mako ${RESULTS[*]}> ${RESULTS_DIR}/extra/index.html
|
||||
python $BASE/new/rally/tests/ci/render.py ci/index_verify.mako \
|
||||
${RESULTS[*]}> ${RESULTS_DIR}/extra/index.html
|
||||
|
||||
if [[ ${RESULTS[*]} == *"fail"* ]]
|
||||
then
|
||||
|
37
tests/ci/render.py
Normal file
37
tests/ci/render.py
Normal 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))
|
@ -13,7 +13,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mako
|
||||
import mock
|
||||
|
||||
from rally.ui import utils
|
||||
@ -22,29 +21,32 @@ from tests.unit import test
|
||||
|
||||
class ModuleTestCase(test.TestCase):
|
||||
|
||||
def test_get_template(self):
|
||||
self.assertIsInstance(utils.get_template("task/report.mako"),
|
||||
mako.template.Template)
|
||||
def test_get_mako_template(self):
|
||||
try:
|
||||
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):
|
||||
self.assertRaises(ValueError, utils.get_template, "absent_template")
|
||||
def test_get_jinja_template(self):
|
||||
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")
|
||||
def test_main(self, mock_get_template):
|
||||
self.assertEqual(0, utils.main("render", "somepath", "a=1", "b=2"))
|
||||
@mock.patch("rally.ui.utils.get_mako_template")
|
||||
def test_get_template_mako(self, mock_get_mako_template):
|
||||
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_get_template.return_value.render.assert_called_once_with(
|
||||
a="1", b="2"
|
||||
)
|
||||
|
||||
@mock.patch("rally.ui.utils.print", create=True)
|
||||
@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()
|
||||
@mock.patch("rally.ui.utils.get_jinja_template")
|
||||
def test_get_template_jinja(self, mock_get_jinja_template):
|
||||
mock_get_jinja_template.return_value = "fake_template"
|
||||
template = utils.get_jinja_template("template.html")
|
||||
self.assertEqual("fake_template", template)
|
||||
mock_get_jinja_template.assert_called_once_with("template.html")
|
||||
|
Loading…
Reference in New Issue
Block a user