forked from coreos/fedora-coreos-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
executable file
·70 lines (64 loc) · 2.42 KB
/
check.py
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
#!/usr/bin/python3
#
# Find all Butane configs in the doc tree, use the podman Butane container
# to run them through butane --strict, and fail on any errors.
#
# A Butane config looks like this:
#
# [source,yaml]
# ----
# variant:[...]
# ----
#
# If variant: is missing, we print a warning but continue, since there
# might be [source,yaml] documents that aren't Butane configs.
import argparse
import os
import re
import subprocess
import sys
import textwrap
ERR = '\x1b[1;31m'
WARN = '\x1b[1;33m'
RESET = '\x1b[0m'
container = os.getenv('BUTANE_CONTAINER', 'quay.io/coreos/butane:release')
matcher = re.compile(r'^\[source,\s*yaml\]\n----\n(.+?\n)----$',
re.MULTILINE | re.DOTALL)
parser = argparse.ArgumentParser(description='Run validations on docs.')
parser.add_argument('-v', '--verbose', action='store_true',
help='log all detected Butane configs')
args = parser.parse_args()
def handle_error(e):
raise e
ret = 0
for dirpath, dirnames, filenames in os.walk('.', onerror=handle_error):
dirnames.sort() # walk in sorted order
for filename in sorted(filenames):
filepath = os.path.join(dirpath, filename)
if not filename.endswith('.adoc'):
continue
with open(filepath) as fh:
filedata = fh.read()
# Iterate over YAML source blocks
for match in matcher.finditer(filedata):
bu = match.group(1)
buline = filedata.count('\n', 0, match.start(1)) + 1
if not bu.startswith('variant:'):
print(f'{WARN}Ignoring non-Butane YAML at {filepath}:{buline}{RESET}')
continue
if args.verbose:
print(f'Checking Butane config at {filepath}:{buline}')
result = subprocess.run(
['podman', 'run', '--rm', '-i', container, '--strict'],
universal_newlines=True, # can be spelled "text" on >= 3.7
input=bu,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE)
if result.returncode != 0:
formatted = textwrap.indent(result.stderr.strip(), ' ')
# Not necessary for ANSI terminals, but required by GitHub's
# log renderer
formatted = ERR + formatted.replace('\n', '\n' + ERR)
print(f'{ERR}Invalid Butane config at {filepath}:{buline}:\n{formatted}{RESET}')
ret = 1
sys.exit(ret)