common.py revision 3c77ed4e119083afaec64a173bfdcf024c271635
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 shlex
25import subprocess
26
27SRC_BASE_DIR		= os.path.realpath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..", "..")))
28DEQP_DIR			= os.path.join(SRC_BASE_DIR, "deqp")
29
30def die (msg):
31	print msg
32	exit(-1)
33
34def shellquote(s):
35	return '"%s"' % s.replace('\\', '\\\\').replace('"', '\"').replace('$', '\$').replace('`', '\`')
36
37g_workDirStack = []
38
39def pushWorkingDir (path):
40	oldDir = os.getcwd()
41	os.chdir(path)
42	g_workDirStack.append(oldDir)
43
44def popWorkingDir ():
45	assert len(g_workDirStack) > 0
46	newDir = g_workDirStack[-1]
47	g_workDirStack.pop()
48	os.chdir(newDir)
49
50def execute (args):
51	retcode	= subprocess.call(args)
52	if retcode != 0:
53		raise Exception("Failed to execute '%s', got %d" % (str(args), retcode))
54
55def readFile (filename):
56	f = open(filename, 'rb')
57	data = f.read()
58	f.close()
59	return data
60
61def writeFile (filename, data):
62	f = open(filename, 'wb')
63	f.write(data)
64	f.close()
65
66def which (binName):
67	for path in os.environ['PATH'].split(os.pathsep):
68		path = path.strip('"')
69		fullPath = os.path.join(path, binName)
70		if os.path.isfile(fullPath) and os.access(fullPath, os.X_OK):
71			return fullPath
72
73	return None
74