From 34eb1839c9bd3a6f87032c4f3cae2f95bb773d60 Mon Sep 17 00:00:00 2001 From: Marcin Rogucki Date: Thu, 28 Sep 2017 18:01:18 +0200 Subject: [PATCH] Added script for checking & fixing BOMs in files. 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 | 2 +- .../platform/win32/tcuWin32VulkanPlatform.cpp | 2 +- .../es31fNegativeVertexArrayApiTests.cpp | 2 +- scripts/src_util/check_all.py | 55 +++++++------- scripts/src_util/check_boms.py | 86 ++++++++++++++++++++++ 5 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 scripts/src_util/check_boms.py diff --git a/doc/testspecs/GLES2/functional.rasterization.txt b/doc/testspecs/GLES2/functional.rasterization.txt index afa1703..96fb4bb 100644 --- a/doc/testspecs/GLES2/functional.rasterization.txt +++ b/doc/testspecs/GLES2/functional.rasterization.txt @@ -1,4 +1,4 @@ -------------------------------------------------------------------------- +------------------------------------------------------------------------- drawElements Quality Program Test Specification ----------------------------------------------- diff --git a/framework/platform/win32/tcuWin32VulkanPlatform.cpp b/framework/platform/win32/tcuWin32VulkanPlatform.cpp index 98fd928..3b6cdaa 100644 --- a/framework/platform/win32/tcuWin32VulkanPlatform.cpp +++ b/framework/platform/win32/tcuWin32VulkanPlatform.cpp @@ -1,4 +1,4 @@ -/*------------------------------------------------------------------------- +/*------------------------------------------------------------------------- * drawElements Quality Program Tester Core * ---------------------------------------- * diff --git a/modules/gles31/functional/es31fNegativeVertexArrayApiTests.cpp b/modules/gles31/functional/es31fNegativeVertexArrayApiTests.cpp index 1ed5f2d..72e0f00 100644 --- a/modules/gles31/functional/es31fNegativeVertexArrayApiTests.cpp +++ b/modules/gles31/functional/es31fNegativeVertexArrayApiTests.cpp @@ -1,4 +1,4 @@ -/*------------------------------------------------------------------------- +/*------------------------------------------------------------------------- * drawElements Quality Program OpenGL ES 3.1 Module * ------------------------------------------------- * diff --git a/scripts/src_util/check_all.py b/scripts/src_util/check_all.py index b8378a5..507348f 100644 --- a/scripts/src_util/check_all.py +++ b/scripts/src_util/check_all.py @@ -20,36 +20,37 @@ # #------------------------------------------------------------------------- -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 index 0000000..55846d9 --- /dev/null +++ b/scripts/src_util/check_boms.py @@ -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 -- 2.7.4