blob: ec98a11ce61e82670b1788a16a08f3b0c6f9e57e [file] [log] [blame]
[email protected]0a88a652012-03-09 00:34:451#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Sets environment variables needed to run a chromium unit test."""
7
8import os
[email protected]76361be452012-08-30 22:48:149import stat
[email protected]0a88a652012-03-09 00:34:4510import subprocess
11import sys
12
13# This is hardcoded to be src/ relative to this script.
14ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
15
[email protected]76361be452012-08-30 22:48:1416CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
17CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
18
19
20def should_enable_sandbox(sandbox_path):
21 """Return a boolean indicating that the current slave is capable of using the
22 sandbox and should enable it. This should return True iff the slave is a
23 Linux host with the sandbox file present and configured correctly."""
24 if not (sys.platform.startswith('linux') and
25 os.path.exists(sandbox_path)):
26 return False
27 sandbox_stat = os.stat(sandbox_path)
28 if ((sandbox_stat.st_mode & stat.S_ISUID) and
29 (sandbox_stat.st_mode & stat.S_IRUSR) and
30 (sandbox_stat.st_mode & stat.S_IXUSR) and
31 (sandbox_stat.st_uid == 0)):
32 return True
33 return False
34
35
36def enable_sandbox_if_required(env, verbose=False):
37 """Checks enables the sandbox if it is required, otherwise it disables it."""
38 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
39
40 if should_enable_sandbox(chrome_sandbox_path):
41 if verbose:
42 print 'Enabling sandbox. Setting environment variable:'
43 print ' %s="%s"' % (CHROME_SANDBOX_ENV, chrome_sandbox_path)
44 env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
45 else:
46 if verbose:
[email protected]0d264892013-06-25 17:03:0647 print 'Sandbox not properly installed. Unsetting:'
48 print ' %s' % CHROME_SANDBOX_ENV
49 # The variable should be removed from the environment, making
50 # the variable empty silently disables the sandbox.
[email protected]f6e3214c2013-06-25 19:34:3551 if env.get(CHROME_SANDBOX_ENV):
52 env.pop(CHROME_SANDBOX_ENV)
[email protected]76361be452012-08-30 22:48:1453
[email protected]0a88a652012-03-09 00:34:4554
[email protected]4bf4d632012-05-31 15:50:3055def fix_python_path(cmd):
56 """Returns the fixed command line to call the right python executable."""
57 out = cmd[:]
58 if out[0] == 'python':
59 out[0] = sys.executable
60 elif out[0].endswith('.py'):
61 out.insert(0, sys.executable)
62 return out
63
64
[email protected]0a88a652012-03-09 00:34:4565def run_executable(cmd, env):
66 """Runs an executable with:
[email protected]3766ed1c2012-07-26 20:53:5667 - environment variable CR_SOURCE_ROOT set to the root directory.
[email protected]0a88a652012-03-09 00:34:4568 - environment variable LANGUAGE to en_US.UTF-8.
[email protected]76361be452012-08-30 22:48:1469 - environment variable CHROME_DEVEL_SANDBOX set if need
[email protected]0a88a652012-03-09 00:34:4570 - Reuses sys.executable automatically.
71 """
72 # Many tests assume a English interface...
[email protected]331578db2012-08-14 16:56:5073 env['LANG'] = 'en_US.UTF-8'
[email protected]3766ed1c2012-07-26 20:53:5674 # Used by base/base_paths_linux.cc as an override. Just make sure the default
75 # logic is used.
76 env.pop('CR_SOURCE_ROOT', None)
[email protected]76361be452012-08-30 22:48:1477 enable_sandbox_if_required(env)
[email protected]8ba98352012-05-23 20:43:5978 # Ensure paths are correctly separated on windows.
79 cmd[0] = cmd[0].replace('/', os.path.sep)
[email protected]4bf4d632012-05-31 15:50:3080 cmd = fix_python_path(cmd)
[email protected]50ec9f232012-03-16 04:18:2381 try:
82 return subprocess.call(cmd, env=env)
83 except OSError:
84 print >> sys.stderr, 'Failed to start %s' % cmd
85 raise
[email protected]0a88a652012-03-09 00:34:4586
87
88def main():
89 return run_executable(sys.argv[1:], os.environ.copy())
90
91
[email protected]ed763a72012-08-29 03:51:2292if __name__ == '__main__':
[email protected]0a88a652012-03-09 00:34:4593 sys.exit(main())