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
|
|
|
|
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 '#'
|
|
|
|
if line.endswith('=begin') or not line.startswith(comment):
|
|
|
|
print('Error: no leading comment in ' + fname)
|
|
|
|
return 1
|
|
|
|
if len(line.replace(comment, '').strip()) > 53:
|
|
|
|
print('Error: leading comment too long 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, []
|
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:
|
2015-10-24 00:00:54 -06:00
|
|
|
if doclines or l.strip().endswith('=begin'):
|
|
|
|
doclines.append(l.rstrip())
|
|
|
|
if l.startswith('=end'):
|
|
|
|
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-04-15 00:51:24 -06:00
|
|
|
title, underline = [d for d in doclines if d and '=begin' 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():
|
2015-10-24 00:00:54 -06:00
|
|
|
"""Check that all DFHack scripts include documentation (not 3rdparty)"""
|
2015-11-04 06:23:45 -07:00
|
|
|
err = 0
|
|
|
|
for root, _, files in os.walk('scripts'):
|
|
|
|
for f in files:
|
|
|
|
# TODO: remove 3rdparty exemptions from checks
|
|
|
|
# Requires reading their CMakeLists to only apply to used scripts
|
|
|
|
if f[-3:] in {'.rb', 'lua'} and '3rdparty' not in root:
|
|
|
|
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__':
|
2015-10-24 00:00:54 -06:00
|
|
|
sys.exit(bool(main()))
|