2015-02-14 17:59:17 -07:00
|
|
|
import re, os, sys
|
|
|
|
|
|
|
|
valid_extensions = ['c', 'cpp', 'h', 'hpp', 'mm', 'lua', 'rb', 'proto',
|
2015-03-04 15:33:12 -07:00
|
|
|
'init', 'init-example', 'rst']
|
2015-02-14 17:59:17 -07:00
|
|
|
path_blacklist = [
|
2015-02-23 16:44:19 -07:00
|
|
|
'^library/include/df/',
|
|
|
|
'^plugins/stonesense/allegro',
|
|
|
|
'^plugins/isoworld/allegro',
|
|
|
|
'^plugins/isoworld/agui',
|
|
|
|
'^depends/',
|
|
|
|
'^.git/',
|
|
|
|
'^build',
|
|
|
|
'.pb.h',
|
2015-02-14 17:59:17 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
def valid_file(filename):
|
|
|
|
return len(filter(lambda ext: filename.endswith('.' + ext), valid_extensions)) and \
|
|
|
|
not len(filter(lambda path: path.replace('\\', '/') in filename.replace('\\', '/'), path_blacklist))
|
|
|
|
|
|
|
|
success = True
|
|
|
|
def error(msg):
|
|
|
|
global success
|
|
|
|
success = False
|
|
|
|
sys.stderr.write(msg + '\n')
|
|
|
|
|
|
|
|
class LinterError(Exception): pass
|
|
|
|
|
|
|
|
class Linter(object):
|
|
|
|
def check(self, lines):
|
|
|
|
failures = []
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if not self.check_line(line):
|
2015-02-14 19:43:10 -07:00
|
|
|
failures.append(i + 1)
|
2015-02-14 17:59:17 -07:00
|
|
|
if len(failures):
|
|
|
|
raise LinterError('%s: %s' % (self.msg, self.display_lines(failures, len(lines))))
|
|
|
|
|
2015-02-14 20:25:10 -07:00
|
|
|
def fix(self, lines):
|
|
|
|
for i in range(len(lines)):
|
|
|
|
lines[i] = self.fix_line(lines[i])
|
|
|
|
|
2015-02-14 17:59:17 -07:00
|
|
|
def display_lines(self, lines, total):
|
|
|
|
if len(lines) == total - 1:
|
|
|
|
return 'entire file'
|
|
|
|
if not len(lines):
|
|
|
|
# should never happen
|
|
|
|
return 'nowhere'
|
2015-02-14 20:07:57 -07:00
|
|
|
if len(lines) == 1:
|
|
|
|
return 'line %i' % lines[0]
|
|
|
|
s = 'lines '
|
2015-02-14 17:59:17 -07:00
|
|
|
range_start = range_end = lines[0]
|
|
|
|
for i, line in enumerate(lines):
|
2015-02-14 20:07:57 -07:00
|
|
|
if line > range_end + 1:
|
2015-02-14 17:59:17 -07:00
|
|
|
if range_start == range_end:
|
|
|
|
s += ('%i, ' % range_end)
|
|
|
|
else:
|
|
|
|
s += ('%i-%i, ' % (range_start, range_end))
|
|
|
|
range_start = range_end = line
|
2015-02-14 20:07:57 -07:00
|
|
|
if i == len(lines) - 1:
|
|
|
|
s += ('%i' % line)
|
2015-02-14 17:59:17 -07:00
|
|
|
else:
|
|
|
|
range_end = line
|
2015-02-14 20:07:57 -07:00
|
|
|
if i == len(lines) - 1:
|
|
|
|
s += ('%i-%i, ' % (range_start, range_end))
|
2015-02-14 17:59:17 -07:00
|
|
|
return s.rstrip(' ').rstrip(',')
|
|
|
|
|
|
|
|
class NewlineLinter(Linter):
|
|
|
|
msg = 'Contains DOS-style newlines'
|
|
|
|
def check_line(self, line):
|
|
|
|
return '\r' not in line
|
2015-02-14 20:25:10 -07:00
|
|
|
def fix_line(self, line):
|
|
|
|
return line.replace('\r', '')
|
2015-02-14 17:59:17 -07:00
|
|
|
|
|
|
|
class TrailingWhitespaceLinter(Linter):
|
|
|
|
msg = 'Contains trailing whitespace'
|
|
|
|
def check_line(self, line):
|
|
|
|
line = line.replace('\r', '')
|
|
|
|
return not line.endswith(' ') and not line.endswith('\t')
|
2015-02-14 20:25:10 -07:00
|
|
|
def fix_line(self, line):
|
|
|
|
return line.rstrip('\t ')
|
2015-02-14 17:59:17 -07:00
|
|
|
|
2015-02-14 20:07:57 -07:00
|
|
|
class TabLinter(Linter):
|
|
|
|
msg = 'Contains tabs'
|
|
|
|
def check_line(self, line):
|
|
|
|
return '\t' not in line
|
2015-02-14 20:25:10 -07:00
|
|
|
def fix_line(self, line):
|
|
|
|
return line.replace('\t', ' ')
|
2015-02-14 20:07:57 -07:00
|
|
|
|
|
|
|
linters = [NewlineLinter(), TrailingWhitespaceLinter(), TabLinter()]
|
|
|
|
|
|
|
|
def main():
|
|
|
|
root_path = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else '.')
|
2015-02-21 21:06:50 -07:00
|
|
|
if not os.path.exists(root_path):
|
|
|
|
print('Nonexistent path: %s' % root_path)
|
|
|
|
sys.exit(2)
|
2015-02-14 20:25:10 -07:00
|
|
|
fix = (len(sys.argv) > 2 and sys.argv[2] == '--fix')
|
2015-02-14 20:13:10 -07:00
|
|
|
global path_blacklist
|
2015-02-23 16:44:19 -07:00
|
|
|
path_blacklist = map(lambda s: os.path.join(root_path, s.replace('^', '')) if s.startswith('^') else s, path_blacklist)
|
2015-02-14 17:59:17 -07:00
|
|
|
|
2015-02-14 20:07:57 -07:00
|
|
|
for cur, dirnames, filenames in os.walk(root_path):
|
|
|
|
for filename in filenames:
|
|
|
|
full_path = os.path.join(cur, filename)
|
|
|
|
rel_path = full_path.replace(root_path, '.')
|
|
|
|
if not valid_file(full_path):
|
|
|
|
continue
|
2015-02-14 20:25:10 -07:00
|
|
|
lines = []
|
2015-02-14 20:07:57 -07:00
|
|
|
with open(full_path, 'rb') as f:
|
|
|
|
lines = f.read().split('\n')
|
2015-02-14 20:25:10 -07:00
|
|
|
for linter in linters:
|
|
|
|
try:
|
|
|
|
linter.check(lines)
|
|
|
|
except LinterError as e:
|
|
|
|
error('%s: %s' % (rel_path, e))
|
|
|
|
if fix:
|
|
|
|
linter.fix(lines)
|
|
|
|
contents = '\n'.join(lines)
|
|
|
|
with open(full_path, 'wb') as f:
|
|
|
|
f.write(contents)
|
2015-02-14 17:59:17 -07:00
|
|
|
|
2015-02-14 20:07:57 -07:00
|
|
|
if success:
|
|
|
|
print('All linters completed successfully')
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
2015-02-14 17:59:17 -07:00
|
|
|
|
2015-02-14 20:07:57 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|