#!/usr/bin/env python3 """Reject release trees containing ELF files without debug information.""" from __future__ import annotations import argparse import os from pathlib import Path import re import shutil import subprocess DEBUG_SECTION = re.compile(r"\]\s+\.(?:z)?debug_info\s") def is_elf(path: Path) -> bool: with path.open("rb") as stream: return stream.read(4) == b"\x7fELF" def verify(root: Path) -> int: readelf = shutil.which("readelf") if readelf is None: raise ValueError("readelf is required") verified = 0 for path in sorted(root.rglob("*")): if path.is_symlink() or not path.is_file() or not is_elf(path): continue completed = subprocess.run( [readelf, "-SW", os.fspath(path)], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env={**os.environ, "LC_ALL": "C"}, ) if completed.returncode != 0: message = completed.stderr.strip() or completed.stdout.strip() raise ValueError( f"readelf failed for {path.relative_to(root)}: {message}" ) if DEBUG_SECTION.search(completed.stdout) is None: raise ValueError( f"ELF {path.relative_to(root)} has no debug information; " "expected a RelWithDebInfo artifact" ) verified += 1 if verified == 0: raise ValueError("release tree contains no ELF files to verify") return verified def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", required=True, type=Path) arguments = parser.parse_args() try: root = arguments.root.resolve(strict=True) if root.is_symlink() or not root.is_dir(): raise ValueError("--root must be a real directory") verified = verify(root) except (OSError, ValueError) as error: parser.error(str(error)) print(f"verified debug information in {verified} ELF files") return 0 if __name__ == "__main__": raise SystemExit(main())