From 00d0cc4b992b4fbdaf543daeacc17ba0c257fdf4 Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Thu, 28 Apr 2022 12:17:52 -0600 Subject: [PATCH 1/3] Remove cheetah from templater, as well as tests --- cloudinit/templater.py | 39 ++++-------------------------- tests/unittests/test_templating.py | 30 ----------------------- 2 files changed, 5 insertions(+), 64 deletions(-) diff --git a/cloudinit/templater.py b/cloudinit/templater.py index d8af5096062..761b8ea5c1c 100644 --- a/cloudinit/templater.py +++ b/cloudinit/templater.py @@ -14,13 +14,6 @@ import re import sys -try: - from Cheetah.Template import Template as CTemplate - - CHEETAH_AVAILABLE = True -except (ImportError, AttributeError): - CHEETAH_AVAILABLE = False - try: from jinja2 import DebugUndefined as JUndefined from jinja2 import Template as JTemplate @@ -96,8 +89,6 @@ def replacer(match): def detect_template(text): - def cheetah_render(content, params): - return CTemplate(content, searchList=[params]).respond() def jinja_render(content, params): # keep_trailing_newline is in jinja2 2.7+, not 2.6 @@ -119,14 +110,10 @@ def jinja_render(content, params): rest = "" type_match = TYPE_MATCHER.match(ident) if not type_match: - if CHEETAH_AVAILABLE: - LOG.debug("Using Cheetah as the renderer for unknown template.") - return ("cheetah", cheetah_render, text) - else: - return ("basic", basic_render, text) + return ("basic", basic_render, text) else: template_type = type_match.group(1).lower().strip() - if template_type not in ("jinja", "cheetah", "basic"): + if template_type not in ("jinja", "basic"): raise ValueError( "Unknown template rendering type '%s' requested" % template_type @@ -139,14 +126,6 @@ def jinja_render(content, params): return ("basic", basic_render, rest) elif template_type == "jinja" and JINJA_AVAILABLE: return ("jinja", jinja_render, rest) - if template_type == "cheetah" and not CHEETAH_AVAILABLE: - LOG.warning( - "Cheetah not available as the selected renderer for" - " desired template, reverting to the basic renderer." - ) - return ("basic", basic_render, rest) - elif template_type == "cheetah" and CHEETAH_AVAILABLE: - return ("cheetah", cheetah_render, rest) # Only thing left over is the basic renderer (it is always available). return ("basic", basic_render, rest) @@ -154,11 +133,8 @@ def jinja_render(content, params): def render_from_file(fn, params): if not params: params = {} - # jinja in python2 uses unicode internally. All py2 str will be decoded. - # If it is given a str that has non-ascii then it will raise a - # UnicodeDecodeError. So we explicitly convert to unicode type here. template_type, renderer, content = detect_template( - util.load_file(fn, decode=False).decode("utf-8") + util.load_file(fn) ) LOG.debug("Rendering content of '%s' using renderer %s", fn, template_type) return renderer(content, params) @@ -170,15 +146,13 @@ def render_to_file(fn, outfn, params, mode=0o644): def render_string_to_file(content, outfn, params, mode=0o644): - """Render string (or py2 unicode) to file. - Warning: py2 str with non-ascii chars will cause UnicodeDecodeError.""" + """Render string""" contents = render_string(content, params) util.write_file(outfn, contents, mode=mode) def render_string(content, params): - """Render string (or py2 unicode). - Warning: py2 str with non-ascii chars will cause UnicodeDecodeError.""" + """Render string""" if not params: params = {} _template_type, renderer, content = detect_template(content) @@ -196,6 +170,3 @@ def render_cloudcfg(variant, template, output): sys.stdout.write(contents) else: write_file(output, contents, omode="w") - - -# vi: ts=4 expandtab diff --git a/tests/unittests/test_templating.py b/tests/unittests/test_templating.py index 3a0195bf292..0cdedbe760a 100644 --- a/tests/unittests/test_templating.py +++ b/tests/unittests/test_templating.py @@ -10,14 +10,6 @@ from cloudinit.util import load_file, write_file from tests.unittests import helpers as test_helpers -try: - import Cheetah - - HAS_CHEETAH = True - c = Cheetah # make pyflakes and pylint happy, as Cheetah is not used here -except ImportError: - HAS_CHEETAH = False - class TestTemplates(test_helpers.CiTestCase): @@ -52,28 +44,6 @@ def test_render_basic(self): out_data = templater.basic_render(in_data, {"b": 2}) self.assertEqual(expected_data.strip(), out_data) - @test_helpers.skipIf(not HAS_CHEETAH, "cheetah renderer not available") - def test_detection(self): - blob = "## template:cheetah" - - (template_type, _renderer, contents) = templater.detect_template(blob) - self.assertIn("cheetah", template_type) - self.assertEqual("", contents.strip()) - - blob = "blahblah $blah" - (template_type, _renderer, _contents) = templater.detect_template(blob) - self.assertIn("cheetah", template_type) - self.assertEqual(blob, contents) - - blob = "##template:something-new" - self.assertRaises(ValueError, templater.detect_template, blob) - - def test_render_cheetah(self): - blob = """## template:cheetah -$a,$b""" - c = templater.render_string(blob, {"a": 1, "b": 2}) - self.assertEqual("1,2", c) - def test_render_jinja(self): blob = """## template:jinja {{a}},{{b}}""" From 965e34ac1ae738bdd8c405a2cf1e376348654744 Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Thu, 28 Apr 2022 12:41:04 -0600 Subject: [PATCH 2/3] fmt --- cloudinit/templater.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cloudinit/templater.py b/cloudinit/templater.py index 761b8ea5c1c..298eaf6b917 100644 --- a/cloudinit/templater.py +++ b/cloudinit/templater.py @@ -89,7 +89,6 @@ def replacer(match): def detect_template(text): - def jinja_render(content, params): # keep_trailing_newline is in jinja2 2.7+, not 2.6 add = "\n" if content.endswith("\n") else "" @@ -133,9 +132,7 @@ def jinja_render(content, params): def render_from_file(fn, params): if not params: params = {} - template_type, renderer, content = detect_template( - util.load_file(fn) - ) + template_type, renderer, content = detect_template(util.load_file(fn)) LOG.debug("Rendering content of '%s' using renderer %s", fn, template_type) return renderer(content, params) From ae46db77c8f14561adc3ff56d6e1cf4b93a8d95a Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Fri, 29 Apr 2022 10:49:24 -0600 Subject: [PATCH 3/3] drop cheetah in __init__.py --- tests/unittests/__init__.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/unittests/__init__.py b/tests/unittests/__init__.py index 657cb399b76..e69de29bb2d 100644 --- a/tests/unittests/__init__.py +++ b/tests/unittests/__init__.py @@ -1,12 +0,0 @@ -# This file is part of cloud-init. See LICENSE file for license information. - -try: - # For test cases, avoid the following UserWarning to stderr: - # You don't have the C version of NameMapper installed ... - from Cheetah import NameMapper as _nm - - _nm.C_VERSION = True -except ImportError: - pass - -# vi: ts=4 expandtab