diff --git a/scripts/bootstrap.ps1 b/scripts/bootstrap.ps1 index 83e2f8e..2b2e6ff 100644 --- a/scripts/bootstrap.ps1 +++ b/scripts/bootstrap.ps1 @@ -27,6 +27,7 @@ param( [switch]$NoInstall, [switch]$NoPlaywright, [string]$PlaywrightBrowsers = "chromium", + [switch]$FixUrllib3, [switch]$Quiet ) @@ -191,8 +192,41 @@ if (-not $NoInstall) { Write-Log " $ $venvPython -m pip uninstall urllib3-future -y" "INFO" Write-Log " $ $venvPython -m pip install --upgrade --force-reinstall urllib3" "INFO" Write-Log " $ $venvPython -m pip install niquests -U" "INFO" - Write-Log "Aborting bootstrap to avoid leaving a broken environment." "ERROR" - exit 7 + + if ($FixUrllib3) { + Write-Log "Attempting automatic fix (--FixUrllib3)..." "INFO" + try { & $venvPython -m pip uninstall urllib3-future -y } catch {} + try { & $venvPython -m pip install --upgrade --force-reinstall urllib3 } catch { Write-Log "pip install failed: $_" "ERROR"; exit 7 } + try { & $venvPython -m pip install niquests -U } catch { Write-Log "pip install niquests failed: $_" "ERROR" } + & $venvPython -c "import sys; from SYS.env_check import check_urllib3_compat; ok, msg = check_urllib3_compat(); print(msg); sys.exit(0 if ok else 2)" + if ($LASTEXITCODE -ne 0) { + Write-Log "Automatic fix failed; aborting." "ERROR" + exit 7 + } else { + Write-Log "Success: urllib3 problems appear resolved; continuing." "INFO" + } + } else { + if ($Quiet) { + Write-Log "Bootstrap detected a potentially broken 'urllib3' installation. Use -FixUrllib3 to attempt an automatic fix." "ERROR" + exit 7 + } + $ans = Read-Host "Attempt automatic fix now? (y/N)" + if ($ans -eq 'y' -or $ans -eq 'Y') { + try { & $venvPython -m pip uninstall urllib3-future -y } catch {} + try { & $venvPython -m pip install --upgrade --force-reinstall urllib3 } catch { Write-Log "pip install failed: $_" "ERROR"; exit 7 } + try { & $venvPython -m pip install niquests -U } catch { Write-Log "pip install niquests failed: $_" "ERROR" } + & $venvPython -c "import sys; from SYS.env_check import check_urllib3_compat; ok, msg = check_urllib3_compat(); print(msg); sys.exit(0 if ok else 2)" + if ($LASTEXITCODE -ne 0) { + Write-Log "Automatic fix failed; aborting." "ERROR" + exit 7 + } else { + Write-Log "Success: urllib3 problems appear resolved; continuing." "INFO" + } + } else { + Write-Log "Aborting bootstrap to avoid leaving a broken environment." "ERROR" + exit 7 + } + } } } catch { Write-Log "Failed to run environment verification: $_" "ERROR" diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 07890e4..28f585e 100644 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -24,11 +24,39 @@ PYTHON_CMD="" NOINSTALL=false FORCE=false QUIET=false +FIX_URLLIB3=false # Playwright options PLAYWRIGHT_BROWSERS="chromium" # comma-separated (chromium,firefox,webkit) or 'all' NO_PLAYWRIGHT=false -usage() { +attempt_fix_urllib3() { + local venv_py="$1" + echo "Attempting automatic urllib3 fix in venv: $venv_py" >&2 + # Temporarily disable set -e to inspect return codes ourselves + set +e + "$venv_py" -m pip uninstall urllib3-future -y >/dev/null 2>&1 || true + "$venv_py" -m pip install --upgrade --force-reinstall urllib3 + local pip_ret=$? + # Best-effort install/update for niquests; non-fatal + "$venv_py" -m pip install niquests -U >/dev/null 2>&1 || true + set -e + + if [[ $pip_ret -ne 0 ]]; then + echo "ERROR: pip failed to reinstall urllib3 (exit $pip_ret)" >&2 + return 2 + fi + + # Verify fix + if "$venv_py" -c 'from SYS.env_check import check_urllib3_compat; ok,msg = check_urllib3_compat(); import sys; sys.exit(0 if ok else 2)'; then + echo "Success: urllib3 issues resolved" >&2 + return 0 + fi + + echo "ERROR: urllib3 fix attempt incomplete (import check failed)" >&2 + return 3 +} + +usage() { cat < Comma-separated list of browsers to install (default: chromium) -q, --quiet Quiet / non-interactive mode; abort on errors instead of prompting + -F, --fix-urllib3 Attempt to automatically fix known urllib3 issues in the venv if detected -f, --force Overwrite existing venv without prompting -h, --help Show this help EOF @@ -53,6 +82,7 @@ while [[ $# -gt 0 ]]; do -d|--desktop) DESKTOP=true; shift;; -n|--no-install) NOINSTALL=true; shift;; -f|--force) FORCE=true; shift;; + -F|--fix-urllib3) FIX_URLLIB3=true; shift;; -q|--quiet) QUIET=true; shift;; -h|--help) usage; exit 0;; --no-playwright) NO_PLAYWRIGHT=true; shift;; @@ -172,7 +202,33 @@ if [[ "$NOINSTALL" != "true" ]]; then echo " $VENV_PY -m pip uninstall urllib3-future -y" >&2 echo " $VENV_PY -m pip install --upgrade --force-reinstall urllib3" >&2 echo " $VENV_PY -m pip install niquests -U" >&2 - exit 7 + echo "" >&2 + if [[ "$FIX_URLLIB3" == "true" ]]; then + echo "Attempting automatic fix (--fix-urllib3 requested)..." >&2 + if attempt_fix_urllib3 "$VENV_PY"; then + echo "Success: urllib3 issues resolved; continuing..." >&2 + else + echo "ERROR: Automatic fix did not resolve the issue; inspect .pth files in site-packages (e.g., urllib3_future.pth) and remove them, then re-run with --fix-urllib3" >&2 + exit 7 + fi + else + if [[ "$QUIET" == "true" ]]; then + echo "ERROR: Bootstrap detected a potentially broken 'urllib3' installation. Use --fix-urllib3 to attempt an automatic fix." >&2 + exit 7 + fi + read -p "Attempt automatic fix now? (y/N) " REPLY + if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then + if attempt_fix_urllib3 "$VENV_PY"; then + echo "Success: urllib3 issues resolved; continuing..." >&2 + else + echo "ERROR: Automatic fix did not resolve the issue; aborting." >&2 + exit 7 + fi + else + echo "Aborting bootstrap. Re-run with --fix-urllib3 to attempt an automatic fix or run the commands above." >&2 + exit 7 + fi + fi fi # Install Playwright browsers (default: chromium) unless explicitly disabled