-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathshellcheck_run_steps.py
More file actions
108 lines (96 loc) · 2.98 KB
/
shellcheck_run_steps.py
File metadata and controls
108 lines (96 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import annotations
import argparse
import contextlib
import os
import subprocess
import tempfile
from collections.abc import Mapping
from collections.abc import Sequence
from typing import Any
import ruamel.yaml
yaml = ruamel.yaml.YAML(typ="safe")
def do_shellcheck(
melange_cfg: Mapping[str, Any],
shellcheck: list[str],
) -> None:
if melange_cfg == {}:
return
pkgs = [melange_cfg]
pkgs.extend(melange_cfg.get("subpackages", []))
pipelines: list[Mapping[str, Any]] = []
for pkg in pkgs:
pipelines.extend(pkg.get("pipeline", []))
if "test" in pkg.keys():
test_pipeline = pkg["test"].get("pipeline", [])
pipelines.extend(test_pipeline)
name = melange_cfg["package"]["name"]
all_run_files = []
with contextlib.ExitStack() as stack:
for step in pipelines:
if "runs" not in step.keys():
continue
all_run_files.extend(
[
stack.enter_context(
tempfile.NamedTemporaryFile(
mode="w",
prefix=name,
dir=os.getcwd(),
delete_on_close=False,
),
),
],
)
for shfile in all_run_files:
shfile.write(step["runs"])
shfile.close()
subprocess.check_call(
["/usr/bin/shellcheck"]
+ ["--shell=busybox", "--"]
+ [os.path.basename(f.name) for f in all_run_files],
cwd=os.getcwd(),
)
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
parser.add_argument(
"--shellcheck",
default=[
"docker",
"run",
f"--volume={os.getcwd()}:/mnt",
"--rm",
"-it",
"koalaman/shellcheck:latest",
],
nargs="*",
help="shellcheck command",
)
args = parser.parse_args(argv)
melange_cfg = {}
for filename in args.filenames:
with tempfile.NamedTemporaryFile(
"w",
delete_on_close=False,
) as compiled_out:
subprocess.check_call(
[
"melange",
"compile",
"--arch=x86_64",
"--pipeline-dir=./pipelines",
filename,
],
stdout=compiled_out,
)
compiled_out.close()
try:
with open(compiled_out.name) as compiled_in:
melange_cfg = yaml.load(compiled_in)
do_shellcheck(melange_cfg, args.shellcheck)
except ruamel.yaml.YAMLError as exc:
print(exc)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())