1e5c31af7Sopenharmony_ci# -*- coding: utf-8 -*- 2e5c31af7Sopenharmony_ci 3e5c31af7Sopenharmony_ci#------------------------------------------------------------------------- 4e5c31af7Sopenharmony_ci# drawElements Quality Program utilities 5e5c31af7Sopenharmony_ci# -------------------------------------- 6e5c31af7Sopenharmony_ci# 7e5c31af7Sopenharmony_ci# Copyright 2015 The Android Open Source Project 8e5c31af7Sopenharmony_ci# 9e5c31af7Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 10e5c31af7Sopenharmony_ci# you may not use this file except in compliance with the License. 11e5c31af7Sopenharmony_ci# You may obtain a copy of the License at 12e5c31af7Sopenharmony_ci# 13e5c31af7Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 14e5c31af7Sopenharmony_ci# 15e5c31af7Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 16e5c31af7Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 17e5c31af7Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18e5c31af7Sopenharmony_ci# See the License for the specific language governing permissions and 19e5c31af7Sopenharmony_ci# limitations under the License. 20e5c31af7Sopenharmony_ci# 21e5c31af7Sopenharmony_ci#------------------------------------------------------------------------- 22e5c31af7Sopenharmony_ci 23e5c31af7Sopenharmony_ciimport sys 24e5c31af7Sopenharmony_cifrom argparse import ArgumentParser 25e5c31af7Sopenharmony_cifrom common import getChangedFiles, getAllProjectFiles, isTextFile 26e5c31af7Sopenharmony_ci 27e5c31af7Sopenharmony_cidef checkFileWhitespace (file): 28e5c31af7Sopenharmony_ci if (sys.version_info < (3, 0)): 29e5c31af7Sopenharmony_ci f = open(file, 'rt') 30e5c31af7Sopenharmony_ci else: 31e5c31af7Sopenharmony_ci f = open(file, 'rt', encoding="ascii", errors='replace', newline='') 32e5c31af7Sopenharmony_ci error = False 33e5c31af7Sopenharmony_ci for lineNum, line in enumerate(f): 34e5c31af7Sopenharmony_ci if line.endswith(" \n") or line.endswith("\t\n"): 35e5c31af7Sopenharmony_ci error = True 36e5c31af7Sopenharmony_ci print("%s:%i trailing whitespace" % (file, lineNum+1)) 37e5c31af7Sopenharmony_ci if " \t" in line: 38e5c31af7Sopenharmony_ci error = True 39e5c31af7Sopenharmony_ci print("%s:%i merged <space><tab>" % (file, lineNum+1)) 40e5c31af7Sopenharmony_ci if line.endswith("\r") or line.endswith("\r\n"): 41e5c31af7Sopenharmony_ci error = True 42e5c31af7Sopenharmony_ci print("%s:%i incorrect line ending" % (file, lineNum+1)) 43e5c31af7Sopenharmony_ci f.close() 44e5c31af7Sopenharmony_ci 45e5c31af7Sopenharmony_ci return not error 46e5c31af7Sopenharmony_ci 47e5c31af7Sopenharmony_cidef checkWhitespace (files): 48e5c31af7Sopenharmony_ci error = False 49e5c31af7Sopenharmony_ci for file in files: 50e5c31af7Sopenharmony_ci if isTextFile(file): 51e5c31af7Sopenharmony_ci if not checkFileWhitespace(file): 52e5c31af7Sopenharmony_ci error = True 53e5c31af7Sopenharmony_ci 54e5c31af7Sopenharmony_ci return not error 55e5c31af7Sopenharmony_ci 56e5c31af7Sopenharmony_ciif __name__ == "__main__": 57e5c31af7Sopenharmony_ci parser = ArgumentParser() 58e5c31af7Sopenharmony_ci parser.add_argument("-e", "--only-errors", action="store_true", dest="onlyErrors", default=False, help="Print only on error") 59e5c31af7Sopenharmony_ci parser.add_argument("-i", "--only-changed", action="store_true", dest="useGitIndex", default=False, help="Check only modified files. Uses git.") 60e5c31af7Sopenharmony_ci 61e5c31af7Sopenharmony_ci args = parser.parse_args() 62e5c31af7Sopenharmony_ci 63e5c31af7Sopenharmony_ci if args.useGitIndex: 64e5c31af7Sopenharmony_ci files = getChangedFiles() 65e5c31af7Sopenharmony_ci else: 66e5c31af7Sopenharmony_ci files = getAllProjectFiles() 67e5c31af7Sopenharmony_ci 68e5c31af7Sopenharmony_ci error = not checkWhitespace(files) 69e5c31af7Sopenharmony_ci 70e5c31af7Sopenharmony_ci if error: 71e5c31af7Sopenharmony_ci print("One or more checks failed") 72e5c31af7Sopenharmony_ci sys.exit(1) 73e5c31af7Sopenharmony_ci if not args.onlyErrors: 74e5c31af7Sopenharmony_ci print("All checks passed") 75