diff --git a/gitea-python-ci-cd.md b/gitea-python-ci-cd.md index f938597..36a5e48 100644 --- a/gitea-python-ci-cd.md +++ b/gitea-python-ci-cd.md @@ -281,19 +281,36 @@ done Strip unnecessary files from site-packages to reduce archive size: ```bash +# Generic cleanup find "$SITE_PACKAGES" -type d -name __pycache__ -exec rm -rf {} + find "$SITE_PACKAGES" -type d -name tests -exec rm -rf {} + find "$SITE_PACKAGES" -type d -name "*.dist-info" -exec rm -rf {} + find "$SITE_PACKAGES" -name "*.pyi" -delete # Remove build-time-only packages -rm -rf "$SITE_PACKAGES"/{pip,setuptools,pkg_resources}* +rm -rf "$SITE_PACKAGES"/{pip,setuptools,pkg_resources,_distutils_hack}* -# Remove heavy unused parts of specific libraries -rm -f "$SITE_PACKAGES"/cv2/opencv_videoio_ffmpeg*.dll # -28 MB -rm -rf "$SITE_PACKAGES"/numpy/{tests,f2py,typing} +# OpenCV: remove ffmpeg DLL (~28 MB), Haar cascades, dev files +rm -f "$SITE_PACKAGES"/cv2/opencv_videoio_ffmpeg*.dll +rm -rf "$SITE_PACKAGES"/cv2/{data,gapi,misc,utils,typing_stubs,typing} + +# NumPy: remove unused submodules (only keep core, fft, random) +for mod in polynomial linalg ma lib distutils f2py typing _pyinstaller; do + rm -rf "$SITE_PACKAGES/numpy/$mod" +done + +# zeroconf: remove service type database (~1-2 MB) +rm -rf "$SITE_PACKAGES/zeroconf/_services" + +# Strip debug symbols from native extensions +find "$SITE_PACKAGES" -name "*.pyd" -exec strip --strip-debug {} \; + +# Remove .py source files (keep compiled .pyc only) +find "$SITE_PACKAGES" -name "*.py" ! -name "__init__.py" -delete ``` +**Tip:** If a library is only needed for one feature (e.g., Pillow for system tray icons), move it to an optional dependency group and strip unused plugins. Replace its core usage with a library already in the dependency tree (e.g., use cv2 for JPEG encoding instead of Pillow). + ### 4.4. Bundling tkinter (Optional) Embedded Python doesn't include tkinter. Extract it from the official MSI packages: @@ -351,6 +368,47 @@ Include install/uninstall scripts for running as a service: # Then: systemctl daemon-reload && systemctl enable && systemctl start ``` +### 5.2. Shared Build Logic + +When building for both Windows and Linux, extract shared logic into a `build-common.sh` sourced by both platform scripts. This avoids duplicating version detection, site-packages cleanup, frontend builds, and app file copying. + +```bash +# build-common.sh — shared functions +detect_version() { ... } # git tag → env var → pyproject.toml fallback +clean_dist() { ... } # rm -rf + mkdir +build_frontend() { ... } # npm ci && npm run build +copy_app_files() { ... } # cp src/, config/, clean .map/__pycache__ + +# Parameterized by platform — extension suffix differs: +# cleanup_site_packages +# Windows: cleanup_site_packages "$SP" "pyd" "dll" +# Linux: cleanup_site_packages "$SP" "so" "so" +cleanup_site_packages() { + local sp_dir="$1" ext_suffix="${2:-so}" lib_suffix="${3:-so}" + # Generic: __pycache__, tests/, *.dist-info, *.pyi + # NumPy: remove unused submodules (polynomial, linalg, ma, etc.) + # OpenCV: remove ffmpeg, Haar cascades, dev files + # Pillow: remove unused image format plugins + # zeroconf: remove _services/ database + # strip --strip-debug on *.$ext_suffix + # Remove .py source (keep .pyc only) +} +``` + +```bash +# build-dist-windows.sh / build-dist.sh +source "$(dirname "$0")/build-common.sh" +detect_version "${1:-}" +clean_dist +# ... platform-specific Python setup + dependency install ... +cleanup_site_packages "$SITE_PACKAGES" "pyd" "dll" # or "so" "so" +build_frontend +copy_app_files +# ... platform-specific launcher + packaging ... +``` + +**Key principle:** Both scripts run on Linux (Windows build is cross-compiled). Keep platform-specific code (embedded Python, ._pth patching, .bat launchers, systemd services) in the platform scripts. Keep size optimization, cleanup, and build steps that are identical in the common file. + ## 6. NSIS Installer (Windows) Cross-compilable on Linux: `apt install nsis && makensis installer.nsi`