|
43 | 43 | USERUID = os.getuid() |
44 | 44 | USERNAME = getpass.getuser() |
45 | 45 |
|
| 46 | + |
| 47 | +def rmforest(top_dir): |
| 48 | + """Rewrite of rmtree that tries to handle permission errors (e.g.: when a directory that the user own is u-w) |
| 49 | + :param top_dir: diretory to remove |
| 50 | + :type top_dir: str or Path |
| 51 | + """ |
| 52 | + top = Path(top_dir) |
| 53 | + for root, dirs, files in top.walk(top_down=False): |
| 54 | + for name in files: |
| 55 | + f = root / name |
| 56 | + try: |
| 57 | + f.unlink() |
| 58 | + except PermissionError: |
| 59 | + perm = f.stat().st_mode | 0o00600 |
| 60 | + f.chmod(perm) |
| 61 | + pperm = f.parent.stat().st_mode | 0o00700 |
| 62 | + f.parent.chmod(pperm) |
| 63 | + f.unlink() |
| 64 | + for name in dirs: |
| 65 | + d = root / name |
| 66 | + d.rmdir() |
| 67 | + top.rmdir() |
| 68 | + |
| 69 | + |
46 | 70 | def make_selection(array): |
47 | 71 | return [(elem, elem.replace('_', ' ').capitalize()) if isinstance(elem, str) else elem for elem in array] |
48 | 72 |
|
@@ -645,15 +669,15 @@ def filter_ids(dest_list, label): |
645 | 669 | for bdir_file in build_dir.iterdir(): |
646 | 670 | if bdir_file.is_dir() and bdir_file.name not in ('logs', 'tests'): |
647 | 671 | try: |
648 | | - shutil.rmtree(bdir_file) |
| 672 | + rmforest(bdir_file) |
649 | 673 | except Exception: |
650 | 674 | _logger.exception('Failed to remove %s', bdir_file) |
651 | 675 | elif bdir_file.name == 'logs': |
652 | 676 | for log_file_path in bdir_file.iterdir(): |
653 | 677 | if log_file_path.is_dir(): |
654 | 678 |
|
655 | 679 | try: |
656 | | - shutil.rmtree(log_file_path) |
| 680 | + rmforest(log_file_path) |
657 | 681 | except Exception: |
658 | 682 | _logger.exception('Failed to remove %s', log_file_path) |
659 | 683 | elif log_file_path.name in ('run.txt', 'wake_up.txt') or not log_file_path.name.endswith('.txt'): |
|
0 commit comments