From 2c4c3e345faf0a049ff1ca29658b271534db55cf Mon Sep 17 00:00:00 2001 From: Lehman Garrison Date: Mon, 24 Feb 2025 15:43:30 -0500 Subject: [PATCH] fix: avoid races in filesystem ops Use `exist_ok` and `missing_ok` in pathlib operations where we can. --- bin/update_how_it_works_image.py | 3 +-- cibuildwheel/__main__.py | 3 +-- cibuildwheel/util/file.py | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/bin/update_how_it_works_image.py b/bin/update_how_it_works_image.py index 0ed36fafd..7f55326f6 100755 --- a/bin/update_how_it_works_image.py +++ b/bin/update_how_it_works_image.py @@ -42,8 +42,7 @@ def main() -> None: ) dest_path = Path("docs/data/how-it-works.png") - if dest_path.exists(): - dest_path.unlink() + dest_path.unlink(missing_ok=True) Path(screenshot).rename(dest_path) diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index d14f9f89f..2b43257a6 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -399,8 +399,7 @@ def build_in_directory(args: CommandLineArguments) -> None: output_dir = options.globals.output_dir - if not output_dir.exists(): - output_dir.mkdir(parents=True) + output_dir.mkdir(parents=True, exist_ok=True) tmp_path = Path(mkdtemp(prefix="cibw-run-")).resolve(strict=True) try: diff --git a/cibuildwheel/util/file.py b/cibuildwheel/util/file.py index c961651e9..4a59a7e0b 100644 --- a/cibuildwheel/util/file.py +++ b/cibuildwheel/util/file.py @@ -25,8 +25,7 @@ def download(url: str, dest: Path) -> None: print(f"+ Download {url} to {dest}") dest_dir = dest.parent - if not dest_dir.exists(): - dest_dir.mkdir(parents=True) + dest_dir.mkdir(parents=True, exist_ok=True) # we've had issues when relying on the host OS' CA certificates on Windows, # so we use certifi (this sounds odd but requests also does this by default)