From 32494a97ac36d40ffbd7e8deecf789eaa50b38e2 Mon Sep 17 00:00:00 2001 From: nose Date: Wed, 24 Dec 2025 02:42:13 -0800 Subject: [PATCH] dfd --- scripts/bootstrap.ps1 | 125 +++++++++++++++++++++++++++++++++++++++--- scripts/bootstrap.sh | 84 ++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 8 deletions(-) diff --git a/scripts/bootstrap.ps1 b/scripts/bootstrap.ps1 index 2b2e6ff..12d6452 100644 --- a/scripts/bootstrap.ps1 +++ b/scripts/bootstrap.ps1 @@ -28,6 +28,7 @@ param( [switch]$NoPlaywright, [string]$PlaywrightBrowsers = "chromium", [switch]$FixUrllib3, + [switch]$RemovePth, [switch]$Quiet ) @@ -193,17 +194,98 @@ if (-not $NoInstall) { Write-Log " $ $venvPython -m pip install --upgrade --force-reinstall urllib3" "INFO" Write-Log " $ $venvPython -m pip install niquests -U" "INFO" + function Get-SitePackages { + param($python) + try { + $json = & $python -c "import site, sysconfig, json; p=[]; +try: + p.extend(site.getsitepackages()) +except Exception: + pass +try: + pp = sysconfig.get_paths().get('purelib') + if pp: + p.append(pp) +except Exception: + pass +seen = [] +out = [] +for s in p: + if s and s not in seen: + seen.append(s) + out.append(s) +print(json.dumps(out))" + return $json | ConvertFrom-Json + } catch { + return @() + } + } + + function Find-InterferingPth { + param($python) + $pths = @() + $sps = Get-SitePackages -python $python + foreach ($sp in $sps) { + if (Test-Path $sp) { + Get-ChildItem -Path $sp -Filter *.pth -File -ErrorAction SilentlyContinue | ForEach-Object { + $c = Get-Content -Path $_.FullName -ErrorAction SilentlyContinue | Out-String + if ($c -match 'urllib3_future' -or $c -match 'urllib3-future') { + $pths += $_.FullName + } + } + } + } + return $pths + } + + # Helper to try removal and re-verify + function RemovePthAndVerify { + param($python, $paths) + foreach ($p in $paths) { Remove-Item -Force $p -ErrorAction SilentlyContinue } + try { & $python -m pip install --upgrade --force-reinstall urllib3 } catch { Write-Log "pip install failed: $_" "ERROR"; return $false } + & $python -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)" + return ($LASTEXITCODE -eq 0) + } + 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 { + if ($LASTEXITCODE -eq 0) { Write-Log "Success: urllib3 problems appear resolved; continuing." "INFO" + } else { + Write-Log "Initial automatic fix did not resolve the issue; searching for interfering .pth files..." "INFO" + $pths = Find-InterferingPth -python $venvPython + if ($pths.Count -eq 0) { + Write-Log "No interfering .pth files found; aborting." "ERROR" + exit 7 + } + Write-Log ("Found interfering .pth files:`n" + ($pths -join "`n")) "ERROR" + if ($RemovePth) { + Write-Log "Removing .pth files as requested..." "INFO" + if (RemovePthAndVerify -python $venvPython -paths $pths) { + Write-Log "Success: urllib3 problems resolved after .pth removal; continuing." "INFO" + } else { + Write-Log "Automatic fix failed even after .pth removal; aborting." "ERROR" + exit 7 + } + } else { + if ($Quiet) { Write-Log "Detected interfering .pth files but cannot prompt in quiet mode. Use -RemovePth to remove them automatically." "ERROR"; exit 7 } + $ans = Read-Host "Remove these files now? (y/N)" + if ($ans -eq 'y' -or $ans -eq 'Y') { + if (RemovePthAndVerify -python $venvPython -paths $pths) { + Write-Log "Success: urllib3 problems resolved after .pth removal; continuing." "INFO" + } else { + Write-Log "Automatic fix failed even after .pth removal; aborting." "ERROR" + exit 7 + } + } else { + Write-Log "User declined to remove .pth files. Aborting." "ERROR" + exit 7 + } + } } } else { if ($Quiet) { @@ -216,11 +298,38 @@ if (-not $NoInstall) { 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 { + if ($LASTEXITCODE -eq 0) { Write-Log "Success: urllib3 problems appear resolved; continuing." "INFO" + } else { + Write-Log "Initial automatic fix did not resolve the issue; searching for interfering .pth files..." "INFO" + $pths = Find-InterferingPth -python $venvPython + if ($pths.Count -eq 0) { + Write-Log "No interfering .pth files found; aborting." "ERROR" + exit 7 + } + Write-Log ("Found interfering .pth files:`n" + ($pths -join "`n")) "ERROR" + if ($RemovePth) { + Write-Log "Removing .pth files as requested..." "INFO" + if (RemovePthAndVerify -python $venvPython -paths $pths) { + Write-Log "Success: urllib3 problems resolved after .pth removal; continuing." "INFO" + } else { + Write-Log "Automatic fix failed even after .pth removal; aborting." "ERROR" + exit 7 + } + } else { + $ans2 = Read-Host "Remove these files now? (y/N)" + if ($ans2 -eq 'y' -or $ans2 -eq 'Y') { + if (RemovePthAndVerify -python $venvPython -paths $pths) { + Write-Log "Success: urllib3 problems resolved after .pth removal; continuing." "INFO" + } else { + Write-Log "Automatic fix failed even after .pth removal; aborting." "ERROR" + exit 7 + } + } else { + Write-Log "User declined to remove .pth files. Aborting." "ERROR" + exit 7 + } + } } } else { Write-Log "Aborting bootstrap to avoid leaving a broken environment." "ERROR" diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 28f585e..8d7fbd9 100644 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -28,6 +28,7 @@ FIX_URLLIB3=false # Playwright options PLAYWRIGHT_BROWSERS="chromium" # comma-separated (chromium,firefox,webkit) or 'all' NO_PLAYWRIGHT=false +REMOVE_PTH=false attempt_fix_urllib3() { local venv_py="$1" @@ -53,6 +54,87 @@ attempt_fix_urllib3() { fi echo "ERROR: urllib3 fix attempt incomplete (import check failed)" >&2 + + # Detect potential interfering .pth files in site-packages + echo "Searching for interfering .pth files in the venv site-packages..." >&2 + local site_pkgs + site_pkgs=$("$venv_py" - <<'PY' +import json, site, sysconfig +paths = [] +try: + paths.extend(site.getsitepackages()) +except Exception: + pass +try: + p = sysconfig.get_paths().get('purelib') + if p: + paths.append(p) +except Exception: + pass +seen = [] +out = [] +for s in paths: + if s and s not in seen: + seen.append(s) + out.append(s) +print("\n".join(out)) +PY +) + local pths=() + if [[ -n "$site_pkgs" ]]; then + while IFS= read -r sp; do + if [[ -d "$sp" ]]; then + while IFS= read -r f; do + if [[ -n "$f" ]]; then + if grep -qi 'urllib3_future' "$f" >/dev/null 2>&1 || grep -qi 'urllib3-future' "$f" >/dev/null 2>&1; then + pths+=("$f") + fi + fi + done < <(find "$sp" -maxdepth 1 -type f -name '*.pth' -print 2>/dev/null) + fi + done <<< "$site_pkgs" + fi + + if [[ ${#pths[@]} -eq 0 ]]; then + echo "No obvious interfering .pth files found in site-packages. Manual inspection recommended." >&2 + return 3 + fi + + echo "Found the following potentially interfering .pth files:" >&2 + for p in "${pths[@]}"; do echo " - $p" >&2; done + + if [[ "$REMOVE_PTH" == "true" ]]; then + echo "Removing .pth files as requested..." >&2 + for p in "${pths[@]}"; do rm -f "$p"; done + else + if [[ "$QUIET" == "true" ]]; then + echo "Detected interfering .pth files but cannot prompt in quiet mode. Re-run with --remove-pth to remove them automatically." >&2 + return 3 + fi + read -p "Remove these files now? (y/N) " resp + if [[ "$resp" != "y" && "$resp" != "Y" ]]; then + echo "User declined to remove .pth files. Aborting." >&2 + return 3 + fi + for p in "${pths[@]}"; do rm -f "$p"; done + fi + + # Re-run reinstall & verify + echo "Reinstalling urllib3 after .pth removal..." >&2 + set +e + "$venv_py" -m pip install --upgrade --force-reinstall urllib3 + local pip_ret2=$? + set -e + if [[ $pip_ret2 -ne 0 ]]; then + echo "ERROR: pip reinstall failed after .pth removal (exit $pip_ret2)" >&2 + return 2 + fi + 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 after .pth removal" >&2 + return 0 + fi + + echo "ERROR: urllib3 still not importable after .pth removal" >&2 return 3 } @@ -69,6 +151,7 @@ Options: --playwright-browsers 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 + -R, --remove-pth When fixing urllib3, automatically remove interfering .pth files (like urllib3_future.pth) -f, --force Overwrite existing venv without prompting -h, --help Show this help EOF @@ -83,6 +166,7 @@ while [[ $# -gt 0 ]]; do -n|--no-install) NOINSTALL=true; shift;; -f|--force) FORCE=true; shift;; -F|--fix-urllib3) FIX_URLLIB3=true; shift;; + -R|--remove-pth) REMOVE_PTH=true; shift;; -q|--quiet) QUIET=true; shift;; -h|--help) usage; exit 0;; --no-playwright) NO_PLAYWRIGHT=true; shift;;