Repository navigation
Expand file tree
/
Copy pathselfcheck.py
More file actions
112 lines (91 loc) · 3.09 KB
/
Copy pathselfcheck.py
File metadata and controls
112 lines (91 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
"""自检脚本:无人值守跑一遍关键路径,捕获 Tk 回调里的运行时异常。
用法(仓库根目录):
python selfcheck.py
覆盖路径:
构建主窗口 → 2 秒倒计时(走到 _fire,关机调用被替换为空函数,绝不真关机)
→ 圆环 / 剩余时间刷新 → 定时对话框开·关(含开着时换主题)
→ 定时关机日志窗口
退出码:0 = 全部通过,1 = 有异常(会打印完整堆栈)。
"""
import sys
import time
import traceback
import tkinter as tk
import ShutdownTimer as st
def main():
errors = []
# 关键:绝不真的关机
st.ShutdownApp._do_shutdown = lambda self: None
st.ShutdownApp._do_shutdown_mac = lambda self: None
# 自检环境没有 mainloop(),把更新检查线程停掉,避免 after() 报"not in main loop"
st.UpdateChecker.check_async = lambda self, silent=False: None
root = getattr(st, "_root", None) or tk.Tk()
root.withdraw()
def on_err(exc, val, tb):
errors.append("".join(traceback.format_exception(exc, val, tb)))
root.report_callback_exception = on_err
app = st.ShutdownApp(root)
root.update()
# ---- 1. 主题刷新 ----
try:
app.refresh_theme()
root.update()
except Exception:
errors.append(traceback.format_exc())
# ---- 2. 倒计时 + 圆环 + 剩余时间 ----
try:
app.entry.config(state="normal")
app.entry.delete(0, "end")
app.entry.insert(0, "2")
app.unit = "second"
app.start()
deadline = time.time() + 4.0
while time.time() < deadline:
root.update()
time.sleep(0.02)
# 检查圆环是否真的画出来了(image item 应存在)
item = getattr(app, "_ring_item", None)
if item is None:
errors.append("ring image item 未创建(圆环没画出来)\n")
try:
txt = app.cv.itemcget(app.time_txt, "text")
if not txt:
errors.append("剩余时间文本为空\n")
except Exception:
errors.append("读取剩余时间文本失败\n" + traceback.format_exc())
except Exception:
errors.append(traceback.format_exc())
# ---- 3. 定时对话框(开着时换主题)----
try:
dlg = st.ScheduleCanvasDialog(app)
root.update()
app.refresh_theme()
root.update()
dlg.redraw()
root.update()
dlg.close()
root.update()
except Exception:
errors.append(traceback.format_exc())
# ---- 4. 日志窗口 ----
try:
st.sched_log("selfcheck: hello")
st.show_sched_log(root, app.P, app.family)
root.update()
except Exception:
errors.append(traceback.format_exc())
try:
root.destroy()
except Exception:
pass
if errors:
print("SELF-CHECK FAILED (%d)" % len(errors))
for e in errors:
print("-" * 60)
print(e)
return 1
print("SELF-CHECK PASSED")
return 0
if __name__ == "__main__":
sys.exit(main())