Files
Raspi_Auto_Fan/build.sh
2026-05-11 13:13:29 +08:00

63 lines
1.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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
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
# 清理旧的构建文件
echo "清理旧的构建产物..."
rm -rf build dist fan.spec || true
# 使用 PyInstaller 打包
echo "开始打包 fan.py ..."
pyinstaller --onefile fan.py
echo "打包完成!可执行文件位于 dist/ (查看 dist/ 目录获取文件名)"