1# -*- coding: utf-8 -*-
2
3import os
4import shlex
5import subprocess
6
7SRC_BASE_DIR		= os.path.realpath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..", "..")))
8DEQP_DIR			= os.path.join(SRC_BASE_DIR, "deqp")
9
10def die (msg):
11	print msg
12	exit(-1)
13
14def shellquote(s):
15	return '"%s"' % s.replace('\\', '\\\\').replace('"', '\"').replace('$', '\$').replace('`', '\`')
16
17g_workDirStack = []
18
19def pushWorkingDir (path):
20	oldDir = os.getcwd()
21	os.chdir(path)
22	g_workDirStack.append(oldDir)
23
24def popWorkingDir ():
25	assert len(g_workDirStack) > 0
26	newDir = g_workDirStack[-1]
27	g_workDirStack.pop()
28	os.chdir(newDir)
29
30def execute (args):
31	retcode	= subprocess.call(args)
32	if retcode != 0:
33		raise Exception("Failed to execute '%s', got %d" % (str(args), retcode))
34
35def readFile (filename):
36	f = open(filename, 'rb')
37	data = f.read()
38	f.close()
39	return data
40
41def writeFile (filename, data):
42	f = open(filename, 'wb')
43	f.write(data)
44	f.close()
45
46def which (binName):
47	for path in os.environ['PATH'].split(os.pathsep):
48		path = path.strip('"')
49		fullPath = os.path.join(path, binName)
50		if os.path.isfile(fullPath) and os.access(fullPath, os.X_OK):
51			return fullPath
52
53	return None
54