summaryrefslogtreecommitdiff
path: root/check.py
blob: 6db82d64c0bd6bc00b250c5495dc6fe9d3cbb1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Search for lines >= 80 chars or with trailing whitespace."""

import os
import sys


def main():
    args = sys.argv[1:] or os.curdir
    for arg in args:
        if os.path.isdir(arg):
            for dn, dirs, files in os.walk(arg):
                for fn in sorted(files):
                    if fn.endswith('.py'):
                        process(os.path.join(dn, fn))
                dirs[:] = [d for d in dirs if d[0] != '.']
                dirs.sort()
        else:
            process(arg)


def isascii(x):
    try:
        x.encode('ascii')
        return True
    except UnicodeError:
        return False


def process(fn):
    try:
        f = open(fn)
    except IOError as err:
        print(err)
        return
    try:
        for i, line in enumerate(f):
            line = line.rstrip('\n')
            sline = line.rstrip()
            if len(line) >= 80 or line != sline or not isascii(line):
                print('{}:{:d}:{}{}'.format(
                    fn, i+1, sline, '_' * (len(line) - len(sline))))
    finally:
        f.close()

main()