Skip to content

Commit 40b4cc4

Browse files
core: libs: commonwealth: general: Add ignore field in delete_everything
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent e88a8a4 commit 40b4cc4

File tree

1 file changed

+24
-2
lines changed
  • core/libs/commonwealth/src/commonwealth/utils

1 file changed

+24
-2
lines changed

core/libs/commonwealth/src/commonwealth/utils/general.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,40 @@ def get_host_os() -> HostOs:
5656
return HostOs.Other
5757

5858

59-
def delete_everything(path: Path) -> None:
59+
# pylint:disable=dangerous-default-value
60+
def delete_everything(path: Path, ignore: list[Path] = []) -> None:
61+
def is_ignored(target: Path) -> bool:
62+
for ignored_path in ignore:
63+
try:
64+
ignored_resolved = ignored_path.resolve()
65+
target_resolved = target.resolve()
66+
try:
67+
# If relative_to works, target is inside or equal to ignored_resolved
68+
target_resolved.relative_to(ignored_resolved)
69+
return True
70+
except ValueError:
71+
# Not relative, so not in ignored path
72+
continue
73+
except Exception:
74+
continue
75+
return False
76+
77+
if is_ignored(path):
78+
return
79+
6080
if path.is_file() and not file_is_open(path):
6181
path.unlink()
6282
return
6383

6484
for item in path.glob("*"):
85+
if is_ignored(item):
86+
continue
6587
try:
6688
if item.is_file() and not file_is_open(item):
6789
item.unlink()
6890
if item.is_dir() and not item.is_symlink():
6991
# Delete folder contents
70-
delete_everything(item)
92+
delete_everything(item, ignore=ignore)
7193
except Exception as exception:
7294
logger.warning(f"Failed to delete: {item}, {exception}")
7395

0 commit comments

Comments
 (0)