diff --git a/plugins/action/stack_host_copy.py b/plugins/action/stack_host_copy.py index 8fa247270e..bc418e5e99 100755 --- a/plugins/action/stack_host_copy.py +++ b/plugins/action/stack_host_copy.py @@ -8,12 +8,14 @@ from typing import Any from ansible.errors import AnsibleError from ansible.module_utils.parsing.convert_bool import boolean as _to_bool +from ansible.plugins.action import copy as _copy_action from ansible.plugins.action.copy import ActionModule as CopyActionModule from ansible.plugins.action.copy import _walk_dirs from ansible.template import trust_as_template from ansible.utils.hashing import checksum _IS_STACK_HOST_EXPR = trust_as_template("{{ IS_STACK_HOST | bool }}") +_BYTECODE_CACHE_DIR = "__pycache__" _TREE_CHECK_ARGS = frozenset( { "src", @@ -41,6 +43,30 @@ def _parents(rel: str) -> list[str]: return [str(parent) for parent in PurePath(rel).parents if str(parent) != "."] +def _walk_dirs_without_bytecode_caches(*args: Any, **kwargs: Any) -> dict[str, Any]: + """Walk a source tree like the copy action, minus CPython bytecode caches. + + Args: + *args: Positional arguments of the copy action's ``_walk_dirs``. + **kwargs: Keyword arguments of the copy action's ``_walk_dirs``. + + Returns: + The ``_walk_dirs`` result without the files, directories and symlinks + at or below a ``__pycache__`` directory. + """ + tree = _walk_dirs(*args, **kwargs) + if not isinstance(tree["symlinks"], list): + return tree + return { + kind: [ + entry + for entry in entries + if _BYTECODE_CACHE_DIR not in PurePath(entry[1]).parts + ] + for kind, entries in tree.items() + } + + class ActionModule(CopyActionModule): def run( self, tmp: Any = None, task_vars: dict[str, Any] | None = None @@ -60,7 +86,12 @@ class ActionModule(CopyActionModule): unchanged = self._unchanged_tree(task_vars) if unchanged is not None: return unchanged - return super().run(tmp=tmp, task_vars=task_vars) + walk = _copy_action._walk_dirs + _copy_action._walk_dirs = _walk_dirs_without_bytecode_caches + try: + return super().run(tmp=tmp, task_vars=task_vars) + finally: + _copy_action._walk_dirs = walk def _unchanged_tree(self, task_vars: dict[str, Any]) -> dict[str, Any] | None: """Settle a directory copy whose files all match in one remote call. @@ -96,7 +127,7 @@ class ActionModule(CopyActionModule): return None source = source if source.endswith(os.path.sep) else source + os.path.sep shell = self._connection._shell - tree = _walk_dirs( + tree = _walk_dirs_without_bytecode_caches( source, local_follow=_to_bool(args.get("local_follow", True), strict=False), trailing_slash_detector=shell.path_has_trailing_slash, diff --git a/tests/unit/python/plugins/action/test_stack_host_copy.py b/tests/unit/python/plugins/action/test_stack_host_copy.py index dddb8f650d..f7e4744f34 100644 --- a/tests/unit/python/plugins/action/test_stack_host_copy.py +++ b/tests/unit/python/plugins/action/test_stack_host_copy.py @@ -6,7 +6,12 @@ from pathlib import Path from typing import ClassVar from unittest.mock import patch -from plugins.action.stack_host_copy import ActionModule +from ansible.plugins.action import copy as copy_action + +from plugins.action.stack_host_copy import ( + ActionModule, + _walk_dirs_without_bytecode_caches, +) class _FakeTask: @@ -173,9 +178,11 @@ class _FakeRemote: def __init__(self, entries): self.entries = dict(entries) self.calls = [] + self.args = [] def __call__(self, module_name=None, module_args=None, task_vars=None, **kwargs): self.calls.append(module_name) + self.args.append(module_args) if module_name == "ansible.legacy.file_checksums": return { "changed": False, @@ -374,6 +381,93 @@ class TestStackHostCopyTree(unittest.TestCase): self.assertFalse(result["changed"]) self.assertEqual(remote.calls, ["ansible.legacy.file_checksums"]) + def _add_bytecode_caches(self): + for rel in ( + "__pycache__/mod.cpython-314.pyc", + "nested/__pycache__/mod.cpython-313.pyc", + ): + (self.src / rel).parent.mkdir(parents=True, exist_ok=True) + (self.src / rel).write_bytes(b"bytecode") + + def test_bytecode_caches_stay_out_of_the_tree_check(self): + self._add_bytecode_caches() + remote = _FakeRemote(self._synced()) + + result = self._run(remote) + + self.assertFalse(result["changed"]) + self.assertEqual(remote.calls, ["ansible.legacy.file_checksums"]) + self.assertEqual(sorted(remote.args[0]["paths"]), sorted(self._synced())) + + def test_bytecode_caches_are_not_transferred(self): + self._add_bytecode_caches() + entries = self._synced() + entries[self.DEST + "icon.png"] = (b"old", 0o644) + remote = _FakeRemote(entries) + + result = self._run(remote) + + self.assertTrue(result["changed"]) + self.assertIn("ansible.legacy.copy", remote.calls) + touched = [str(value) for args in remote.args for value in args.values()] + self.assertEqual([value for value in touched if "__pycache__" in value], []) + + def test_bytecode_outside_a_cache_directory_stays_in_the_copy(self): + (self.src / "legacy.pyc").write_bytes(b"bytecode") + remote = _FakeRemote(self._synced()) + + result = self._run(remote) + + self.assertTrue(result["changed"]) + self.assertIn(self.DEST + "legacy.pyc", remote.args[0]["paths"]) + self.assertIn("ansible.legacy.copy", remote.calls) + + def test_symlinked_root_passes_the_cache_filter_unchanged(self): + with tempfile.TemporaryDirectory() as links: + root = Path(links) / "root" + root.symlink_to(self.src) + + filtered = _walk_dirs_without_bytecode_caches(str(root), local_follow=False) + + self.assertEqual(filtered["symlinks"], (str(self.src), "root")) + + +class TestStackHostCopyWalkSwap(unittest.TestCase): + def _action(self): + task = _FakeTask(args={"src": "x", "dest": "/opt/compose/app/x"}) + return _make_action(task, _FakeTemplar(is_stack_host=True)) + + def test_copy_action_walks_without_caches_only_during_the_run(self): + original = copy_action._walk_dirs + seen = [] + + def copy_run(**kwargs): + seen.append(copy_action._walk_dirs) + return {"changed": False} + + with patch( + "plugins.action.stack_host_copy.CopyActionModule.run", + side_effect=copy_run, + ): + self._action().run(task_vars={}) + + self.assertEqual(seen, [_walk_dirs_without_bytecode_caches]) + self.assertIs(copy_action._walk_dirs, original) + + def test_copy_action_walk_is_restored_when_the_copy_raises(self): + original = copy_action._walk_dirs + + with ( + patch( + "plugins.action.stack_host_copy.CopyActionModule.run", + side_effect=RuntimeError("boom"), + ), + self.assertRaises(RuntimeError), + ): + self._action().run(task_vars={}) + + self.assertIs(copy_action._walk_dirs, original) + if __name__ == "__main__": unittest.main()