xref: /aosp_15_r20/external/deqp/scripts/ctsbuild/common.py (revision 35238bce31c2a825756842865a792f8cf7f89930)
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 sys
25import shlex
26import platform
27import subprocess
28import logging
29
30DEQP_DIR = os.path.realpath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..")))
31
32# HostInfo describes properties of the host where these scripts
33# are running on.
34class HostInfo:
35    OS_WINDOWS = 0
36    OS_LINUX = 1
37    OS_OSX = 2
38
39    @staticmethod
40    def getOs ():
41        if sys.platform == 'darwin':
42            return HostInfo.OS_OSX
43        elif sys.platform == 'win32':
44            return HostInfo.OS_WINDOWS
45        elif sys.platform.startswith('linux'):
46            return HostInfo.OS_LINUX
47        else:
48            raise Exception("Unknown sys.platform '%s'" % sys.platform)
49
50    @staticmethod
51    def getArchBits ():
52        MACHINE_BITS = {
53            "i386": 32,
54            "i686": 32,
55            "x86": 32,
56            "x86_64": 64,
57            "AMD64": 64,
58            "arm64": 64
59        }
60        machine = platform.machine()
61
62        if not machine in MACHINE_BITS:
63            raise Exception("Unknown platform.machine() '%s'" % machine)
64
65        return MACHINE_BITS[machine]
66
67def die (msg):
68    print(msg)
69    exit(-1)
70
71def shellquote(s):
72    return '"%s"' % s.replace('\\', '\\\\').replace('"', '\"').replace('$', '\$').replace('`', '\`')
73
74g_workDirStack = []
75
76def pushWorkingDir (path):
77    oldDir = os.getcwd()
78    os.chdir(path)
79    logging.debug("Current working directory: " + path)
80    g_workDirStack.append(oldDir)
81
82def popWorkingDir ():
83    assert len(g_workDirStack) > 0
84    newDir = g_workDirStack[-1]
85    g_workDirStack.pop()
86    os.chdir(newDir)
87    logging.debug("Current working directory: " + newDir)
88
89def execute (args):
90    logging.debug("Running: " + " ".join(args))
91    retcode = subprocess.call(args)
92    if retcode != 0:
93        raise Exception("Failed to execute '%s', got %d" % (str(args), retcode))
94
95def readBinaryFile (filename):
96    f = open(filename, 'rb')
97    data = f.read()
98    f.close()
99    return data
100
101def readFile (filename):
102    f = open(filename, 'rt')
103    data = f.read()
104    f.close()
105    return data
106
107def writeBinaryFile (filename, data):
108    logging.debug("Writing to binary file: " + filename)
109    f = open(filename, 'wb')
110    f.write(data)
111    f.close()
112
113def writeFile (filename, data):
114    logging.debug("Writing to file: " + filename)
115    if (sys.version_info < (3, 0)):
116        f = open(filename, 'wt')
117    else:
118        f = open(filename, 'wt', newline='\n')
119    f.write(data)
120    f.close()
121
122def which (binName, paths = None):
123    if paths == None:
124        paths = os.environ['PATH'].split(os.pathsep)
125
126    def whichImpl (binWithExt):
127        for path in paths:
128            path = path.strip('"')
129            fullPath = os.path.join(path, binWithExt)
130            if os.path.isfile(fullPath) and os.access(fullPath, os.X_OK):
131                return fullPath
132
133        return None
134
135    extensions = [""]
136    if HostInfo.getOs() == HostInfo.OS_WINDOWS:
137        extensions += [".exe", ".bat"]
138
139    for extension in extensions:
140        extResult = whichImpl(binName + extension)
141        if extResult != None:
142            logging.debug("which: found " + binName + " as " + extResult)
143            return extResult
144
145    return None
146
147def initializeLogger(verbose):
148    log_level = logging.INFO
149    if verbose:
150        log_level = logging.DEBUG
151    logging.root.setLevel(level=log_level)
152    logging.debug("Logging is set to " + str(log_level))
153