|
43 | 43 | USERUID = os.getuid() |
44 | 44 | USERNAME = getpass.getuser() |
45 | 45 |
|
| 46 | + |
| 47 | +def force_rmtree(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 |
|
@@ -646,15 +670,15 @@ def filter_ids(dest_list, label): |
646 | 670 | for bdir_file in build_dir.iterdir(): |
647 | 671 | if bdir_file.is_dir() and bdir_file.name not in ('logs', 'tests'): |
648 | 672 | try: |
649 | | - shutil.rmtree(bdir_file) |
| 673 | + force_rmtree(bdir_file) |
650 | 674 | except Exception: |
651 | 675 | _logger.exception('Failed to remove %s', bdir_file) |
652 | 676 | elif bdir_file.name == 'logs': |
653 | 677 | for log_file_path in bdir_file.iterdir(): |
654 | 678 | if log_file_path.is_dir(): |
655 | 679 |
|
656 | 680 | try: |
657 | | - shutil.rmtree(log_file_path) |
| 681 | + force_rmtree(log_file_path) |
658 | 682 | except Exception: |
659 | 683 | _logger.exception('Failed to remove %s', log_file_path) |
660 | 684 | elif log_file_path.name in ('run.txt', 'wake_up.txt') or not log_file_path.name.endswith('.txt'): |
|
0 commit comments