summaryrefslogtreecommitdiff
path: root/longlines.py
blob: f0aa9a669cf58aa966e63693e48b030a5d431acb (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
"""Search for lines > 80 chars or with trailing whitespace."""

import sys, os

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('%s:%d:%s%s' % (fn, i+1, sline, '_' * (len(line) - len(sline))))
  finally:
    f.close()

main()