mongo/bazel/wrapper_hook/remove_auto_header_dirs.py
Daniel Moody e6a4d5e471 SERVER-114938 clean up auto header dirs (#44863)
GitOrigin-RevId: bcd5d6ce137d8429fd04728d13563a5c7996a51a
2025-12-12 16:04:23 +00:00

38 lines
957 B
Python

import os
import sys
import shutil
from concurrent.futures import ThreadPoolExecutor
TARGET = ".auto_header"
def delete_dir(path: str):
# Don't follow/delete symlinked dirs
if os.path.islink(path):
return False
try:
shutil.rmtree(path, ignore_errors=True)
return True
except Exception:
return False
def find_targets(root: str):
for dirpath, dirnames, _ in os.walk(root):
# If TARGET exists directly under this directory
if TARGET in dirnames:
target_path = os.path.join(dirpath, TARGET)
yield target_path
# Prevent os.walk from descending into it
dirnames.remove(TARGET)
def clean_up_auto_header_dirs(ROOT):
targets = list(find_targets(ROOT))
if not targets:
return
workers = min(32, (os.cpu_count() or 8) * 2)
with ThreadPoolExecutor(max_workers=workers) as ex:
_ = list(ex.map(delete_dir, targets))