Check for tabs and fix more line number issues

develop
lethosor 2015-02-14 22:07:57 -05:00
parent f91a5767cd
commit 7b29538485
1 changed files with 39 additions and 26 deletions

@ -36,19 +36,23 @@ class Linter(object):
if not len(lines): if not len(lines):
# should never happen # should never happen
return 'nowhere' return 'nowhere'
s = 'lines ' if len(lines) != 1 else 'line ' if len(lines) == 1:
return 'line %i' % lines[0]
s = 'lines '
range_start = range_end = lines[0] range_start = range_end = lines[0]
for i, line in enumerate(lines): for i, line in enumerate(lines):
if line > range_start + 1 or i == len(lines) - 1: if line > range_end + 1:
if i == len(lines) - 1:
range_end = line
if range_start == range_end: if range_start == range_end:
s += ('%i, ' % range_end) s += ('%i, ' % range_end)
else: else:
s += ('%i-%i, ' % (range_start, range_end)) s += ('%i-%i, ' % (range_start, range_end))
range_start = range_end = line range_start = range_end = line
if i == len(lines) - 1:
s += ('%i' % line)
else: else:
range_end = line range_end = line
if i == len(lines) - 1:
s += ('%i-%i, ' % (range_start, range_end))
return s.rstrip(' ').rstrip(',') return s.rstrip(' ').rstrip(',')
class NewlineLinter(Linter): class NewlineLinter(Linter):
@ -62,28 +66,37 @@ class TrailingWhitespaceLinter(Linter):
line = line.replace('\r', '') line = line.replace('\r', '')
return not line.endswith(' ') and not line.endswith('\t') return not line.endswith(' ') and not line.endswith('\t')
linters = [NewlineLinter(), TrailingWhitespaceLinter()] class TabLinter(Linter):
msg = 'Contains tabs'
def check_line(self, line):
return '\t' not in line
linters = [NewlineLinter(), TrailingWhitespaceLinter(), TabLinter()]
def main():
root_path = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else '.')
path_blacklist.append(root_path + '/.git')
path_blacklist.append(root_path + '/build')
root_path = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else '.') for cur, dirnames, filenames in os.walk(root_path):
path_blacklist.append(root_path + '/.git') for filename in filenames:
path_blacklist.append(root_path + '/build') full_path = os.path.join(cur, filename)
rel_path = full_path.replace(root_path, '.')
if not valid_file(full_path):
continue
with open(full_path, 'rb') as f:
lines = f.read().split('\n')
for linter in linters:
try:
linter.check(lines)
except LinterError as e:
error('%s: %s' % (rel_path, e))
for cur, dirnames, filenames in os.walk(root_path): if success:
for filename in filenames: print('All linters completed successfully')
full_path = os.path.join(cur, filename) sys.exit(0)
rel_path = full_path.replace(root_path, '.') else:
if not valid_file(full_path): sys.exit(1)
continue
with open(full_path, 'rb') as f:
lines = f.read().split('\n')
for linter in linters:
try:
linter.check(lines)
except LinterError as e:
error('%s: %s' % (rel_path, e))
if success: if __name__ == '__main__':
print('All linters completed successfully') main()
sys.exit(0)
else:
sys.exit(1)