Added script for checking & fixing BOMs in files.
authorMarcin Rogucki <marcin.rogucki@mobica.com>
Thu, 28 Sep 2017 16:01:18 +0000 (18:01 +0200)
committerAlexander Galazin <Alexander.Galazin@arm.com>
Mon, 2 Oct 2017 12:31:17 +0000 (08:31 -0400)
Affects:
doc/testspecs/GLES2/functional.rasterization.txt
framework/platform/win32/tcuWin32VulkanPlatform.cpp
modules/gles31/functional/es31fNegativeVertexArrayApiTests.cpp
scripts/src_util/check_all.py

Adds:
scripts/src_util/check_boms.py

Change-Id: I17dc821e44a30a119e22ff42afbdca775ecec046
Components: Vulkan
(cherry picked from commit 69731671eed80fd6915375f22d88c0404a6accb6)

doc/testspecs/GLES2/functional.rasterization.txt
framework/platform/win32/tcuWin32VulkanPlatform.cpp
modules/gles31/functional/es31fNegativeVertexArrayApiTests.cpp
scripts/src_util/check_all.py
scripts/src_util/check_boms.py [new file with mode: 0644]

index afa1703..96fb4bb 100644 (file)
@@ -1,4 +1,4 @@
--------------------------------------------------------------------------
+-------------------------------------------------------------------------
 drawElements Quality Program Test Specification
 -----------------------------------------------
 
index 98fd928..3b6cdaa 100644 (file)
@@ -1,4 +1,4 @@
-/*-------------------------------------------------------------------------
+/*-------------------------------------------------------------------------
  * drawElements Quality Program Tester Core
  * ----------------------------------------
  *
index 1ed5f2d..72e0f00 100644 (file)
@@ -1,4 +1,4 @@
-/*-------------------------------------------------------------------------
+/*-------------------------------------------------------------------------
  * drawElements Quality Program OpenGL ES 3.1 Module
  * -------------------------------------------------
  *
index b8378a5..507348f 100644 (file)
 #
 #-------------------------------------------------------------------------
 
-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"
diff --git a/scripts/src_util/check_boms.py b/scripts/src_util/check_boms.py
new file mode 100644 (file)
index 0000000..55846d9
--- /dev/null
@@ -0,0 +1,86 @@
+# -*- 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