| Conditions | 6 |
| Total Lines | 25 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | # SPDX-FileCopyrightText: 2020 Peter Bittner <[email protected]> |
||
| 17 | def remove_empty_directories(directory): |
||
| 18 | """ |
||
| 19 | Recursively remove empty directories in the given directory tree. |
||
| 20 | |||
| 21 | This walks the directory tree in post-order (bottom-up), attempting to |
||
| 22 | remove directories that are empty. |
||
| 23 | """ |
||
| 24 | try: |
||
| 25 | subdirs = [ |
||
| 26 | Path(entry.path) for entry in os.scandir(directory) if entry.is_dir() |
||
| 27 | ] |
||
| 28 | except (OSError, PermissionError) as err: |
||
| 29 | log.warning('Cannot access directory %s: %s', directory, err) |
||
| 30 | return |
||
| 31 | |||
| 32 | for subdir in subdirs: |
||
| 33 | if should_ignore(subdir, Runner.ignore): |
||
| 34 | log.debug('Skipping %s', subdir) |
||
| 35 | else: |
||
| 36 | remove_empty_directories(subdir) |
||
| 37 | try: |
||
| 38 | if next(subdir.iterdir(), None) is None: |
||
| 39 | Runner.rmdir(subdir) |
||
| 40 | except (OSError, PermissionError) as err: |
||
| 41 | log.debug('Cannot check or remove directory %s: %s', subdir, err) |
||
| 42 |