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