完成软件更新功能

This commit is contained in:
2026-04-08 00:36:09 +08:00
parent ff8aa0269d
commit 948d23074a
4 changed files with 145 additions and 9 deletions
+19
View File
@@ -18,6 +18,7 @@ import time
APP_NAME = "CleanDesktopOrganizer"
REQUEST_FILE = "update_request.json"
LOG_FILE = "update.log"
def _programdata_dir() -> str:
@@ -29,6 +30,16 @@ def _request_path() -> str:
return os.path.join(_programdata_dir(), REQUEST_FILE)
def _log(msg: str):
try:
os.makedirs(_programdata_dir(), exist_ok=True)
p = os.path.join(_programdata_dir(), LOG_FILE)
with open(p, "a", encoding="utf-8") as f:
f.write(msg.rstrip() + "\n")
except Exception:
pass
def _read_request() -> dict:
p = _request_path()
with open(p, "r", encoding="utf-8") as f:
@@ -79,6 +90,7 @@ def main() -> int:
except FileNotFoundError:
return 0
except Exception:
_log("ERROR: cannot read request file")
return 2
src = str(req.get("src") or "")
@@ -87,8 +99,11 @@ def main() -> int:
restart = bool(req.get("restart", True))
if not src or not dst:
_log(f"ERROR: bad request src={src!r} dst={dst!r} pid={pid}")
return 3
_log(f"START: pid={pid} src={src} dst={dst}")
# 等待主程序退出,最多 60 秒
waited = 0.0
while _pid_exists(pid) and waited < 60.0:
@@ -96,6 +111,7 @@ def main() -> int:
waited += 0.5
ok = _copy_with_retries(src, dst, retries=15)
_log(f"COPY: ok={ok}")
try:
os.remove(src)
except Exception:
@@ -110,10 +126,13 @@ def main() -> int:
if ok and restart:
try:
subprocess.Popen([dst], creationflags=subprocess.DETACHED_PROCESS | subprocess.CREATE_NO_WINDOW)
_log("RESTART: started")
except Exception:
_log("RESTART: failed")
pass
return 0
_log("DONE: failed")
return 4