fs_utils.py 665 B

1234567891011121314151617181920212223
  1. """Filesystem helpers shared across Hermes CLI subsystems."""
  2. import os
  3. import shutil
  4. import stat
  5. from pathlib import Path
  6. def rmtree_force(path: Path) -> None:
  7. """``shutil.rmtree`` that clears read-only bits before deleting.
  8. Windows refuses to delete a tree containing read-only files — git clones
  9. store objects read-only — raising ``PermissionError`` where POSIX unlinks
  10. them fine. The ``onerror`` hook chmods the file writable and retries.
  11. """
  12. def _onerror(func, p, _exc_info):
  13. try:
  14. os.chmod(p, stat.S_IWRITE)
  15. except OSError:
  16. pass
  17. func(p)
  18. shutil.rmtree(path, onerror=_onerror)