From 12bf59b3cdb08ee25d12942c02f2375f8a91bf2d Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 17 Jan 2023 11:40:58 +0100 Subject: [PATCH] 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 --- tools/python/jwutils/misc.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/python/jwutils/misc.py b/tools/python/jwutils/misc.py index 3b243f6..e1f9781 100644 --- a/tools/python/jwutils/misc.py +++ b/tools/python/jwutils/misc.py @@ -19,6 +19,16 @@ def silentremove(filename): #export if e.errno != errno.ENOENT: 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: add = total_size - len(token) if add <= 0: