1# -*- coding: utf-8 -*- 2 3#------------------------------------------------------------------------- 4# drawElements Quality Program utilities 5# -------------------------------------- 6# 7# Copyright 2015 The Android Open Source Project 8# 9# Licensed under the Apache License, Version 2.0 (the "License"); 10# you may not use this file except in compliance with the License. 11# You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, software 16# distributed under the License is distributed on an "AS IS" BASIS, 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18# See the License for the specific language governing permissions and 19# limitations under the License. 20# 21#------------------------------------------------------------------------- 22 23import os 24import subprocess 25 26TEXT_FILE_EXTENSION = [ 27 ".bat", 28 ".c", 29 ".cfg", 30 ".cmake", 31 ".cpp", 32 ".css", 33 ".h", 34 ".hh", 35 ".hpp", 36 ".html", 37 ".inl", 38 ".java", 39 ".js", 40 ".m", 41 ".mk", 42 ".mm", 43 ".py", 44 ".rule", 45 ".sh", 46 ".test", 47 ".txt", 48 ".xml", 49 ".xsl", 50 ] 51 52BINARY_FILE_EXTENSION = [ 53 ".bin", 54 ".png", 55 ".pkm", 56 ".xcf", 57 ".nspv", 58 ] 59 60def isTextFile (filePath): 61 # Special case for a preprocessor test file that uses a non-ascii/utf8 encoding 62 if filePath.endswith("preprocessor.test"): 63 return False 64 65 ext = os.path.splitext(filePath)[1] 66 if ext in TEXT_FILE_EXTENSION: 67 return True 68 if ext in BINARY_FILE_EXTENSION: 69 return False 70 71 # Analyze file contents, zero byte is the marker for a binary file 72 f = open(filePath, "rb") 73 74 TEST_LIMIT = 1024 75 nullFound = False 76 numBytesTested = 0 77 78 byte = f.read(1) 79 while byte and numBytesTested < TEST_LIMIT: 80 if byte == "\0": 81 nullFound = True 82 break 83 84 byte = f.read(1) 85 numBytesTested += 1 86 87 f.close() 88 return not nullFound 89 90def getProjectPath (): 91 # File system hierarchy is fixed 92 scriptDir = os.path.dirname(os.path.abspath(__file__)) 93 projectDir = os.path.normpath(os.path.join(scriptDir, "../..")) 94 return projectDir 95 96def git (*args): 97 process = subprocess.Popen(['git'] + list(args), cwd=getProjectPath(), stdout=subprocess.PIPE) 98 output = process.communicate()[0] 99 if process.returncode != 0: 100 raise Exception("Failed to execute '%s', got %d" % (str(args), process.returncode)) 101 return output 102 103def getAbsolutePathPathFromProjectRelativePath (projectRelativePath): 104 return os.path.normpath(os.path.join(getProjectPath(), projectRelativePath)) 105 106def getChangedFiles (): 107 # Added, Copied, Moved, Renamed 108 output = git('diff', '--cached', '--name-only', '-z', '--diff-filter=ACMR') 109 relativePaths = output.split('\0')[:-1] # remove trailing '' 110 return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths] 111 112def getAllProjectFiles (): 113 output = git('ls-files', '--cached', '-z').decode() 114 relativePaths = output.split('\0')[:-1] # remove trailing '' 115 return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths] 116