|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fail before a build when the installed Hugo is below the site's minimum.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +MIN_VERSION_PATTERN = re.compile(r"^\s*min\s*=\s*['\"]([^'\"]+)['\"]\s*$", re.MULTILINE) |
| 13 | + |
| 14 | + |
| 15 | +def parse_version(value: str) -> tuple[int, int, int] | None: |
| 16 | + """Parse a semantic Hugo version from text.""" |
| 17 | + match = re.search(r"(\d+)\.(\d+)\.(\d+)", value) |
| 18 | + if not match: |
| 19 | + return None |
| 20 | + return tuple(int(part) for part in match.groups()) |
| 21 | + |
| 22 | + |
| 23 | +def read_minimum_version(config_path: Path) -> str: |
| 24 | + """Read module.hugoVersion.min from the site's TOML configuration.""" |
| 25 | + config = config_path.read_text(encoding="utf-8") |
| 26 | + section_match = re.search( |
| 27 | + r"(?ms)^[ \t]*\[module\.hugoVersion\][ \t]*\n?(.*?)(?=^[ \t]*\[|\Z)", |
| 28 | + config, |
| 29 | + ) |
| 30 | + if section_match is None: |
| 31 | + raise ValueError(f"Missing [module.hugoVersion] section in {config_path}") |
| 32 | + |
| 33 | + min_match = MIN_VERSION_PATTERN.search(section_match.group(1)) |
| 34 | + if min_match is None: |
| 35 | + raise ValueError(f"Missing module.hugoVersion.min in {config_path}") |
| 36 | + |
| 37 | + minimum = min_match.group(1) |
| 38 | + if parse_version(minimum) is None: |
| 39 | + raise ValueError(f"Invalid Hugo minimum version in {config_path}: {minimum}") |
| 40 | + return minimum |
| 41 | + |
| 42 | + |
| 43 | +def installed_version(hugo_command: str) -> tuple[str, tuple[int, int, int]]: |
| 44 | + """Return the version reported by the selected Hugo executable.""" |
| 45 | + result = subprocess.run( |
| 46 | + [hugo_command, "version"], |
| 47 | + check=True, |
| 48 | + capture_output=True, |
| 49 | + text=True, |
| 50 | + ) |
| 51 | + output = f"{result.stdout}\n{result.stderr}" |
| 52 | + version = parse_version(output) |
| 53 | + if version is None: |
| 54 | + raise ValueError(f"Could not parse Hugo version from: {output.strip()}") |
| 55 | + return ".".join(str(part) for part in version), version |
| 56 | + |
| 57 | + |
| 58 | +def main() -> int: |
| 59 | + parser = argparse.ArgumentParser(description=__doc__) |
| 60 | + parser.add_argument("--config", type=Path, default=Path("config.toml")) |
| 61 | + parser.add_argument("--hugo", default="hugo", help="Hugo executable to check") |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + try: |
| 65 | + minimum_text = read_minimum_version(args.config) |
| 66 | + minimum = parse_version(minimum_text) |
| 67 | + if minimum is None: |
| 68 | + raise ValueError(f"Invalid Hugo minimum version: {minimum_text}") |
| 69 | + current_text, current = installed_version(args.hugo) |
| 70 | + except (OSError, subprocess.CalledProcessError, ValueError) as error: |
| 71 | + print(f"Hugo version check failed: {error}", file=sys.stderr) |
| 72 | + return 1 |
| 73 | + |
| 74 | + if current < minimum: |
| 75 | + print( |
| 76 | + f"Hugo {current_text} is too old for this site. " |
| 77 | + f"Hugo >= {minimum_text} is required by " |
| 78 | + f"[module.hugoVersion].min in {args.config}.", |
| 79 | + file=sys.stderr, |
| 80 | + ) |
| 81 | + return 1 |
| 82 | + |
| 83 | + print(f"Hugo {current_text} satisfies the minimum version {minimum_text}.") |
| 84 | + return 0 |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + raise SystemExit(main()) |
0 commit comments