2020-06-29 22:29:30 -06:00
|
|
|
#!/usr/bin/env python3
|
2015-10-24 00:00:54 -06:00
|
|
|
import os
|
2015-11-04 06:23:45 -07:00
|
|
|
from os.path import basename, dirname, join, splitext
|
2015-10-24 00:00:54 -06:00
|
|
|
import sys
|
2015-09-22 23:25:41 -06:00
|
|
|
|
2016-07-26 20:51:42 -06:00
|
|
|
SCRIPT_PATH = sys.argv[1] if len(sys.argv) > 1 else 'scripts'
|
2020-06-30 12:03:05 -06:00
|
|
|
IS_GITHUB_ACTIONS = bool(os.environ.get('GITHUB_ACTIONS'))
|
2015-11-04 06:23:45 -07:00
|
|
|
|
|
|
|
def expected_cmd(path):
|
|
|
|
"""Get the command from the name of a script."""
|
|
|
|
dname, fname = basename(dirname(path)), splitext(basename(path))[0]
|
|
|
|
if dname in ('devel', 'fix', 'gui', 'modtools'):
|
|
|
|
return dname + '/' + fname
|
|
|
|
return fname
|
2015-10-24 00:00:54 -06:00
|
|
|
|
|
|
|
|
2015-11-09 19:20:12 -07:00
|
|
|
def check_ls(fname, line):
|
|
|
|
"""Check length & existence of leading comment for "ls" builtin command."""
|
|
|
|
line = line.strip()
|
|
|
|
comment = '--' if fname.endswith('.lua') else '#'
|
2016-06-14 21:22:04 -06:00
|
|
|
if '[====[' in line or not line.startswith(comment):
|
2020-06-30 12:03:05 -06:00
|
|
|
print_error('missing leading comment (requred for `ls`)', fname)
|
2015-11-09 19:20:12 -07:00
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2020-06-30 12:03:05 -06:00
|
|
|
def print_error(message, filename, line=None):
|
|
|
|
if not isinstance(line, int):
|
|
|
|
line = 1
|
|
|
|
print('Error: %s:%i: %s' % (filename, line, message))
|
|
|
|
if IS_GITHUB_ACTIONS:
|
|
|
|
print('::error file=%s,line=%i::%s' % (filename, line, message))
|
|
|
|
|
|
|
|
|
2015-10-24 00:00:54 -06:00
|
|
|
def check_file(fname):
|
2015-11-04 06:23:45 -07:00
|
|
|
errors, doclines = 0, []
|
2016-06-14 21:22:04 -06:00
|
|
|
tok1, tok2 = ('=begin', '=end') if fname.endswith('.rb') else \
|
|
|
|
('[====[', ']====]')
|
2020-06-30 12:03:05 -06:00
|
|
|
doc_start_line = None
|
2015-10-24 00:00:54 -06:00
|
|
|
with open(fname, errors='ignore') as f:
|
2015-11-09 19:20:12 -07:00
|
|
|
lines = f.readlines()
|
2020-06-30 12:03:05 -06:00
|
|
|
if not lines:
|
|
|
|
print_error('empty file', fname)
|
|
|
|
return 1
|
2015-11-09 19:20:12 -07:00
|
|
|
errors += check_ls(fname, lines[0])
|
2020-06-30 12:03:05 -06:00
|
|
|
for i, l in enumerate(lines):
|
2016-06-14 21:22:04 -06:00
|
|
|
if doclines or l.strip().endswith(tok1):
|
2020-06-30 12:03:05 -06:00
|
|
|
if not doclines:
|
|
|
|
doc_start_line = i + 1
|
2015-10-24 00:00:54 -06:00
|
|
|
doclines.append(l.rstrip())
|
2016-06-14 21:22:04 -06:00
|
|
|
if l.startswith(tok2):
|
2015-10-24 00:00:54 -06:00
|
|
|
break
|
|
|
|
else:
|
2015-10-27 17:07:02 -06:00
|
|
|
if doclines:
|
2020-06-30 12:03:05 -06:00
|
|
|
print_error('docs start but do not end', fname, doc_start_line)
|
2015-10-27 17:07:02 -06:00
|
|
|
else:
|
2020-06-30 12:03:05 -06:00
|
|
|
print_error('no documentation found', fname)
|
2015-10-24 00:00:54 -06:00
|
|
|
return 1
|
2016-08-11 09:57:42 -06:00
|
|
|
|
|
|
|
if not doclines:
|
2020-06-30 12:03:05 -06:00
|
|
|
print_error('missing or malformed documentation', fname)
|
2016-08-11 09:57:42 -06:00
|
|
|
return 1
|
|
|
|
|
2016-06-14 21:22:04 -06:00
|
|
|
title, underline = [d for d in doclines
|
|
|
|
if d and '=begin' not in d and '[====[' not in d][:2]
|
2020-06-30 12:03:05 -06:00
|
|
|
title_line = doc_start_line + doclines.index(title)
|
|
|
|
expected_underline = '=' * len(title)
|
|
|
|
if underline != expected_underline:
|
|
|
|
print_error('title/underline mismatch: expected {!r}, got {!r}'.format(
|
|
|
|
expected_underline, underline),
|
|
|
|
fname, title_line + 1)
|
2015-11-04 06:23:45 -07:00
|
|
|
errors += 1
|
|
|
|
if title != expected_cmd(fname):
|
2020-06-30 12:03:05 -06:00
|
|
|
print_error('expected script title {!r}, got {!r}'.format(
|
|
|
|
expected_cmd(fname), title),
|
|
|
|
fname, title_line)
|
2015-11-04 06:23:45 -07:00
|
|
|
errors += 1
|
|
|
|
return errors
|
2015-09-22 23:25:41 -06:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2016-07-26 20:51:42 -06:00
|
|
|
"""Check that all DFHack scripts include documentation"""
|
2015-11-04 06:23:45 -07:00
|
|
|
err = 0
|
2022-05-10 23:06:47 -06:00
|
|
|
exclude = {'.git', 'internal', 'test'}
|
2020-07-25 00:40:27 -06:00
|
|
|
for root, dirs, files in os.walk(SCRIPT_PATH, topdown=True):
|
|
|
|
dirs[:] = [d for d in dirs if d not in exclude]
|
2015-11-04 06:23:45 -07:00
|
|
|
for f in files:
|
2022-05-10 23:06:47 -06:00
|
|
|
if f.split('.')[-1] in {'rb', 'lua'}:
|
2015-11-04 06:23:45 -07:00
|
|
|
err += check_file(join(root, f))
|
|
|
|
return err
|
2015-10-24 00:00:54 -06:00
|
|
|
|
2015-09-22 23:25:41 -06:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-07-26 20:51:42 -06:00
|
|
|
sys.exit(min(100, main()))
|