Skip to content

Commit d2412bc

Browse files
committed
cephadm: make cephadm compatible with jinja2 2.11.3
That version is what's currently being shipped in RPMs on rhel9 Signed-off-by: Adam King <[email protected]>
1 parent e884e3a commit d2412bc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/cephadm/cephadmlib/templating.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,42 @@ class _PackageLoader(jinja2.PackageLoader):
7676
zipimporter function.
7777
"""
7878

79+
def __init__(self, pkg: str, dir: str) -> None:
80+
super().__init__(pkg, dir)
81+
# see the comment in the get_source function below about
82+
# the _loader attribute. This _original_package_name
83+
# attribute is being set up for dealing with the same
84+
# old jinja2 version that comment references.
85+
self._original_package_name = pkg
86+
7987
def get_source(
8088
self, environment: jinja2.Environment, template: str
8189
) -> Tuple[str, str, Optional[Callable[[], bool]]]:
90+
if not hasattr(self, '_loader'):
91+
# This if-block is intended to only be run when we are using an old
92+
# enough version of jinja2 that there is no `_loader` attribute
93+
# on the jinja2.PackageLoader class. Specifically the one within
94+
# the current rhel 9 RPM for jinja2. In versions that old
95+
# there is instead a "provider" attribute pointing to an
96+
# IResourceProvider object that seems to itself have a loader
97+
# that we can use. See the changes in
98+
# https://github.com/pallets/jinja/pull/1082 to get a feel for
99+
# the before and after we're expecting from the PackageLoader.
100+
# Becuase of this special case, mypy will complain about
101+
# accessing the provider attribute when run with newer versions
102+
# of Jinja2 that no longer have the attribute. As we generally expect
103+
# to be running unit tests on versions where this is true, this additional
104+
# assertion is needed to make mypy happy
105+
assert hasattr(self, 'provider')
106+
self._loader = self.provider.loader
82107
if isinstance(self._loader, zipimport.zipimporter):
83108
return self._get_archive_source(template)
84109
return super().get_source(environment, template)
85110

86111
def _get_archive_source(self, template: str) -> Tuple[str, str, None]:
87112
assert isinstance(self._loader, zipimport.zipimporter)
113+
if not hasattr(self, 'package_name'):
114+
self.package_name = self._original_package_name
88115
arelpath = posixpath.join(
89116
self.package_name, self.package_path, template
90117
)

0 commit comments

Comments
 (0)