diff --git a/build.sh b/build.sh index 7149b24..52329f0 100755 --- a/build.sh +++ b/build.sh @@ -1,13 +1,62 @@ -#!/bin/bash +#!/usr/bin/env bash +set -euo pipefail +# 切换到脚本所在目录,保证相对路径正确 +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +echo "工作目录:$PWD" + +# 选择可用的 Python(优先 python3) +if command -v python3 >/dev/null 2>&1; then + PYTHON_CMD=python3 +elif command -v python >/dev/null 2>&1; then + PYTHON_CMD=python +else + echo "未找到 Python 可执行文件,请先安装 Python 3。" >&2 + exit 1 +fi + +# 如果没有 venv 或 venv 不完整则创建 +if [ -d "venv" ] && [ -x "venv/bin/python" ]; then + echo "虚拟环境已存在,激活中..." +else + echo "未检测到虚拟环境,使用 $PYTHON_CMD 创建 venv..." + $PYTHON_CMD -m venv venv +fi + +# 激活虚拟环境 +# shellcheck source=/dev/null source venv/bin/activate -# 安装打包工具 PyInstaller -# pip install pyinstaller + +echo "虚拟环境已激活:$(which python) ($(python --version 2>&1))" + +# 升级基础打包工具 +pip install --upgrade pip setuptools wheel + +# 如果存在 requirements.txt,则按文件安装;否则安装默认依赖 +if [ -f "requirements.txt" ]; then + echo "检测到 requirements.txt,安装依赖..." + pip install -r requirements.txt +else + DEFAULT_PKGS=(pyinstaller) + echo "未找到 requirements.txt,检查并安装默认依赖:${DEFAULT_PKGS[*]}" + for pkg in "${DEFAULT_PKGS[@]}"; do + if pip show "$pkg" >/dev/null 2>&1; then + echo "$pkg 已安装" + else + echo "正在安装 $pkg ..." + pip install "$pkg" + fi + done +fi # 清理旧的构建文件 -rm -rf build dist fan.spec +echo "清理旧的构建产物..." +rm -rf build dist fan.spec || true -# 将 fan.py 打包为单文件可执行程序 +# 使用 PyInstaller 打包 +echo "开始打包 fan.py ..." pyinstaller --onefile fan.py -echo "打包完成!可执行文件位于 dist/fan" +echo "打包完成!可执行文件位于 dist/ (查看 dist/ 目录获取文件名)" diff --git a/die_loop.py b/burning_test/die_loop.py similarity index 100% rename from die_loop.py rename to burning_test/die_loop.py diff --git a/mat_cal.py b/burning_test/mat_cal.py similarity index 100% rename from mat_cal.py rename to burning_test/mat_cal.py diff --git a/install.sh b/install_to_systemd.sh similarity index 100% rename from install.sh rename to install_to_systemd.sh diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f4412ee --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pigpio +pyinstaller \ No newline at end of file