-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_lint.py
More file actions
executable file
·367 lines (324 loc) · 12.4 KB
/
Copy pathdocker_lint.py
File metadata and controls
executable file
·367 lines (324 loc) · 12.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""Run the fleet Docker linters with bounded, observable execution."""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from collections.abc import Callable, Sequence
from dataclasses import dataclass
from pathlib import Path
PSSCRIPTANALYZER_VERSION = "1.23.0"
PSSCRIPTANALYZER_VOLUME = f"projecttemplate-psscriptanalyzer-{PSSCRIPTANALYZER_VERSION}"
MAX_FILE_ARGUMENT_BYTES = 16 * 1024
@dataclass(frozen=True)
class Linter:
"""Describe one lint container and its tracked-file targets."""
name: str
image: str
patterns: tuple[str, ...]
workdir: str
arguments: tuple[str, ...] = ()
LINTERS = (
Linter("editorconfig-checker", "mstruebing/editorconfig-checker:latest", (), "/check"),
Linter(
"actionlint",
"rhysd/actionlint:latest",
(".github/workflows/*.yml", ".github/workflows/*.yaml"),
"/repo",
arguments=("-color",),
),
Linter(
"markdownlint",
"davidanson/markdownlint-cli2:latest",
("*.md",),
"/workdir",
),
Linter(
"cspell",
"ghcr.io/streetsidesoftware/cspell:latest",
("README.md", "HISTORY.md"),
"/workdir",
arguments=("--no-progress",),
),
Linter("shellcheck", "koalaman/shellcheck:stable", ("*.sh",), "/mnt"),
Linter("PSScriptAnalyzer", "mcr.microsoft.com/powershell:latest", ("*.ps1",), "/mnt"),
)
class CommandFailed(RuntimeError):
"""Report a Docker command that failed or exceeded its bound."""
class CommandTimedOut(CommandFailed):
"""Report a Docker command that exceeded its bound."""
def run_command(
command: Sequence[str], timeout: int, *, capture_output: bool = False
) -> subprocess.CompletedProcess[str]:
"""Run one command and preserve its output for the caller."""
try:
return subprocess.run(
command, check=False, text=True, timeout=timeout, capture_output=capture_output
)
except subprocess.TimeoutExpired as error:
if command[:2] == ["docker", "run"] and "--name" in command:
name = command[command.index("--name") + 1]
try:
cleanup = subprocess.run(
["docker", "rm", "--force", name],
check=False,
timeout=min(timeout, 30),
)
except subprocess.TimeoutExpired as cleanup_error:
raise CommandTimedOut(
f"timed out after {timeout}s; cleanup timed out for {name}"
) from cleanup_error
except OSError as cleanup_error:
raise CommandTimedOut(
f"timed out after {timeout}s; cleanup could not start for {name}: "
f"{cleanup_error}"
) from cleanup_error
if cleanup.returncode != 0:
raise CommandTimedOut(
f"timed out after {timeout}s; cleanup failed for {name} "
f"(exit {cleanup.returncode})"
) from error
raise CommandTimedOut(f"timed out after {timeout}s") from error
except OSError as error:
raise CommandFailed(f"could not start command: {error}") from error
def tracked_files(root: Path, linter: Linter) -> list[str]:
"""Return the tracked files the linter can inspect."""
command = [
"git",
"-C",
str(root),
"ls-files",
"-z",
"--cached",
"--others",
"--exclude-standard",
]
if linter.patterns:
command.extend(["--", *linter.patterns])
try:
result = subprocess.run(command, check=True, capture_output=True)
except FileNotFoundError as error:
raise CommandFailed("target discovery failed: git was not found") from error
except subprocess.CalledProcessError as error:
detail = os.fsdecode(error.stderr).strip() if error.stderr else f"exit {error.returncode}"
raise CommandFailed(f"target discovery failed: {detail}") from error
except OSError as error:
raise CommandFailed(f"target discovery failed: {error}") from error
return [os.fsdecode(entry) for entry in result.stdout.split(b"\0") if entry]
def docker_mount(root: Path, destination: str) -> str:
"""Build the read-only repository mount argument."""
source = str(root).replace('"', '""')
return f'type=bind,"src={source}",dst={destination},readonly'
def file_batches(linter: Linter, files: Sequence[str]) -> list[list[str]]:
"""Split file arguments before the host command-line limit becomes relevant."""
if linter.name not in {"markdownlint", "cspell", "shellcheck", "PSScriptAnalyzer"}:
return [list(files)]
batches: list[list[str]] = []
batch: list[str] = []
batch_bytes = 0
for file in files:
multiplier = 2 if linter.name == "PSScriptAnalyzer" else 1
file_bytes = len(os.fsencode(file)) * multiplier + 4
if batch and batch_bytes + file_bytes > MAX_FILE_ARGUMENT_BYTES:
batches.append(batch)
batch = []
batch_bytes = 0
batch.append(file)
batch_bytes += file_bytes
if batch:
batches.append(batch)
return batches
def resolve_digest(
name: str,
image: str,
timeout: int,
runner: Callable[..., subprocess.CompletedProcess[str]],
) -> str:
"""Resolve the pulled image to its immutable repository digest."""
result = run_step(
f"inspect {name}",
["docker", "image", "inspect", "--format", "{{index .RepoDigests 0}}", image],
timeout,
runner,
capture_output=True,
)
digest = (result.stdout or "").strip()
if not digest:
raise CommandFailed("container failure (image has no repository digest)")
return digest
def run_step(
label: str,
command: Sequence[str],
timeout: int,
runner: Callable[..., subprocess.CompletedProcess[str]],
*,
capture_output: bool = False,
) -> subprocess.CompletedProcess[str]:
"""Run one visible bounded step and classify its result."""
print(f"START {label} (timeout {timeout}s)", flush=True)
try:
result = runner(command, timeout, capture_output=capture_output)
except CommandTimedOut as error:
print(f"TIMEOUT {label}: {error}", flush=True)
raise
except CommandFailed as error:
print(f"FAILED {label}: {error}", flush=True)
raise
if result.returncode != 0:
print(f"FAILED {label} (exit {result.returncode})", flush=True)
raise CommandFailed(f"container failure (exit {result.returncode})")
print(f"COMPLETE {label}", flush=True)
return result
def powershell_command(files: Sequence[str]) -> str:
"""Build the analyzer command after targets are known."""
quoted = ",".join("'" + file.replace("'", "''") + "'" for file in files)
return (
"Import-Module PSScriptAnalyzer; "
f"$files = @({quoted}); "
"$found = @(); "
"foreach ($file in $files) { $found += Invoke-ScriptAnalyzer -Path $file "
"-Settings ./PSScriptAnalyzerSettings.psd1 }; "
"if ($found) { $found | Format-Table RuleName,Severity,ScriptName,Line,Message "
"-AutoSize | Out-String -Width 200 | Write-Host; exit 1 }"
)
def container_command(root: Path, linter: Linter, digest: str, files: Sequence[str]) -> list[str]:
"""Build a network-disabled, read-only lint invocation."""
command = [
"docker",
"run",
"--rm",
"--name",
f"projecttemplate-lint-{linter.name.lower()}-{os.getpid()}",
"--network=none",
"--mount",
docker_mount(root, linter.workdir),
"--workdir",
linter.workdir,
]
if linter.name == "PSScriptAnalyzer":
command.extend(
[
"--mount",
f"source={PSSCRIPTANALYZER_VOLUME},target=/root/.local/share/powershell/Modules,readonly",
digest,
"pwsh",
"-NoProfile",
"-Command",
powershell_command(files),
]
)
else:
command.extend([digest, *linter.arguments])
if linter.name in {"markdownlint", "cspell", "shellcheck"}:
command.append("--")
command.extend(files)
return command
def install_psscriptanalyzer(
digest: str,
timeout: int,
runner: Callable[..., subprocess.CompletedProcess[str]],
) -> None:
"""Populate the pinned analyzer module without exposing the repository."""
run_step(
"PSScriptAnalyzer module setup",
["docker", "volume", "create", PSSCRIPTANALYZER_VOLUME],
timeout,
runner,
)
run_step(
"PSScriptAnalyzer module install",
[
"docker",
"run",
"--rm",
"--name",
f"projecttemplate-lint-psscriptanalyzer-setup-{os.getpid()}",
"--mount",
f"source={PSSCRIPTANALYZER_VOLUME},target=/root/.local/share/powershell/Modules",
digest,
"pwsh",
"-NoProfile",
"-Command",
(
"Set-PSRepository PSGallery -InstallationPolicy Trusted; "
f"Install-Module PSScriptAnalyzer -RequiredVersion {PSSCRIPTANALYZER_VERSION} "
"-Force -Scope CurrentUser"
),
],
timeout,
runner,
)
def lint(
root: Path,
timeout: int,
selected: set[str],
runner: Callable[..., subprocess.CompletedProcess[str]] = run_command,
) -> int:
"""Pull applicable images, then run every selected linter."""
try:
applicable: list[tuple[Linter, list[str]]] = []
for linter in LINTERS:
if linter.name not in selected:
continue
files = tracked_files(root, linter)
if not files:
print(f"SKIP {linter.name}: zero targets", flush=True)
continue
print(f"TARGETS {linter.name}: {len(files)} file(s)", flush=True)
applicable.append((linter, files))
if not applicable:
print("COMPLETE lint: zero applicable linters", flush=True)
return 0
print("PHASE pull", flush=True)
digests: dict[str, str] = {}
for linter, _ in applicable:
run_step(f"pull {linter.name}", ["docker", "pull", linter.image], timeout, runner)
digests[linter.name] = resolve_digest(linter.name, linter.image, timeout, runner)
print("PHASE execution: pulls complete, repository mounts begin", flush=True)
for linter, files in applicable:
if linter.name == "PSScriptAnalyzer":
install_psscriptanalyzer(digests[linter.name], timeout, runner)
batches = file_batches(linter, files)
for index, batch in enumerate(batches, start=1):
label = f"lint {linter.name} ({len(files)} file(s))"
if len(batches) > 1:
label = (
f"lint {linter.name} batch {index}/{len(batches)} "
f"({len(batch)} of {len(files)} file(s))"
)
run_step(
label,
container_command(root, linter, digests[linter.name], batch),
timeout,
runner,
)
except CommandFailed as error:
print(f"RESULT failed: {error}", flush=True)
return 1
print(f"RESULT success: {len(applicable)} linter(s) completed", flush=True)
return 0
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, default=Path.cwd(), help="repository root")
parser.add_argument(
"--timeout", type=int, default=300, help="seconds allowed per Docker command"
)
parser.add_argument(
"--linter",
action="append",
choices=[linter.name for linter in LINTERS],
help="linter to run, repeat to select multiple (default: all)",
)
return parser.parse_args(argv)
def main(argv: Sequence[str] | None = None) -> int:
"""Run the command-line entry point."""
args = parse_args(argv)
if args.timeout <= 0:
raise SystemExit("--timeout must be greater than zero")
root = args.root.resolve()
selected = set(args.linter or (linter.name for linter in LINTERS))
return lint(root, args.timeout, selected)
if __name__ == "__main__":
sys.exit(main())