From 6d0a7c8e706ecb03c1bcb779dd05bde4ed620f26 Mon Sep 17 00:00:00 2001 From: John Kristensen Date: Sat, 6 Jun 2026 13:59:01 +1000 Subject: [PATCH] Support copying files to a directory Since commit dbe0b1125f the `copy()` function no longer supports copying files to a directory. The change in behaviour has lead to at least one regression[1]. This restore the previous behaviour. [1] https://github.com/getpelican/pelican/issues/3442 --- pelican/tests/test_utils.py | 12 ++++++++++++ pelican/utils.py | 3 +++ 2 files changed, 15 insertions(+) diff --git a/pelican/tests/test_utils.py b/pelican/tests/test_utils.py index aad07555..fc690274 100644 --- a/pelican/tests/test_utils.py +++ b/pelican/tests/test_utils.py @@ -868,6 +868,18 @@ class TestCopy(unittest.TestCase): self._exist_dir("b0", "b1", "b2", "b3", "b") self._exist_file("b0", "b1", "b2", "b3", "b", "a.txt") + def test_copy_file_to_existing_dir(self): + self._create_dir("a") + self._create_file("a", "a.txt") + self._create_dir("b0", "b1") + utils.copy( + os.path.join(self.root_dir, "a", "a.txt"), + os.path.join(self.root_dir, "b0", "b1"), + ) + self._exist_dir("b0") + self._exist_dir("b0", "b1") + self._exist_file("b0", "b1", "a.txt") + class TestDateFormatter(unittest.TestCase): """Tests that the output of DateFormatter jinja filter is same as diff --git a/pelican/utils.py b/pelican/utils.py index 90d1a3fd..16245d63 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -366,6 +366,9 @@ def copy(source: str, destination: str, ignores: Iterable[str] | None = None) -> def copy_file(source: str, destination: str) -> None: """Copy a file""" + if os.path.isdir(destination): + destination = os.path.join(destination, os.path.basename(source)) + try: shutil.copyfile(source, destination) except OSError as e: