• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    ".h264",
59    ".h265",
60    ".mp4",
61	".diff"
62    ]
63
64def isTextFile (filePath):
65    # Special case for a preprocessor test file that uses a non-ascii/utf8 encoding
66    if filePath.endswith("preprocessor.test"):
67        return False
68
69    ext = os.path.splitext(filePath)[1]
70    if ext in TEXT_FILE_EXTENSION:
71        return True
72    if ext in BINARY_FILE_EXTENSION:
73        return False
74
75    # Analyze file contents, zero byte is the marker for a binary file
76    f = open(filePath, "rb")
77
78    TEST_LIMIT = 1024
79    nullFound = False
80    numBytesTested = 0
81
82    byte = f.read(1)
83    while byte and numBytesTested < TEST_LIMIT:
84        if byte == "\0":
85            nullFound = True
86            break
87
88        byte = f.read(1)
89        numBytesTested += 1
90
91    f.close()
92    return not nullFound
93
94def getProjectPath ():
95    # File system hierarchy is fixed
96    scriptDir = os.path.dirname(os.path.abspath(__file__))
97    projectDir = os.path.normpath(os.path.join(scriptDir, "../.."))
98    return projectDir
99
100def git (*args):
101    process = subprocess.Popen(['git'] + list(args), cwd=getProjectPath(), stdout=subprocess.PIPE)
102    output = process.communicate()[0]
103    if process.returncode != 0:
104        raise Exception("Failed to execute '%s', got %d" % (str(args), process.returncode))
105    return output
106
107def getAbsolutePathPathFromProjectRelativePath (projectRelativePath):
108    return os.path.normpath(os.path.join(getProjectPath(), projectRelativePath))
109
110def getChangedFiles ():
111    # Added, Copied, Moved, Renamed
112    output = git('diff', '--cached', '--name-only', '-z', '--diff-filter=ACMR')
113    relativePaths = output.split('\0')[:-1] # remove trailing ''
114    return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths]
115
116def getAllProjectFiles ():
117    output = git('ls-files', '--cached', '-z').decode()
118    relativePaths = output.split('\0')[:-1] # remove trailing ''
119    return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths]
120