From 775fe461b09ff442c637ec371769818d6e000e19 Mon Sep 17 00:00:00 2001 From: leimingsheng Date: Mon, 20 Apr 2026 09:31:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(master=20:=20tool)=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=93=E5=8C=85=E5=B7=A5=E7=A8=8B=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/auto_package/auto_package.py | 122 ++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tool/auto_package/auto_package.py diff --git a/tool/auto_package/auto_package.py b/tool/auto_package/auto_package.py new file mode 100644 index 0000000..0fabcd5 --- /dev/null +++ b/tool/auto_package/auto_package.py @@ -0,0 +1,122 @@ +import os +import zipfile +import sys + +# ===================== 【可配置项】 ===================== +# 输出目录(可自定义修改,例如 D:/release / /home/user/release) +OUTPUT_DIR = "" + +# 最小打包内容 +MIN_FILES = [ + "extension", + "nicsensor.sh", + "project.config", + "setup.sh" +] + +# 完整工程打包内容(所有文件) +FULL_FILES = [ + "docs", + "extension", + "tool", + "nicsensor.sh", + "project.config", + "readme.md", + "setup.sh" +] +# ====================================================== + + +def get_project_root(): + """ + 自动获取工程根目录(脚本在 tool/auto_package/ 下) + """ + # 当前脚本目录:tool/auto_package + current_dir = os.path.dirname(os.path.abspath(__file__)) + # 工程根目录 = 往上两级 + project_root = os.path.abspath(os.path.join(current_dir, "../..")) + return project_root + + +def pack_files(zip_path, file_list, project_root): + """ + 统一打包函数 + """ + os.chdir(project_root) # 切换到工程根目录,保证打包路径正确 + + with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: + for item in file_list: + if not os.path.exists(item): + print(f"⚠️ 跳过不存在:{item}") + continue + + # 递归打包文件夹 + if os.path.isdir(item): + for root, dirs, files_in_dir in os.walk(item): + for f in files_in_dir: + file_path = os.path.join(root, f) + arc_path = os.path.relpath(file_path, project_root) + zipf.write(file_path, arc_path) + print(f" ✔ {arc_path}") + else: + zipf.write(item, item) + print(f" ✔ {item}") + + +def build_all(version): + """ + 一次性构建 min + full 两个包 + """ + project_root = get_project_root() + OUTPUT_DIR = os.path.join(project_root, "Release") + output_abs = os.path.abspath(OUTPUT_DIR) + + # 创建输出目录 + if not os.path.exists(output_abs): + os.makedirs(output_abs) + + # 文件名 + zip_min = os.path.join(output_abs, f"nicsensor-{version}-min.zip") + zip_full = os.path.join(output_abs, f"nicsensor-{version}-full.zip") + + print("=" * 60) + print(" 开始自动打包") + print(f"版本号:{version}") + print(f"工程根目录:{project_root}") + print(f"输出目录:{output_abs}") + print("=" * 60) + + # 打包最小版 + print("\n📦 开始打包【最小版本】") + pack_files(zip_min, MIN_FILES, project_root) + + # 打包完整版 + print("\n📦 开始打包【完整工程】") + pack_files(zip_full, FULL_FILES, project_root) + + print("\n✅ 全部打包完成!") + print(f"📍 最小包:{zip_min}") + print(f"📍 完整包:{zip_full}") + print("=" * 60) + + +def show_usage(): + print("=" * 50) + print(" Nicsensor 一键打包脚本") + print("=" * 50) + print("使用方法:") + print(" python pack.py 版本号") + print() + print("示例:") + print(" python pack.py v1.0.0") + print(" python pack.py 2.5.0") + print("=" * 50) + + +if __name__ == "__main__": + if len(sys.argv) != 2: + show_usage() + sys.exit(1) + + version = sys.argv[1] + build_all(version) \ No newline at end of file