2022-08-17 09:35:52 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# Validate basic syntax of shell script and yaml.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
validated=0
|
|
|
|
|
|
|
|
def openat(dirfd, name, mode='r'):
|
|
|
|
def opener(path, flags):
|
|
|
|
return os.open(path, flags, dir_fd=dirfd)
|
|
|
|
return open(name, mode, opener=opener)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_shell(rootfd, name):
|
|
|
|
subprocess.check_call(['bash', '-n', name], preexec_fn=lambda: os.fchdir(rootfd))
|
|
|
|
global validated
|
|
|
|
validated +=1
|
|
|
|
|
|
|
|
|
|
|
|
for root, dirs, files, rootfd in os.fwalk('.'):
|
2022-08-19 18:48:20 +00:00
|
|
|
# Skip .git, repo, cache, tmp, logs, fedora-comps
|
2024-03-01 18:51:34 +00:00
|
|
|
for d in ['.git', 'repo', 'cache', 'tmp', 'logs', 'fedora-comps']:
|
2022-08-19 18:48:20 +00:00
|
|
|
if d in dirs:
|
|
|
|
dirs.remove(d)
|
2022-08-17 09:35:52 +00:00
|
|
|
for name in files:
|
|
|
|
if name.endswith(('.yaml', '.yml')):
|
|
|
|
print("Validating:", name)
|
|
|
|
with open(os.open(name, dir_fd=rootfd, flags=os.O_RDONLY)) as f:
|
|
|
|
yaml.safe_load(f)
|
2024-03-01 18:51:34 +00:00
|
|
|
result = subprocess.run(['grep', '-RniEv', '^( )*[a-z#/-]|^$|^#', name], encoding='UTF-8',
|
|
|
|
preexec_fn=lambda: os.fchdir(rootfd))
|
2023-02-16 10:48:24 +00:00
|
|
|
if result.returncode == 0:
|
|
|
|
raise Exception("Found likely invalid indentation in YAML file: {}".format(name))
|
|
|
|
validated +=1
|
2022-08-17 09:35:52 +00:00
|
|
|
continue
|
|
|
|
elif name.endswith('.sh'):
|
|
|
|
print("Validating:", name)
|
|
|
|
validate_shell(rootfd, name)
|
|
|
|
continue
|
|
|
|
stbuf = os.lstat(name, dir_fd=rootfd)
|
|
|
|
if not stat.S_ISREG(stbuf.st_mode):
|
|
|
|
continue
|
|
|
|
if not stbuf.st_mode & stat.S_IXUSR:
|
|
|
|
continue
|
|
|
|
mimetype = subprocess.check_output(['file', '-b', '--mime-type', name], encoding='UTF-8',
|
2024-03-01 18:51:34 +00:00
|
|
|
preexec_fn=lambda: os.fchdir(rootfd)).strip()
|
2022-08-17 09:35:52 +00:00
|
|
|
if mimetype == 'text/x-shellscript':
|
|
|
|
print("Validating:", name)
|
|
|
|
validate_shell(rootfd, name)
|
|
|
|
|
|
|
|
print(f"Validated {validated} files")
|