From a83b36c3a2f7a5c66c32dcb1ed03ff80c8509208 Mon Sep 17 00:00:00 2001 From: JacobNolan1 <38154750+JacobNolan1@users.noreply.github.com> Date: Wed, 7 Aug 2024 02:04:04 +1000 Subject: [PATCH] Perform file move of pythonservice.exe during install (#2251) During service install perform file move of pythonservice.exe if file exists at src Co-authored-by: Jacob Nolan --- win32/Lib/win32serviceutil.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/win32/Lib/win32serviceutil.py b/win32/Lib/win32serviceutil.py index 5601e13eb..2871b9056 100644 --- a/win32/Lib/win32serviceutil.py +++ b/win32/Lib/win32serviceutil.py @@ -50,8 +50,13 @@ def LocatePythonServiceExe(exe=None): # pywin32 installed it next to win32service.pyd (but we can't run it from there) maybe = os.path.join(os.path.dirname(win32service.__file__), exe) if os.path.exists(maybe): - print(f"copying host exe '{maybe}' -> '{correct}'") - win32api.CopyFile(maybe, correct) + print(f"moving host exe '{maybe}' -> '{correct}'") + # Handle case where MoveFile() fails. Particularly if destination file + # has a resource lock and can't be replaced by src file + try: + win32api.MoveFileEx(maybe, correct, win32con.MOVEFILE_REPLACE_EXISTING) + except win32api.error as exc: + print(f"Failed to move host exe '{exc}'") if not os.path.exists(correct): raise error(f"Can't find '{correct}'")