|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Locate nglview's Python frontend file |
| 5 | +NGLVIEW_DIR=$(python -c "import nglview, os; print(os.path.dirname(nglview.__file__))") |
| 6 | +FRONTEND_FILE="$NGLVIEW_DIR/_frontend.py" |
| 7 | + |
| 8 | +if [[ ! -f "$FRONTEND_FILE" ]]; then |
| 9 | + echo "nglview _frontend.py not found, skipping patch" >&2 |
| 10 | + exit 0 |
| 11 | +fi |
| 12 | + |
| 13 | +# Locate the installed labextension's package.json by searching |
| 14 | +# every labextensions directory Jupyter knows about (handles conda's |
| 15 | +# $CONDA_PREFIX/share/jupyter/labextensions layout automatically) |
| 16 | +LABEXT_PKG=$(python -c " |
| 17 | +from jupyter_core.paths import jupyter_path |
| 18 | +import os, sys |
| 19 | +for d in jupyter_path('labextensions'): |
| 20 | + candidate = os.path.join(d, 'nglview-js-widgets', 'package.json') |
| 21 | + if os.path.isfile(candidate): |
| 22 | + print(candidate) |
| 23 | + sys.exit(0) |
| 24 | +sys.exit(1) |
| 25 | +") || { echo "nglview-js-widgets labextension not found, skipping patch" >&2; exit 0; } |
| 26 | + |
| 27 | +PY_VERSION=$(python -c "import re; print(re.search(r\"__frontend_version__ = '([^']+)'\", open('$FRONTEND_FILE').read()).group(1))") |
| 28 | +JS_VERSION=$(python -c "import json; print(json.load(open('$LABEXT_PKG'))['version'])") |
| 29 | + |
| 30 | +# If there is a version mismatch then make them the same python side. |
| 31 | +if [[ "$PY_VERSION" != "$JS_VERSION" ]]; then |
| 32 | + echo "nglview mismatch: python=$PY_VERSION js=$JS_VERSION (found at $LABEXT_PKG) -- patching" |
| 33 | + sed -i "s/__frontend_version__ = '$PY_VERSION'/__frontend_version__ = '$JS_VERSION'/" "$FRONTEND_FILE" |
| 34 | +else |
| 35 | + echo "nglview versions already match ($PY_VERSION), skipping patch" |
| 36 | +fi |
0 commit comments