Skip to content

Commit d449bb0

Browse files
committed
shellcheck_run_steps: just call shellcheck once per yaml
We can pass all run steps to one instance of shellcheck. This should be faster. Signed-off-by: dann frazier <dann.frazier@chainguard.dev>
1 parent 584d6fd commit d449bb0

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

pre_commit_hooks/shellcheck_run_steps.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import argparse
4+
import contextlib
45
import os
56
import subprocess
67
import tempfile
@@ -48,16 +49,31 @@ def do_shellcheck(
4849
if "test" in pkg.keys():
4950
test_pipeline = pkg["test"].get("pipeline", [])
5051
pipelines.extend(test_pipeline)
51-
52-
for step in pipelines:
53-
if "runs" not in step.keys():
54-
continue
55-
with tempfile.NamedTemporaryFile(mode="w", dir=os.getcwd()) as shfile:
56-
shfile.write(step["runs"])
57-
subprocess.check_call(
58-
shellcheck
59-
+ ["--shell=busybox", "--", os.path.basename(shfile.name)],
52+
all_run_files = []
53+
with contextlib.ExitStack() as stack:
54+
for step in pipelines:
55+
if "runs" not in step.keys():
56+
continue
57+
all_run_files.extend(
58+
[
59+
stack.enter_context(
60+
tempfile.NamedTemporaryFile(
61+
mode="w",
62+
dir=os.getcwd(),
63+
delete_on_close=False,
64+
),
65+
),
66+
],
6067
)
68+
for shfile in all_run_files:
69+
shfile.write(step["runs"])
70+
shfile.close()
71+
subprocess.check_call(
72+
["/usr/bin/shellcheck"]
73+
+ ["--shell=busybox", "--"]
74+
+ [os.path.basename(f.name) for f in all_run_files],
75+
cwd=os.getcwd(),
76+
)
6177

6278

6379
def main(argv: Sequence[str] | None = None) -> int:

0 commit comments

Comments
 (0)