opensheaf-literate

Bootstrapping: a tangler tangled by itself

The files of this package are tangled out of these documents by the package itself. The tangled files are kept in the repository, so that the package installs as any other, and so that there is a tangler to begin with: the copy in src/literate/. But a change to the chapter on tangling is a change to the tangler, and a tangler that tangles its own source wrongly could write a wrong tangler without anyone noticing. So, following build_handaxeweb of handaxeweb, a change is only accepted if the new tangler, run on the documents, reproduces itself:

  1. the tangler of the repository tangles everything into a temporary folder, a;
  2. the new tangler found in a tangles everything again, into b;
  3. a and b must agree, file for file;
  4. the tangler of b then writes the files into the project, and weaves the website if it can.

If step 3 fails, the new tangler does not tangle as the old one did: either the change to the tangler was meant to change its output, and running the script a second time (now with the new tangler in the repository) agrees, or it was not, and the documents need another look.

#!/bin/sh
# Tangle the program out of literate/, checking that the tangler
# reproduces itself, then weave the website into site/ if markdown and
# pygments are installed.
set -e
cd "$(dirname "$0")/.."
⟨run Python on a given copy of the package⟩
bootstrap() {
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    literate src tangle --out "$tmp/a" >/dev/null
    literate "$tmp/a/src" tangle --out "$tmp/b" >/dev/null
    diff -r "$tmp/a" "$tmp/b"
    literate "$tmp/b/src" build
}
bootstrap
exit

The work is done in a function, called on the last lines: the shell reads a script as it runs it, and the last step may rewrite this very script; a function is read whole before it runs, and exit keeps the shell from reading on. The tangling into the temporary folders writes every file and says so; only the last step's report is of interest.

The package to run is chosen by putting its folder first on Python's path; an installed copy of the package comes after it and does not interfere. Python must not write its compiled files into the temporary folders, or they would differ for that reason alone.

export PYTHONDONTWRITEBYTECODE=1
literate() {
    dir=$1
    shift
    PYTHONPATH="$dir" python3 -m literate "$@"
}

The continuous integration runs this script and then checks, with git diff --exit-code, that the files in the repository are the ones it writes.

What git ignores

The website and what Python and its packaging leave behind are not kept.

__pycache__/
*.egg-info/
build/
dist/
.venv/
.pytest_cache/
.DS_Store
site/