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