Update SW_run.py
Browse files
SW_run.py
CHANGED
|
@@ -1,56 +1,47 @@
|
|
| 1 |
-
from modules import launch_utils
|
| 2 |
-
import os
|
| 3 |
import subprocess
|
| 4 |
-
import threading
|
| 5 |
-
import logging
|
| 6 |
import sys
|
|
|
|
|
|
|
| 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 |
-
if args.test_server:
|
| 51 |
-
configure_for_tests()
|
| 52 |
-
|
| 53 |
-
start()
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
main()
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
|
|
|
|
|
|
| 2 |
import sys
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# 检查是否存在 logs_run.txt 文件,如果存在则删除它
|
| 7 |
+
log_file = '/bushu/logs_run.txt'
|
| 8 |
+
if os.path.exists(log_file):
|
| 9 |
+
os.remove(log_file)
|
| 10 |
+
|
| 11 |
+
# 配置日志
|
| 12 |
+
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
+
|
| 14 |
+
# 创建一个将输出同时发送到控制台和日志文件的处理程序
|
| 15 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
| 16 |
+
console_handler.setLevel(logging.INFO)
|
| 17 |
+
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
| 18 |
+
console_handler.setFormatter(formatter)
|
| 19 |
+
logging.getLogger().addHandler(console_handler)
|
| 20 |
+
|
| 21 |
+
# 重定向标准输出和标准错误到日志文件和控制台
|
| 22 |
+
sys.stdout = logging.getLogger().handlers[0].stream
|
| 23 |
+
sys.stderr = logging.getLogger().handlers[0].stream
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# 在try块中运行 /bushu/ui/launch.py,并将输出重定向到日志文件
|
| 27 |
+
with open(log_file, 'a') as log_file_handle:
|
| 28 |
+
process = subprocess.Popen(['python', '/bushu/ui/launch.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
| 29 |
+
|
| 30 |
+
# 保存包含"Running"的行到url.txt
|
| 31 |
+
with open('/bushu/url.txt', 'a') as url_file:
|
| 32 |
+
for line in process.stdout:
|
| 33 |
+
if "Running" in line:
|
| 34 |
+
url_file.write(line)
|
| 35 |
+
print(line, end='') # 输出包含"Running"的行到控制台
|
| 36 |
+
log_file_handle.write(line)
|
| 37 |
+
process.wait() # 等待子进程完成
|
| 38 |
+
except subprocess.CalledProcessError as e:
|
| 39 |
+
# 捕获异常并记录到日志文件
|
| 40 |
+
logging.exception(f"程序发生异常: {e}")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
# 捕获其他异常并记录到日志文件
|
| 43 |
+
logging.exception(f"程序发生异常: {e}")
|
| 44 |
+
finally:
|
| 45 |
+
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
|
| 46 |
+
logging.getLogger().removeHandler(console_handler)
|
| 47 |
+
console_handler.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|