diff --git a/.gitea/preparse.sh b/.gitea/preparse.sh index 13c706f..e87647d 100755 --- a/.gitea/preparse.sh +++ b/.gitea/preparse.sh @@ -1,15 +1,37 @@ #!/usr/bin/env bash # Reproduces the CI "Preparse .sage files" step locally, so `ruff check` sees # the same generated *.sage.py files that the pipeline lints. Run this before -# `sage --python -m ruff check --no-respect-gitignore .` to catch issues that -# only show up in the generated output (e.g. missing trailing newlines). +# `ruff check --no-respect-gitignore .` to catch issues that only show up in +# the generated output. +# +# Uses the sage.repl.preparse.preparse_file() Python API directly rather than +# the `sage --preparse` CLI flag: the conda-forge `sage` binary used in CI is +# a cut-down entry point that doesn't support `--preparse` (or `--python`) at +# all, unlike the official sagemath CLI. Whichever `python` on PATH can +# actually `import sage.repl` is used to run it -- in CI that's the conda +# env's own `python`; locally (official sage install) it's `sage --python`. # # The generated files are gitignored; clean them up afterwards with: # git clean -x sage/ playground/ set -e +if command -v python >/dev/null 2>&1 && python -c "import sage.repl" >/dev/null 2>&1; then + run_python() { python "$@"; } +else + run_python() { sage --python "$@"; } +fi + for f in $(find . -name "*.sage" -not -path "./playground/*"); do - sage --preparse "$f" - printf 'from sage.all import * # noqa: F401,F403\n%s\n' "$(cat "${f}.py")" > "${f}.py" + run_python -c " +import sys +from sage.repl.preparse import preparse_file + +path = sys.argv[1] +with open(path) as fh: + src = fh.read() +with open(path + '.py', 'w') as fh: + fh.write('from sage.all import * # noqa: F401,F403\n') + fh.write(preparse_file(src)) +" "$f" done