#
#-------------------------------------------------------------------------
-import sys
-from argparse import ArgumentParser
-from common import getChangedFiles, getAllProjectFiles
-from check_include_guards import checkIncludeGuards
-from check_whitespace import checkWhitespace
-from check_license import checkLicense
-from check_invalid_types import checkInvalidTypes
+import sys
+from argparse import ArgumentParser
+from common import getChangedFiles, getAllProjectFiles
+from check_include_guards import checkIncludeGuards
+from check_whitespace import checkWhitespace
+from check_license import checkLicense
+from check_boms import checkBOMs
if __name__ == "__main__":
- parser = ArgumentParser()
- parser.add_argument("-e", "--only-errors", action="store_true", dest="onlyErrors", default=False, help="Print only on error")
- parser.add_argument("-i", "--only-changed", action="store_true", dest="useGitIndex", default=False, help="Check only modified files. Uses git.")
+ parser = ArgumentParser()
+ parser.add_argument("-e", "--only-errors", action="store_true", dest="onlyErrors", default=False, help="Print only on error")
+ parser.add_argument("-i", "--only-changed", action="store_true", dest="useGitIndex", default=False, help="Check only modified files. Uses git.")
+ parser.add_argument("-b", "--fix-bom", action="store_true", dest="fixBOMs", default=False, help="Attempt to fix BOMs")
- args = parser.parse_args()
+ args = parser.parse_args()
- if args.useGitIndex:
- files = getChangedFiles()
- else:
- files = getAllProjectFiles()
+ if args.useGitIndex:
+ files = getChangedFiles()
+ else:
+ files = getAllProjectFiles()
- error = not all([
- checkWhitespace(files),
- checkIncludeGuards(files),
- checkLicense(files),
- checkInvalidTypes(files),
- #todo checkRedundantIncludeGuards(files),
- ])
+ error = not all([
+ checkBOMs(files, args.fixBOMs),
+ checkWhitespace(files),
+ checkIncludeGuards(files),
+ checkLicense(files),
+ #todo checkRedundantIncludeGuards(files),
+ ])
- if error:
- print "One or more checks failed"
- sys.exit(1)
- if not args.onlyErrors:
- print "All checks passed"
+ if error:
+ print "One or more checks failed"
+ sys.exit(1)
+ if not args.onlyErrors:
+ print "All checks passed"
--- /dev/null
+# -*- coding: utf-8 -*-
+
+#-------------------------------------------------------------------------
+# drawElements Quality Program utilities
+# --------------------------------------
+#
+# Copyright (c) 2017 The Khronos Group Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#-------------------------------------------------------------------------
+
+import os
+import sys
+import codecs
+from optparse import OptionParser
+
+FILE_PATTERNS = ["*.hpp", "*.h", "*.cpp", "*.py"]
+IGNORE_FILES = set()
+CHECK_END_COMMENT = True
+
+def hasBOM (file):
+ with open(file, 'rb') as f:
+ line0 = f.readline()
+ if line0.startswith(codecs.BOM_UTF8):
+ return True
+ return False
+
+def removeBOM (file):
+ with open(file, 'r+b') as f:
+ chunk = f.read(1024)
+ if chunk.startswith(codecs.BOM_UTF8):
+ chunk = chunk[3:]
+ else:
+ return
+ readpos = 1024;
+ writepos = 0;
+ while chunk:
+ f.seek(writepos, os.SEEK_SET)
+ f.write(chunk)
+ writepos += len(chunk)
+ f.seek(readpos, os.SEEK_SET)
+ chunk = f.read(1024)
+ readpos += len(chunk)
+ f.truncate(readpos-3)
+
+def getFileList (path):
+ if os.path.isfile(path):
+ yield path
+ elif os.path.isdir(path):
+ for root, dirs, files in os.walk(path):
+ for file in files:
+ yield os.path.join(root, file)
+
+def checkBOMs (files, fix):
+ correct = True
+ for file in files:
+ if hasBOM(file):
+ if fix:
+ removeBOM(file)
+ print "File %s contained BOM and was fixed" % file
+ else:
+ correct = False
+ print "File %s contains BOM" % file
+ return correct
+
+if __name__ == "__main__":
+ parser = OptionParser()
+ parser.add_option("-x", "--fix", action="store_true", dest="fix", default=False, help="attempt to fix BOMs")
+
+ (options, args) = parser.parse_args()
+ fix = options.fix
+
+ print "Checking BOMs..."
+ for dir in args:
+ checkBOMs(getFileList(os.path.normpath(dir)), fix)
\ No newline at end of file