1616from pathlib import Path
1717
1818import yaml
19+ from retry_lychee import read_report
1920
2021ROOT = Path (__file__ ).resolve ().parents [2 ]
22+ REPORT_NAME = "lychee.json"
2123
2224
2325def write_config (directory : Path ) -> None :
2426 # Use the checked-in hook, including its entry point and pinned dependency.
2527 # PyYAML is already installed as a dependency of pre-commit.
2628 config = yaml .safe_load ((ROOT / ".pre-commit-config.yaml" ).read_text (encoding = "utf-8" ))
2729 [hook ] = [hook for repo in config ["repos" ] for hook in repo ["hooks" ] if hook ["id" ] == "lychee" ]
28- hook .setdefault ("args" , []).append ("--offline" )
30+ report = directory / REPORT_NAME
31+ hook .setdefault ("args" , []).extend (["--offline" , "--format" , "json" , "--output" , str (report )])
2932 config_path = directory / "pre-commit.yaml"
3033 config_path .write_text (yaml .safe_dump (config ), encoding = "utf-8" )
3134
3235
33- def verify (output : str , exit_code : int ) -> int :
34- print (output , end = "" )
35- if exit_code == 0 and ("- hook id: lychee" not in output or "Skipped" in output ):
36- raise SystemExit ("The configured Lychee hook did not run" )
37- return exit_code
36+ def verify (log : Path , exit_code : int ) -> int :
37+ report = log .with_name (REPORT_NAME )
38+ try :
39+ print (log .read_text (encoding = "utf-8" ), end = "" )
40+ if exit_code != 0 :
41+ return exit_code
42+ if not report .is_file ():
43+ raise SystemExit (f"The configured Lychee hook produced no report ({ report } ); was it skipped?" )
44+ read_report (report , exit_code )
45+ return 0
46+ finally :
47+ # Consume each shell's report so a later skipped hook cannot reuse it.
48+ report .unlink (missing_ok = True )
3849
3950
4051def main () -> int :
@@ -50,7 +61,7 @@ def main() -> int:
5061 return 0
5162 if args .verify is not None :
5263 log , exit_code = args .verify
53- return verify (( ROOT / log ). read_text ( encoding = "utf-8" ) , int (exit_code ))
64+ return verify (ROOT / log , int (exit_code ))
5465 directory = (ROOT / args .cleanup ).resolve ()
5566 if directory .parent != ROOT or not directory .name .startswith ("lychee offline smoke " ):
5667 raise SystemExit ("Refusing to remove a directory outside the smoke check's temporary files" )
0 commit comments