#!/usr/bin/env python
#coding=utf-8

'''
Remove tailing whitespaces and ensures one and only one empty ending line.
'''

import os, re

def scan(*dirs, **kwargs):
  files = []
  extensions = kwargs['extensions'] if kwargs.has_key('extensions') else None
  excludes = kwargs['excludes'] if kwargs.has_key('excludes') else []
  for top in dirs:
    for root, dirnames, filenames in os.walk(top):
      dirnames = [i for i in dirnames if i in excludes]
      for f in filenames:
        if f in excludes:
          continue
        ext = os.path.splitext(f)[1].lower()
        if extensions is None or ext in extensions:
          files.append(os.path.join(root, f))
  return files

def fixone(src):
  lines = open(src, 'r').readlines()

  trimed = []
  for line in lines:
    trimed.append(re.sub('\s+$', '', line))

  while len(trimed) > 1 and not trimed[-1]:
    trimed.pop()
  trimed.append('')

  with open(src, 'w') as f:
    for line in trimed:
      f.write('%s\n' % line)


def lint(root):
  print('Checking tailing whitespaces in: %s' % root)
  dirs = [
    os.path.join(root, 'cocos'),
    os.path.join(root, 'extensions'),
    os.path.join(root, 'templates'),
    os.path.join(root, 'tests'),
    os.path.join(root, 'tools', 'simulator')
  ]

  files = scan(*dirs, extensions=['.c', '.cpp', '.h', '.hpp', '.m', '.mm', '.java'])
  for f in files:
    print(f)
    fixone(f)


def main():
  default_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
  lint(default_root)

main()

[((15, 37, 15, 49), 'os.walk', 'os.walk', ({(15, 45, 15, 48): 'top'}, {}), '(top)', False, 'import os, re\n'), ((44, 4, 44, 31), 'os.path.join', 'os.path.join', ({(44, 17, 44, 21): 'root', (44, 23, 44, 30): '"""cocos"""'}, {}), "(root, 'cocos')", False, 'import os, re\n'), ((45, 4, 45, 36), 'os.path.join', 'os.path.join', ({(45, 17, 45, 21): 'root', (45, 23, 45, 35): '"""extensions"""'}, {}), "(root, 'extensions')", False, 'import os, re\n'), ((46, 4, 46, 35), 'os.path.join', 'os.path.join', ({(46, 17, 46, 21): 'root', (46, 23, 46, 34): '"""templates"""'}, {}), "(root, 'templates')", False, 'import os, re\n'), ((47, 4, 47, 31), 'os.path.join', 'os.path.join', ({(47, 17, 47, 21): 'root', (47, 23, 47, 30): '"""tests"""'}, {}), "(root, 'tests')", False, 'import os, re\n'), ((48, 4, 48, 44), 'os.path.join', 'os.path.join', ({(48, 17, 48, 21): 'root', (48, 23, 48, 30): '"""tools"""', (48, 32, 48, 43): '"""simulator"""'}, {}), "(root, 'tools', 'simulator')", False, 'import os, re\n'), ((30, 18, 30, 42), 're.sub', 're.sub', ({(30, 25, 30, 31): '"""\\\\s+$"""', (30, 33, 30, 35): '""""""', (30, 37, 30, 41): 'line'}, {}), "('\\\\s+$', '', line)", False, 'import os, re\n'), ((58, 46, 58, 71), 'os.path.dirname', 'os.path.dirname', ({(58, 62, 58, 70): '__file__'}, {}), '(__file__)', False, 'import os, re\n'), ((22, 23, 22, 44), 'os.path.join', 'os.path.join', ({(22, 36, 22, 40): 'root', (22, 42, 22, 43): 'f'}, {}), '(root, f)', False, 'import os, re\n'), ((20, 14, 20, 33), 'os.path.splitext', 'os.path.splitext', ({(20, 31, 20, 32): 'f'}, {}), '(f)', False, 'import os, re\n')]