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