Skip to content

[filesystem] Preserve sibling ZIP files when writing non-ZIP extensions - #4342

Merged
anthrotype merged 2 commits into
fonttools:mainfrom
jdotc1:jdotc1/pr-16-fonttools-archive-staging-20261021
Oct 22, 2026
Merged

anthrotype merged 2 commits into
fonttools:mainfrom
jdotc1:jdotc1/pr-16-fonttools-archive-staging-20261021

Conversation

@jdotc1

@jdotc1 jdotc1 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Writing a .ufoz or extensionless archive currently stages it as .zip in the output directory. That destroys an unrelated ZIP with the same stem before moving the new archive to its final name.

Stage non-.zip output in a unique temporary directory, then move the completed archive to the requested filename. Keep direct .zip writes, automatic parent creation, and repeated-close behavior.

Regression tests preserve and read a real sibling ZIP, verify the requested archive, and cover .ufoz, uppercase .ZIP where it is a distinct name, extensionless output, and missing output parents.

Validation: the new tests produced 3 failures against unchanged production code; the patched module passes all 11 tests. The full suite passes 5,818 tests with 44 optional skips and 2 expected failures. Black checks pass. Native Windows/macOS execution remains for CI.

Validation results above are from the reviewed patch on upstream commit 8c0594d5187ab741d8782469d45114f289466144.

Comment thread README.rst Resolved
Comment thread Lib/fontTools/misc/filesystem/_zipfs.py Outdated
Comment on lines +225 to +235
if self._file == base_name + ".zip":
shutil.make_archive(base_name, format="zip", root_dir=self._temp_dir)
else:
with tempfile.TemporaryDirectory() as archive_dir:
archive = shutil.make_archive(
os.path.join(archive_dir, "archive"),
format="zip",
root_dir=self._temp_dir,
)
os.makedirs(os.path.dirname(os.path.abspath(self._file)), exist_ok=True)
shutil.move(archive, self._file)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we eliminate the branch where we special-case .zip and do the atomic replacement for all extensions. Also we can stage the archive in a temporary directory next to the destination file, so shutil.move() doesn't fall back to cross-filesystem copying:

def close(self):
    if self.isclosed():
        return

    output_dir = os.path.dirname(os.path.abspath(self._file))
    os.makedirs(output_dir, exist_ok=True)

    with tempfile.TemporaryDirectory(dir=output_dir) as staging:
        archive = shutil.make_archive(
            os.path.join(staging, "archive"),
            format="zip",
            root_dir=self._temp_dir,
        )
        os.replace(archive, self._file)

    super().close()

@anthrotype
anthrotype merged commit 9c557da into fonttools:main Oct 22, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants