整理
This commit is contained in:
61
build.sh
61
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/ 目录获取文件名)"
|
||||
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
pigpio
|
||||
pyinstaller
|
||||
Reference in New Issue
Block a user