monkeypatch_tempdir_cleanup.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import platform
  9. if platform.system() == 'Windows':
  10. from tempfile import TemporaryDirectory
  11. from pathlib import Path
  12. import os
  13. import stat
  14. realTempdirCleanup = TemporaryDirectory.cleanup
  15. def cleanup(self):
  16. """
  17. Make files writable before removing them
  18. In Windows and with Python < 3.8, TemporaryDirectory() will fail to clean up files that are read-only. Git marks
  19. files in the object store as read-only, so running git clone in a tempdir will fail. This wrapper marks files as
  20. writable before the cleanup runs.
  21. """
  22. for (dirpath, dirnames, filenames) in os.walk(self.name):
  23. for filename in filenames:
  24. (Path(dirpath) / filename).chmod(stat.S_IWRITE)
  25. realTempdirCleanup(self)
  26. TemporaryDirectory.cleanup = cleanup