Blame view

ios/cocos2d/tools/coding-style/tailing-spaces.py 1.48 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #!/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()