diff --git a/build.txt b/build.txt index 20ae812..b12806c 100644 --- a/build.txt +++ b/build.txt @@ -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 \ No newline at end of file diff --git a/datas/start.bat b/datas/start.bat new file mode 100644 index 0000000..45b24c8 --- /dev/null +++ b/datas/start.bat @@ -0,0 +1 @@ +D:/database/start.exe --startWindow \ No newline at end of file diff --git a/dist/install.exe b/dist/install.exe index a38de31..e2de8c6 100644 Binary files a/dist/install.exe and b/dist/install.exe differ diff --git a/dist/start.exe b/dist/start.exe index 2d608d4..07f93db 100644 Binary files a/dist/start.exe and b/dist/start.exe differ diff --git a/src/install.py b/src/install.py index 9065bae..d72b33a 100644 --- a/src/install.py +++ b/src/install.py @@ -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() diff --git a/src/start.py b/src/start.py index 31b40d9..05a31ca 100644 --- a/src/start.py +++ b/src/start.py @@ -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) - open_web() \ No newline at end of file + 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 \ No newline at end of file