monkeypatch_tempdir_cleanup.py 1.0 KB

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