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."
This commit is contained in:
Mehran Kholdi 2021-01-16 03:34:43 +03:30
parent c2110108cb
commit 01a35354b6

View File

@ -6,13 +6,15 @@ from util import run
def be_absent(path): def be_absent(path):
path = Path(path) path = Path(path)
if not path.exists(): if path.is_symlink():
return path.unlink()
elif path.is_symlink() or path.is_file(): elif path.is_file():
path.unlink() path.unlink()
elif path.is_dir(): elif path.is_dir():
path.rmdir() path.rmdir()
# XXX: should we `shutil.rmtree(path)` instead? # XXX: should we `shutil.rmtree(path)` instead?
elif not path.exists():
return
else: else:
raise Exception("Unknown file type") raise Exception("Unknown file type")
@ -20,11 +22,10 @@ def be_absent(path):
def be_symlink(path, to): def be_symlink(path, to):
path = Path(path) path = Path(path)
to = Path(to) to = Path(to)
if path.exists(): if path.is_symlink():
if path.is_symlink(): if os.readlink(path) == str(to):
if os.readlink(path) == str(to): return
return be_absent(path)
be_absent(path)
path.symlink_to(to) path.symlink_to(to)