优化安装程序

This commit is contained in:
chenxudong 2025-06-09 15:38:56 +08:00
parent dc0234af14
commit 9b0d21f4ea
6 changed files with 35 additions and 10 deletions

View File

@ -1,2 +1,2 @@
pyinstaller --onefile --add-data "datas/mariadb.zip:datas" --add-data "datas/init.sql:datas" --add-data "datas/jdk.zip:datas" src/install.py
pyinstaller --onefile --add-data "datas/mariadb.zip:datas" --add-data "datas/init.sql:datas" --add-data "datas/jdk.zip:datas" --add-data "datas/start.bat:datas" src/install.py
pyinstaller --onefile --add-data "datas/electromagnetic.jar:datas" src/start.py

1
datas/start.bat Normal file
View File

@ -0,0 +1 @@
D:/database/start.exe --startWindow

BIN
dist/install.exe vendored

Binary file not shown.

BIN
dist/start.exe vendored

Binary file not shown.

View File

@ -1,5 +1,6 @@
import os
import sys
import time
import zipfile
import subprocess
import loguru
@ -7,7 +8,7 @@ import elevate
import shutil
import psutil
path = r'D:\database'
root_path = r'D:\database'
service_name = 'ComacMariaDB'
passowrd = '1qaz@WSX'
mariadb_data_path = r'D:\database\mariadb\data'
@ -25,10 +26,10 @@ def ensure_dir(directory):
def unzip_file():
new_path = get_resource_path(os.path.join('datas', 'mariadb.zip'))
with zipfile.ZipFile(str(new_path), 'r') as zip_ref:
zip_ref.extractall(path) # 将文件解压到指定路径
zip_ref.extractall(root_path) # 将文件解压到指定路径
def register_service():
args1 = path + '/mariadb/bin/mysql_install_db.exe'
args1 = root_path + '/mariadb/bin/mysql_install_db.exe'
args2 = '--datadir=' + mariadb_data_path
args3 = '--service=' + service_name
args4 = '--password=' + passowrd
@ -44,7 +45,7 @@ def start_mariadb():
loguru.logger.info(res)
def init_db():
command2 = fr'{path}\mariadb\bin\mysql --no-defaults -u root -p{passowrd} -P {mariadb_port} {init_schema} < {init_sql}'
command2 = fr'{root_path}\mariadb\bin\mysql --no-defaults -u root -p{passowrd} -P {mariadb_port} {init_schema} < {init_sql}'
with os.popen(command2) as stream:
res2 = stream.read()
loguru.logger.info(res2)
@ -54,7 +55,7 @@ def init_db():
def init_db_user():
new_path = get_resource_path(os.path.join('datas', 'init.sql'))
shutil.copy(str(new_path), mariadb_data_path)
command1 = fr'{path}\mariadb\bin\mysql -u root -p{passowrd} -P {mariadb_port} -e "create database if not exists {init_schema}; use {init_schema};"'
command1 = fr'{root_path}\mariadb\bin\mysql -u root -p{passowrd} -P {mariadb_port} -e "create database if not exists {init_schema}; use {init_schema};"'
with os.popen(command1) as stream:
res = stream.read()
loguru.logger.info(res)
@ -91,15 +92,19 @@ def get_resource_path(relative_path):
def set_java_env():
new_path = get_resource_path(os.path.join('datas', 'jdk.zip'))
with zipfile.ZipFile(str(new_path), 'r') as zip_ref:
zip_ref.extractall(path) # 将文件解压到指定路径
zip_ref.extractall(root_path) # 将文件解压到指定路径
pass
def set_bat():
new_path = get_resource_path(os.path.join('datas', 'start.bat'))
shutil.copy(str(new_path), root_path)
def start_install():
status = check_service()
if status:
return
loguru.logger.info("开始安装MariaDB")
ensure_dir(path)
ensure_dir(root_path)
loguru.logger.info("开始解压文件")
unzip_file()
loguru.logger.info("开始注册服务")
@ -112,6 +117,10 @@ def start_install():
init_db()
loguru.logger.info("设置Java运行环境")
set_java_env()
loguru.logger.info("生成脚本文件")
set_bat()
loguru.logger.info("安装完成10秒后自动退出")
time.sleep(10)
if __name__ == '__main__':
elevate.elevate()

View File

@ -8,12 +8,15 @@ from datetime import datetime
import re
import webview
import argparse
# 获取当前脚本所在的目录
new_java_path = 'D:/database/jdk/bin/java.exe'
app_log_dir = 'D:/database/logs'
port = 12396
# 构建JDK和JAR的路径
jar_path = os.path.join("datas", "electromagnetic.jar")
url = f'http://127.0.0.1:{port}/index'
def ensure_dir(directory):
if not os.path.exists(directory):
@ -111,12 +114,24 @@ def kill_process_by_port(run_port):
def open_web():
webview.settings['ALLOW_DOWNLOADS'] = True
webview.settings['OPEN_DEVTOOLS_IN_DEBUG'] = False
webview.create_window('数据库管理系统', f'http://127.0.0.1:{port}/index')
webview.create_window('数据库管理系统', url=url)
webview.start(debug=True)
if __name__ == '__main__':
kill_process_by_port(port)
delete_old_files(app_log_dir, days=2)
start()
time.sleep(10)
parser = argparse.ArgumentParser()
parser.add_argument('--startWindow',
type=lambda x: x.lower() == 'true',
default=None,
help='设置为true则执行open_window()false或未设置则不执行')
args = parser.parse_args()
if args.startWindow:
open_web()
else:
logger.info(f"请打开浏览器,输入 {url} 访问使用")
while True:
pass