1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright (c) 2017 The Khronos Group Inc.
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 #-------------------------------------------------------------------------
26 from optparse import OptionParser
28 FILE_PATTERNS = ["*.hpp", "*.h", "*.cpp", "*.py"]
30 CHECK_END_COMMENT = True
33 with open(file, 'rb') as f:
35 if line0.startswith(codecs.BOM_UTF8):
40 with open(file, 'r+b') as f:
42 if chunk.startswith(codecs.BOM_UTF8):
49 f.seek(writepos, os.SEEK_SET)
51 writepos += len(chunk)
52 f.seek(readpos, os.SEEK_SET)
57 def getFileList (path):
58 if os.path.isfile(path):
60 elif os.path.isdir(path):
61 for root, dirs, files in os.walk(path):
63 yield os.path.join(root, file)
65 def checkBOMs (files, fix):
71 print "File %s contained BOM and was fixed" % file
74 print "File %s contains BOM" % file
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")
81 (options, args) = parser.parse_args()
84 print "Checking BOMs..."
86 checkBOMs(getFileList(os.path.normpath(dir)), fix)