1212from __future__ import annotations
1313
1414import argparse
15- import os
1615import shutil
17- import subprocess
18- import sys
1916from pathlib import Path
20- from tempfile import TemporaryDirectory
2117
2218import yaml
2319
2420ROOT = Path (__file__ ).resolve ().parents [2 ]
2521
2622
27- def write_config (directory : Path ) -> Path :
23+ def write_config (directory : Path ) -> None :
2824 # Use the checked-in hook, including its entry point and pinned dependency.
2925 # PyYAML is already installed as a dependency of pre-commit.
3026 config = yaml .safe_load ((ROOT / ".pre-commit-config.yaml" ).read_text (encoding = "utf-8" ))
3127 [hook ] = [hook for repo in config ["repos" ] for hook in repo ["hooks" ] if hook ["id" ] == "lychee" ]
3228 hook .setdefault ("args" , []).append ("--offline" )
3329 config_path = directory / "pre-commit.yaml"
3430 config_path .write_text (yaml .safe_dump (config ), encoding = "utf-8" )
35- return config_path
3631
3732
3833def verify (output : str , exit_code : int ) -> int :
@@ -43,9 +38,9 @@ def verify(output: str, exit_code: int) -> int:
4338
4439
4540def main () -> int :
46- # Windows CI uses separate phases so each shell invokes pre-commit directly.
41+ # CI uses separate phases so each shell invokes pre-commit directly.
4742 parser = argparse .ArgumentParser (description = __doc__ )
48- mode = parser .add_mutually_exclusive_group ()
43+ mode = parser .add_mutually_exclusive_group (required = True )
4944 mode .add_argument ("--prepare" , type = Path , metavar = "DIRECTORY" )
5045 mode .add_argument ("--verify" , nargs = 2 , metavar = ("LOG" , "EXIT_CODE" ))
5146 mode .add_argument ("--cleanup" , type = Path , metavar = "DIRECTORY" )
@@ -56,37 +51,11 @@ def main() -> int:
5651 if args .verify is not None :
5752 log , exit_code = args .verify
5853 return verify ((ROOT / log ).read_text (encoding = "utf-8" ), int (exit_code ))
59- if args .cleanup is not None :
60- directory = (ROOT / args .cleanup ).resolve ()
61- if directory .parent != ROOT or not directory .name .startswith ("lychee offline smoke " ):
62- raise SystemExit ("Refusing to remove a directory outside the smoke check's temporary files" )
63- shutil .rmtree (directory )
64- return 0
65-
66- # pre-commit resolves --config relative to the checkout; keep it on the same drive.
67- with TemporaryDirectory (prefix = "lychee offline smoke " , dir = ROOT ) as directory :
68- config_path = write_config (Path (directory ))
69- result = subprocess .run ( # noqa: S603
70- [
71- sys .executable ,
72- "-m" ,
73- "pre_commit" ,
74- "run" ,
75- "lychee" ,
76- "--config" ,
77- str (config_path ),
78- "--files" ,
79- "README.md" ,
80- "--verbose" ,
81- ],
82- cwd = ROOT ,
83- env = {** os .environ , "SKIP" : "" , "PYTHONUTF8" : "1" },
84- stdout = subprocess .PIPE ,
85- stderr = subprocess .STDOUT ,
86- encoding = "utf-8" ,
87- check = False ,
88- )
89- return verify (result .stdout , result .returncode )
54+ directory = (ROOT / args .cleanup ).resolve ()
55+ if directory .parent != ROOT or not directory .name .startswith ("lychee offline smoke " ):
56+ raise SystemExit ("Refusing to remove a directory outside the smoke check's temporary files" )
57+ shutil .rmtree (directory )
58+ return 0
9059
9160
9261if __name__ == "__main__" :
0 commit comments