From 01a35354b65a49d889b980d01f6d1fa268d3a33b Mon Sep 17 00:00:00 2001 From: Mehran Kholdi Date: Sat, 16 Jan 2021 03:34:43 +0330 Subject: [PATCH] Fix a bug where broken symlinks where not being cleaned up See: https://docs.python.org/3/library/pathlib.html#pathlib.Path.exists "Note If the path points to a symlink, exists() returns whether the symlink points to an existing file or directory." --- declarative.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/declarative.py b/declarative.py index 042449c..85d91ab 100644 --- a/declarative.py +++ b/declarative.py @@ -6,13 +6,15 @@ from util import run def be_absent(path): path = Path(path) - if not path.exists(): - return - elif path.is_symlink() or path.is_file(): + if path.is_symlink(): + path.unlink() + elif path.is_file(): path.unlink() elif path.is_dir(): path.rmdir() # XXX: should we `shutil.rmtree(path)` instead? + elif not path.exists(): + return else: raise Exception("Unknown file type") @@ -20,11 +22,10 @@ def be_absent(path): def be_symlink(path, to): path = Path(path) to = Path(to) - if path.exists(): - if path.is_symlink(): - if os.readlink(path) == str(to): - return - be_absent(path) + if path.is_symlink(): + if os.readlink(path) == str(to): + return + be_absent(path) path.symlink_to(to)