#!/bin/bash # entorno_python.sh — Prepara el entorno de Python de la cartera. # # bash entorno_python.sh ← crea /tmp/.cartera-venv si falta # bash entorno_python.sh --guardar-wheels ← guarda los paquetes en wheels/ # para poder instalar sin internet # # El entorno vive en /tmp (no en el USB): se borra al reiniciar el equipo y # abrir_app.sh lo recrea solo. Usa los paquetes de wheels/ si están; si no # (p. ej. en un USB nuevo), los descarga de internet. # CARTERA_VENV=/otra/ruta cambia dónde se crea (lo usa el simulacro). cd "$(dirname "$0")" || exit 1 VENV=${CARTERA_VENV:-/tmp/.cartera-venv} if ! command -v python3 >/dev/null; then echo "✗ Falta Python 3: sudo apt install python3 python3-venv" exit 1 fi if [ "$1" = "--guardar-wheels" ]; then mkdir -p wheels if python3 -m pip download --quiet --dest wheels -r requirements.txt; then echo "✓ Paquetes guardados en wheels/ para Python $(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')." exit 0 fi echo "✗ No se pudieron descargar los paquetes (¿hay internet?)." exit 1 fi listo() { [ -x "$VENV/bin/python" ] && "$VENV/bin/python" -c "import requests, bs4, openpyxl" 2>/dev/null; } # cryptography llegó después (copias cifradas, contraseña y sincronizar con otro # Caudal): a un entorno ya hecho se le añade; si no se puede, el resto funciona con_cifrado() { "$VENV/bin/python" -c "import cryptography" 2>/dev/null && return 0 { [ -d wheels ] && "$VENV/bin/pip" install --quiet --no-index --find-links wheels "cryptography>=38.0" 2>/dev/null; } \ || "$VENV/bin/pip" install --quiet "cryptography>=38.0" 2>/dev/null \ || echo "⚠ No se pudo instalar «cryptography» (¿sin internet?): todo funciona salvo las copias cifradas y sincronizar con otro Caudal." } if listo; then con_cifrado exit 0 fi rm -rf "$VENV" if ! python3 -m venv "$VENV" 2>/dev/null; then rm -rf "$VENV" v=$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])') echo "✗ No se pudo crear el entorno. Instala el paquete que falta: sudo apt install python${v}-venv" exit 1 fi if [ -d wheels ] && "$VENV/bin/pip" install --quiet --no-index --find-links wheels -r requirements.txt 2>/dev/null; then echo "✓ Entorno listo (paquetes de wheels/, sin internet)." elif "$VENV/bin/pip" install --quiet -r requirements.txt; then echo "✓ Entorno listo (paquetes descargados de internet)." [ -d wheels ] || echo " Consejo: «bash programa/entorno_python.sh --guardar-wheels» para poder instalar sin internet." else rm -rf "$VENV" echo "✗ No se pudieron instalar los paquetes (sin wheels/ para esta versión de Python y sin internet)." exit 1 fi listo || { echo "✗ El entorno no quedó bien."; exit 1; }