2015-12-28 14:31:36 -07:00
|
|
|
from __future__ import print_function
|
2015-10-24 00:00:54 -06:00
|
|
|
from io import open
|
|
|
|
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'
|
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):
|
2015-11-09 19:20:12 -07:00
|
|
|
print('Error: no leading comment in ' + fname)
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
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 \
|
|
|
|
('[====[', ']====]')
|
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()
|
|
|
|
errors += check_ls(fname, lines[0])
|
|
|
|
for l in lines:
|
2016-06-14 21:22:04 -06:00
|
|
|
if doclines or l.strip().endswith(tok1):
|
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:
|
|
|
|
print('Error: docs start but not end: ' + fname)
|
|
|
|
else:
|
|
|
|
print('Error: no documentation in: ' + fname)
|
2015-10-24 00:00:54 -06:00
|
|
|
return 1
|
2016-08-11 09:57:42 -06:00
|
|
|
|
|
|
|
if not doclines:
|
|
|
|
print('Error: missing or malformed documentation in: ' + fname)
|
|
|
|
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]
|
2015-10-24 00:00:54 -06:00
|
|
|
if underline != '=' * len(title):
|
|
|
|
print('Error: title/underline mismatch:', fname, title, underline)
|
2015-11-04 06:23:45 -07:00
|
|
|
errors += 1
|
|
|
|
if title != expected_cmd(fname):
|
|
|
|
print('Warning: expected script title {}, got {}'.format(
|
|
|
|
expected_cmd(fname), title))
|
|
|
|
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
|
2016-07-26 20:51:42 -06:00
|
|
|
for root, _, files in os.walk(SCRIPT_PATH):
|
2015-11-04 06:23:45 -07:00
|
|
|
for f in files:
|
2016-06-14 21:22:04 -06:00
|
|
|
if f[-3:] 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()))
|