|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate the shell commands printed in the manual. |
| 3 | +
|
| 4 | +Two checks, both aimed at the same failure: a farmer copies a command out of |
| 5 | +the manual and it does not work. |
| 6 | +
|
| 7 | + 1. SHELL -- every shell block is linted with shellcheck (severity=error), |
| 8 | + falling back to `bash -n` when shellcheck is unavailable. |
| 9 | +
|
| 10 | + shellcheck rather than `bash -n` because `bash -n` only checks syntax. |
| 11 | + The disk-wipe loop that silently wiped nothing -- |
| 12 | +
|
| 13 | + for i in /dev/sd*; do if [ "$i"!= "/dev/sdX"* ]; then ... ; fi; done |
| 14 | +
|
| 15 | + -- is syntactically VALID; it fails at runtime with "unary operator |
| 16 | + expected". `bash -n` passes it. shellcheck catches it (SC1108, "you need |
| 17 | + a space before and after the ="). |
| 18 | +
|
| 19 | + This covers ```bash / ```sh blocks AND untagged ``` blocks whose first |
| 20 | + line looks like a command. Untagged blocks matter: most of the manual's |
| 21 | + commands are untagged, and that wipe loop lived in one. |
| 22 | +
|
| 23 | + The manual's own conventions are respected, not fought: |
| 24 | + - `<placeholder>` is normalised before linting, because angle brackets |
| 25 | + are shell redirection and every page uses them as placeholders. |
| 26 | + - Pasted terminal sessions (first line is a prompt) are skipped, since |
| 27 | + they are output rather than commands. Tag genuine output as ```text |
| 28 | + or ```console rather than ```bash. |
| 29 | + - Blocks containing a heredoc are skipped: the body is data, and |
| 30 | + shellcheck mis-parses it when the block is linted out of context. |
| 31 | +
|
| 32 | + 2. TWIN DRIFT -- farmers/ and labs/ deliberately carry two versions of the |
| 33 | + same five build pages, written for different audiences. The prose is |
| 34 | + meant to differ; the commands are not. This flags a command block that |
| 35 | + exists in both but has drifted, which is what happens when a fix lands |
| 36 | + in one tree and is forgotten in the other. |
| 37 | +
|
| 38 | +Exits non-zero if either check fails. Run from the repo root. |
| 39 | +""" |
| 40 | + |
| 41 | +import difflib |
| 42 | +import os |
| 43 | +import re |
| 44 | +import subprocess |
| 45 | +import sys |
| 46 | +import tempfile |
| 47 | + |
| 48 | +SKIP_DIRS = ('build', '.docusaurus', 'node_modules', '.git') |
| 49 | +SHELL_LANGS = {'bash', 'sh', 'shell', 'zsh'} |
| 50 | + |
| 51 | +# A pasted terminal session: "$ cmd", "# cmd", or "user@host ... $ cmd". |
| 52 | +PROMPT = re.compile(r'^\s*(\$\s|#\s|\S+@\S+.*?[#$]\s)') |
| 53 | +PLACEHOLDER = re.compile(r'<[A-Za-z0-9_\-. /]+>') |
| 54 | +FENCE = re.compile(r'^(\s*)```(\S*)\s*$') |
| 55 | + |
| 56 | +# An untagged block is treated as shell when its first line opens with a |
| 57 | +# command. Deliberately conservative: it is better to skip an odd block than |
| 58 | +# to fail the build on a config file someone forgot to tag. |
| 59 | +SHELLY = re.compile( |
| 60 | + r'^\s*(sudo|apt|apt-get|wget|curl|git|cd|mkdir|echo|export|for |if |while ' |
| 61 | + r'|docker|systemctl|chmod|chown|ln |cp |mv |rm |tar|ssh|scp|npm|yarn' |
| 62 | + r'|cargo|pip|make|set |source |\./)\b') |
| 63 | + |
| 64 | +# Pages that exist in both trees. Prose may differ; commands must not. |
| 65 | +TWINS = [ |
| 66 | + ('farmers/docs/3node_building/{}.md', |
| 67 | + 'labs/docs/documentation/farmers/3node_building/{}.md') |
| 68 | +] |
| 69 | +TWIN_PAGES = ['2_bootstrap_image', '3_set_hardware', '4_wipe_all_disks', |
| 70 | + '5_set_bios_uefi', '6_boot_3node'] |
| 71 | + |
| 72 | + |
| 73 | +def markdown_files(): |
| 74 | + for root, dirs, files in os.walk('.'): |
| 75 | + dirs[:] = [d for d in dirs if d not in SKIP_DIRS] |
| 76 | + for f in files: |
| 77 | + if f.endswith(('.md', '.mdx')): |
| 78 | + yield os.path.join(root, f).replace('./', '', 1) |
| 79 | + |
| 80 | + |
| 81 | +def code_blocks(path): |
| 82 | + """Yield (lang, start_line, body) for each fenced block.""" |
| 83 | + lines = open(path, encoding='utf-8', errors='replace').read().split('\n') |
| 84 | + i = 0 |
| 85 | + while i < len(lines): |
| 86 | + m = FENCE.match(lines[i]) |
| 87 | + if not m: |
| 88 | + i += 1 |
| 89 | + continue |
| 90 | + lang = m.group(2).lower() |
| 91 | + start = i + 1 |
| 92 | + body = [] |
| 93 | + i += 1 |
| 94 | + while i < len(lines) and not lines[i].strip() == '```' and not FENCE.match(lines[i]): |
| 95 | + body.append(lines[i]) |
| 96 | + i += 1 |
| 97 | + yield lang, start, '\n'.join(body) |
| 98 | + i += 1 |
| 99 | + |
| 100 | + |
| 101 | +def have_shellcheck(): |
| 102 | + try: |
| 103 | + subprocess.run(['shellcheck', '--version'], |
| 104 | + capture_output=True, check=True) |
| 105 | + return True |
| 106 | + except (OSError, subprocess.CalledProcessError): |
| 107 | + return False |
| 108 | + |
| 109 | + |
| 110 | +def check_syntax(): |
| 111 | + failures = [] |
| 112 | + checked = 0 |
| 113 | + sc = have_shellcheck() |
| 114 | + for path in sorted(markdown_files()): |
| 115 | + for lang, line, body in code_blocks(path): |
| 116 | + if not body.strip(): |
| 117 | + continue |
| 118 | + first = next((l for l in body.split('\n') if l.strip()), '') |
| 119 | + if lang in SHELL_LANGS: |
| 120 | + pass |
| 121 | + elif lang == '' and SHELLY.match(first): |
| 122 | + pass # untagged, but opens with a command |
| 123 | + else: |
| 124 | + continue |
| 125 | + if PROMPT.match(first): |
| 126 | + continue # pasted session, not a command to run |
| 127 | + if '<<' in body: |
| 128 | + continue # heredoc body is data, not shell |
| 129 | + checked += 1 |
| 130 | + with tempfile.NamedTemporaryFile('w', suffix='.sh', delete=False) as fh: |
| 131 | + fh.write('#!/bin/bash\n' + PLACEHOLDER.sub('PLACEHOLDER', body)) |
| 132 | + tmp = fh.name |
| 133 | + try: |
| 134 | + if sc: |
| 135 | + r = subprocess.run( |
| 136 | + ['shellcheck', '-s', 'bash', '--severity', 'error', |
| 137 | + '-f', 'gcc', tmp], capture_output=True, text=True) |
| 138 | + detail = (r.stdout.strip().split('\n')[0].split(':', 3)[-1].strip() |
| 139 | + if r.stdout.strip() else r.stderr.strip()[:80]) |
| 140 | + else: |
| 141 | + r = subprocess.run(['bash', '-n', tmp], |
| 142 | + capture_output=True, text=True) |
| 143 | + detail = r.stderr.strip().split('\n')[0].split(': ', 1)[-1] |
| 144 | + finally: |
| 145 | + os.unlink(tmp) |
| 146 | + if r.returncode != 0: |
| 147 | + failures.append((path, line, detail, first.strip()[:60])) |
| 148 | + if not sc: |
| 149 | + print(" note: shellcheck not found, fell back to `bash -n`" |
| 150 | + " (syntax only -- weaker)") |
| 151 | + return checked, failures |
| 152 | + |
| 153 | + |
| 154 | +def check_twin_drift(): |
| 155 | + def shell_bodies(path): |
| 156 | + return [b.strip() for _, _, b in code_blocks(path) if b.strip()] |
| 157 | + |
| 158 | + drift = [] |
| 159 | + shared = 0 |
| 160 | + for fa_t, fb_t in TWINS: |
| 161 | + for page in TWIN_PAGES: |
| 162 | + fa, fb = fa_t.format(page), fb_t.format(page) |
| 163 | + if not (os.path.exists(fa) and os.path.exists(fb)): |
| 164 | + continue |
| 165 | + A, B = shell_bodies(fa), shell_bodies(fb) |
| 166 | + for a in A: |
| 167 | + if a in B: |
| 168 | + shared += 1 |
| 169 | + continue |
| 170 | + best_ratio, best = 0.0, None |
| 171 | + for b in B: |
| 172 | + r = difflib.SequenceMatcher(None, a, b).ratio() |
| 173 | + if r > best_ratio: |
| 174 | + best_ratio, best = r, b |
| 175 | + if best_ratio >= 0.75: |
| 176 | + drift.append((fa, fb, best_ratio, a, best)) |
| 177 | + return shared, drift |
| 178 | + |
| 179 | + |
| 180 | +def main(): |
| 181 | + ok = True |
| 182 | + |
| 183 | + checked, failures = check_syntax() |
| 184 | + print(f"shell blocks parsed : {checked}") |
| 185 | + if failures: |
| 186 | + ok = False |
| 187 | + print(f"FAILED : {len(failures)}\n") |
| 188 | + for path, line, detail, first in failures: |
| 189 | + print(f" {path}:{line}") |
| 190 | + print(f" {detail}") |
| 191 | + print(f" block starts: {first}") |
| 192 | + print("\n If the block is command OUTPUT rather than commands, tag it") |
| 193 | + print(" ```text or ```console instead of ```bash.") |
| 194 | + else: |
| 195 | + print(" all parse cleanly") |
| 196 | + |
| 197 | + shared, drift = check_twin_drift() |
| 198 | + print(f"\nshared twin blocks : {shared}") |
| 199 | + if drift: |
| 200 | + ok = False |
| 201 | + print(f"DRIFTED : {len(drift)}\n") |
| 202 | + for fa, fb, ratio, a, b in drift: |
| 203 | + print(f" {fa}") |
| 204 | + print(f" {fb}") |
| 205 | + print(f" {ratio:.0%} similar but not identical -- fix landed in one tree only?") |
| 206 | + print(f" farmers: {a.splitlines()[0][:70]}") |
| 207 | + print(f" labs: {b.splitlines()[0][:70]}") |
| 208 | + else: |
| 209 | + print(" no drift between farmers/ and labs/ command blocks") |
| 210 | + |
| 211 | + return 0 if ok else 1 |
| 212 | + |
| 213 | + |
| 214 | +if __name__ == '__main__': |
| 215 | + sys.exit(main()) |
0 commit comments