Update SW_run.py
Browse files
SW_run.py
CHANGED
|
@@ -1,73 +1,70 @@
|
|
| 1 |
-
import os
|
| 2 |
import subprocess
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return
|
| 17 |
-
if Path(ngrokTokenFile).exists():
|
| 18 |
-
with open(ngrokTokenFile, encoding="utf-8") as nkfile:
|
| 19 |
-
ngrokToken = nkfile.readline()
|
| 20 |
-
print('use nrgok')
|
| 21 |
-
from pyngrok import conf, ngrok
|
| 22 |
-
conf.get_default().auth_token = ngrokToken
|
| 23 |
-
conf.get_default().monitor_thread = False
|
| 24 |
-
ssh_tunnels = ngrok.get_tunnels(conf.get_default())
|
| 25 |
-
if len(ssh_tunnels) == 0:
|
| 26 |
-
ssh_tunnel = ngrok.connect(port, bind_tls=True)
|
| 27 |
-
print(f'{address_name}:' + ssh_tunnel.public_url)
|
| 28 |
-
else:
|
| 29 |
-
print(f'{address_name}:' + ssh_tunnels[0].public_url)
|
| 30 |
-
else:
|
| 31 |
-
print('skip start ngrok')
|
| 32 |
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
repo_dir = launch_utils.repo_dir
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
list_extensions = launch_utils.list_extensions
|
| 52 |
-
run_extension_installer = launch_utils.run_extension_installer
|
| 53 |
-
prepare_environment = launch_utils.prepare_environment
|
| 54 |
-
configure_for_tests = launch_utils.configure_for_tests
|
| 55 |
-
start = launch_utils.start
|
| 56 |
|
| 57 |
-
|
| 58 |
-
subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import binascii # 用于将文本转换为十六进制
|
| 6 |
+
import re # 用于正则表达式
|
| 7 |
|
| 8 |
+
# 检查是否存在 logs_run.txt 文件,如果存在则删除它
|
| 9 |
+
log_file = '/bushu/logs_run.txt'
|
| 10 |
+
if os.path.exists(log_file):
|
| 11 |
+
os.remove(log_file)
|
| 12 |
|
| 13 |
+
# 检查是否存在 url.txt 文件,如果存在则删除它
|
| 14 |
+
url_file = '/bushu/url.txt'
|
| 15 |
+
if os.path.exists(url_file):
|
| 16 |
+
os.remove(url_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# 配置日志
|
| 19 |
+
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 20 |
|
| 21 |
+
# 创建一个将输出同时发送到控制台和日志文件的处理程序
|
| 22 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
| 23 |
+
console_handler.setLevel(logging.INFO)
|
| 24 |
+
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
| 25 |
+
console_handler.setFormatter(formatter)
|
| 26 |
+
logging.getLogger().addHandler(console_handler)
|
| 27 |
|
| 28 |
+
# 重定向标准输出和标准错误到日志文件和控制台
|
| 29 |
+
sys.stdout = logging.getLogger().handlers[0].stream
|
| 30 |
+
sys.stderr = logging.getLogger().handlers[0].stream
|
| 31 |
|
| 32 |
+
# 更改当前工作目录为 /bushu/ui
|
| 33 |
+
os.chdir('/bushu/ui')
|
|
|
|
| 34 |
|
| 35 |
+
try:
|
| 36 |
+
# 在 try 块中运行 /bushu/ui/launch.py,并将输出重定向到日志文件
|
| 37 |
+
with open(log_file, 'a') as log_file_handle, open(url_file, 'a') as url_file_handle:
|
| 38 |
+
process = subprocess.Popen(['python', '/bushu/ui/launch.py', '--port 8081'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
url_pattern = re.compile(r'https?://\S+')
|
|
|
|
| 41 |
|
| 42 |
+
for line in process.stdout:
|
| 43 |
+
if "Public" in line or "Application" in line:
|
| 44 |
+
# 将包含 "Public" 或 "Application" 的行转换为十六进制并写入 url.txt
|
| 45 |
+
hex_line = binascii.hexlify(line.encode()).decode()
|
| 46 |
+
url_file_handle.write(hex_line + '\n')
|
| 47 |
+
else:
|
| 48 |
+
# 查找行中的 URL 地址并将匹配的行转换为十六进制后写入 url.txt
|
| 49 |
+
urls = url_pattern.findall(line)
|
| 50 |
+
if urls:
|
| 51 |
+
for url in urls:
|
| 52 |
+
hex_url = binascii.hexlify(url.encode()).decode()
|
| 53 |
+
url_file_handle.write(hex_url + '\n')
|
| 54 |
|
| 55 |
+
# 将其他行写入日志文件
|
| 56 |
+
log_file_handle.write(line)
|
| 57 |
+
log_file_handle.flush()
|
| 58 |
|
| 59 |
+
process.wait()
|
| 60 |
|
| 61 |
+
except subprocess.CalledProcessError as e:
|
| 62 |
+
# 捕获异常并记录到日志文件
|
| 63 |
+
logging.exception(f"程序发生异常: {e}")
|
| 64 |
+
except Exception as e:
|
| 65 |
+
# 捕获其他异常并记录到日志文件
|
| 66 |
+
logging.exception(f"程序发生异常: {e}")
|
| 67 |
+
finally:
|
| 68 |
+
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
|
| 69 |
+
logging.getLogger().removeHandler(console_handler)
|
| 70 |
+
console_handler.close()
|