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    ]
57
58def isTextFile (filePath):
59    ext = os.path.splitext(filePath)[1]
60    if ext in TEXT_FILE_EXTENSION:
61        return True
62    if ext in BINARY_FILE_EXTENSION:
63        return False
64
65    # Analyze file contents, zero byte is the marker for a binary file
66    f = open(filePath, "rb")
67
68    TEST_LIMIT = 1024
69    nullFound = False
70    numBytesTested = 0
71
72    byte = f.read(1)
73    while byte and numBytesTested < TEST_LIMIT:
74        if byte == "\0":
75            nullFound = True
76            break
77
78        byte = f.read(1)
79        numBytesTested += 1
80
81    f.close()
82    return not nullFound
83
84def 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, "../.."))
88    return projectDir
89
90def git (*args):
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))
95    return output
96
97def getAbsolutePathPathFromProjectRelativePath (projectRelativePath):
98    return os.path.normpath(os.path.join(getProjectPath(), projectRelativePath))
99
100def 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]
105
106def getAllProjectFiles ():
107    output = git('ls-files', '--cached', '-z')
108    relativePaths = output.split('\0')[:-1] # remove trailing ''
109    return [getAbsolutePathPathFromProjectRelativePath(path) for path in relativePaths]
110