Skip to content

Commit

Permalink
Avoid running get_app more than once (#4115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Apr 17, 2024
1 parent 9e0f6f1 commit 365576e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


_logger = logging.getLogger(__package__)
_CACHED_APP = None


class App:
Expand Down Expand Up @@ -386,8 +387,19 @@ def _sanitize_list_options(tag_list: list[str]) -> list[str]:


@lru_cache
def get_app(*, offline: bool | None = None) -> App:
def get_app(*, offline: bool | None = None, cached: bool = False) -> App:
"""Return the application instance, caching the return value."""
# Avoids ever running the app initialization twice if cached argument
# is mentioned.
if cached:
if offline is not None:
msg = (
"get_app should never be called with other arguments when cached=True."
)
raise RuntimeError(msg)
if cached and _CACHED_APP is not None:
return _CACHED_APP

if offline is None:
offline = default_options.offline

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def __init__( # pylint: disable=too-many-arguments
else:
self.options = options
self.profile = []
self.app = app or get_app(offline=True)
self.app = app or get_app(cached=True)

if profile_name:
self.profile = PROFILES[profile_name]
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ def uncook(value: str, *, implicit: bool = False) -> str:

except jinja2.exceptions.TemplateSyntaxError as exc:
return "", str(exc.message), "invalid"
# pylint: disable=c-extension-no-member
except (NotImplementedError, black.parsing.InvalidInput) as exc:
# black is not able to recognize all valid jinja2 templates, so we
# just ignore InvalidInput errors.
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _run(self) -> list[MatchError]:

# -- phase 1 : syntax check in parallel --
if not self.skip_ansible_syntax_check:
app = get_app(offline=True)
app = get_app(cached=True)

def worker(lintable: Lintable) -> list[MatchError]:
return self._get_ansible_syntax_check_matches(
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def _rolepath(basedir: str, role: str) -> str | None:
path_dwim(basedir, os.path.join("..", role)),
]

for loc in get_app(offline=True).runtime.config.default_roles_path:
for loc in get_app(cached=True).runtime.config.default_roles_path:
loc = os.path.expanduser(loc)
possible_paths.append(path_dwim(loc, role))

Expand Down

0 comments on commit 365576e

Please sign in to comment.