Browse Source

Add Windows compatibility for prune_binaries.py (#1592)

* Add Windows compatibility for prunhe_binaries.py

`file_path.unlink()` is not able to remove read-only files on Windows.

* code formatting and add comments
Sheng 2 years ago
parent
commit
2db58a8698
1 changed files with 7 additions and 0 deletions
  1. 7 0
      utils/prune_binaries.py

+ 7 - 0
utils/prune_binaries.py

@@ -11,6 +11,8 @@ from pathlib import Path
 
 from _common import ENCODING, get_logger, add_common_params
 import sys
+import os
+import stat
 
 
 def prune_dir(unpack_root, prune_files):
@@ -25,6 +27,11 @@ def prune_dir(unpack_root, prune_files):
         file_path = unpack_root / relative_file
         try:
             file_path.unlink()
+        # read-only files can't be deleted on Windows
+        # so remove the flag and try again.
+        except PermissionError:
+            os.chmod(file_path, stat.S_IWRITE)
+            file_path.unlink()
         except FileNotFoundError:
             unremovable_files.add(Path(relative_file).as_posix())
     return unremovable_files