Added script for checking & fixing BOMs in files.
[platform/upstream/VK-GL-CTS.git] / scripts / src_util / check_boms.py
1 # -*- coding: utf-8 -*-
2
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
6 #
7 # Copyright (c) 2017 The Khronos Group Inc.
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #-------------------------------------------------------------------------
22
23 import os
24 import sys
25 import codecs
26 from optparse import OptionParser
27
28 FILE_PATTERNS           = ["*.hpp", "*.h", "*.cpp", "*.py"]
29 IGNORE_FILES            = set()
30 CHECK_END_COMMENT       = True
31
32 def hasBOM (file):
33         with open(file, 'rb') as f:
34                 line0 = f.readline()
35                 if line0.startswith(codecs.BOM_UTF8):
36                         return True
37         return False
38
39 def removeBOM (file):
40         with open(file, 'r+b') as f:
41                 chunk = f.read(1024)
42                 if chunk.startswith(codecs.BOM_UTF8):
43                         chunk = chunk[3:]
44                 else:
45                         return
46                 readpos = 1024;
47                 writepos = 0;
48                 while chunk:
49                         f.seek(writepos, os.SEEK_SET)
50                         f.write(chunk)
51                         writepos += len(chunk)
52                         f.seek(readpos, os.SEEK_SET)
53                         chunk = f.read(1024)
54                         readpos += len(chunk)
55                 f.truncate(readpos-3)
56
57 def getFileList (path):
58         if os.path.isfile(path):
59                 yield path
60         elif os.path.isdir(path):
61                 for root, dirs, files in os.walk(path):
62                         for file in files:
63                                 yield os.path.join(root, file)
64
65 def checkBOMs (files, fix):
66     correct = True
67     for file in files:
68         if hasBOM(file):
69                         if fix:
70                                 removeBOM(file)
71                                 print "File %s contained BOM and was fixed" % file
72                         else:
73                                 correct = False
74                                 print "File %s contains BOM" % file
75     return correct
76
77 if __name__ == "__main__":
78         parser = OptionParser()
79         parser.add_option("-x", "--fix", action="store_true", dest="fix", default=False, help="attempt to fix BOMs")
80
81         (options, args) = parser.parse_args()
82         fix                             = options.fix
83
84         print "Checking BOMs..."
85         for dir in args:
86                 checkBOMs(getFileList(os.path.normpath(dir)), fix)