os.misc: Add update_symlink()

update_symlink(target, link_name) does the equivalent of ln -sf, i.e.
force replacement of link_name with a new sysmlink, if it existed
before the call.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2023-01-17 11:40:58 +01:00
commit 12bf59b3cd

View file

@ -19,6 +19,16 @@ def silentremove(filename): #export
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise # re-raise exception if a different error occurred raise # re-raise exception if a different error occurred
def update_symlink(target, link_name):
try:
os.symlink(target, link_name)
except OSError as e:
if e.errno == errno.EEXIST:
os.remove(link_name)
os.symlink(target, link_name)
else:
raise e
def pad(token: str, total_size: int, right_align: bool = False) -> str: def pad(token: str, total_size: int, right_align: bool = False) -> str:
add = total_size - len(token) add = total_size - len(token)
if add <= 0: if add <= 0: