Files
pricing_review/launch.py
2026-10-11 10:52:01 +08:00

85 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
启动器(文件名必须是英文,Windows 的 cmd 处理中文路径不可靠)。
用法:
python launch.py -> 弹出「造价审核」软件窗口
python launch.py admin -> 只起服务 + 用默认浏览器打开后台管理
"""
import os
import sys
import time
import socket
import threading
import runpy
import traceback
import webbrowser
ROOT = os.path.dirname(os.path.abspath(__file__))
APP = os.path.join(ROOT, "CostReview", "app")
def wait_port(port, timeout=15):
for _ in range(int(timeout / 0.25)):
s = socket.socket()
s.settimeout(0.5)
ok = s.connect_ex(("127.0.0.1", port)) == 0
s.close()
if ok:
return True
time.sleep(0.25)
return False
def main():
if not os.path.isdir(APP):
print("找不到程序目录:%s" % APP)
print("请确认本文件与「CostReview」文件夹放在同一个目录下。")
return 1
os.chdir(APP)
for p in (APP, os.path.join(APP, "server"), os.path.join(APP, "core")):
if p not in sys.path:
sys.path.insert(0, p)
admin = len(sys.argv) > 1 and sys.argv[1].strip().lower() == "admin"
# 后台管理:起服务 + 浏览器打开,不弹软件窗口
if admin:
import api_server
port = api_server.PORT
if wait_port(port, 0.6):
print("服务已在运行(端口 %d),直接打开后台管理" % port)
else:
threading.Thread(target=api_server.run, daemon=True).start()
if not wait_port(port):
print("服务启动失败:端口 %d 起不来" % port)
return 1
print("服务就绪:http://127.0.0.1:%d/admin.html" % port)
webbrowser.open("http://127.0.0.1:%d/admin.html" % port)
print("已打开后台管理。关闭这个黑窗口 = 停止服务。")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
return 0
# 主入口:软件窗口
target = os.path.join(APP, "shell.py")
sys.argv = [target]
try:
runpy.run_path(target, run_name="__main__")
return 0
except SystemExit as e:
return int(e.code or 0)
except Exception:
traceback.print_exc()
print()
print("启动失败,请把上面的错误信息截图发出来。")
return 1
if __name__ == "__main__":
sys.exit(main())