1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015 The Android Open Source Project
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 #-------------------------------------------------------------------------
26 TEXT_FILE_EXTENSION = [
52 BINARY_FILE_EXTENSION = [
58 def isTextFile (filePath):
59 ext = os.path.splitext(filePath)[1]
60 if ext in TEXT_FILE_EXTENSION:
62 if ext in BINARY_FILE_EXTENSION:
65 # Analyze file contents, zero byte is the marker for a binary file
66 f = open(filePath, "rb")
73 while byte and numBytesTested < TEST_LIMIT:
84 def getProjectPath ():
85 # File system hierarchy is fixed
86 scriptDir = os.path.dirname(os.path.abspath(__file__))
87 projectDir = os.path.normpath(os.path.join(scriptDir, "../.."))
91 process = subprocess.Popen(['git'] + list(args), cwd=getProjectPath(), stdout=subprocess.PIPE)
92 output = process.communicate()[0]
93 if process.returncode != 0:
94 raise Exception("Failed to execute '%s', got %d" % (str(args), process.returncode))
97 def getAbsolutePathPathFromProjectRelativePath (projectRelativePath):
98 return os.path.normpath(os.path.join(getProjectPath(), projectRelativePath))
100 def getChangedFiles ():
101 # Added, Copied, Moved, Renamed
102 output = git('diff', '--cached', '--name-only', '-z', '--diff-filter=ACMR')
103 relativePaths = output.split('\0')[:-1] # remove trailing ''
104 return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths]
106 def getAllProjectFiles ():
107 output = git('ls-files', '--cached', '-z')
108 relativePaths = output.split('\0')[:-1] # remove trailing ''
109 return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths]