From fa44764ed8221535aef851042d24ac2c4763a059 Mon Sep 17 00:00:00 2001 From: JacobNolan1 <38154750+JacobNolan1@users.noreply.github.com> Date: Mon, 29 Apr 2024 23:21:39 +1000 Subject: [PATCH 1/2] During service install perform file move of pythonservice.exe if file exists at src During install perform move of pythonservice.exe instead of file copy to handle .exe always being present in src location. Additional try-except added to handle continuation of program if fileMove fails. --- win32/Lib/win32serviceutil.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/win32/Lib/win32serviceutil.py b/win32/Lib/win32serviceutil.py index 5601e13eb..17df48fe7 100644 --- a/win32/Lib/win32serviceutil.py +++ b/win32/Lib/win32serviceutil.py @@ -50,8 +50,14 @@ 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.MoveFile(maybe, correct) + 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}'") From d6031ed4055f5426842c9b8f0d61b566ec9425b0 Mon Sep 17 00:00:00 2001 From: Jacob Nolan Date: Tue, 6 Aug 2024 12:24:21 +1000 Subject: [PATCH 2/2] Replaced pythonservice dll MoveFile with MoveFileEx and enforced replace existing dll file if exists --- win32/Lib/win32serviceutil.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/win32/Lib/win32serviceutil.py b/win32/Lib/win32serviceutil.py index 17df48fe7..2871b9056 100644 --- a/win32/Lib/win32serviceutil.py +++ b/win32/Lib/win32serviceutil.py @@ -54,11 +54,10 @@ def LocatePythonServiceExe(exe=None): # Handle case where MoveFile() fails. Particularly if destination file # has a resource lock and can't be replaced by src file try: - win32api.MoveFile(maybe, correct) + 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}'")