Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Sets environment variables needed to run a chromium unit test.""" | 6 """Sets environment variables needed to run a chromium unit test.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import stat | |
| 9 import subprocess | 10 import subprocess |
| 10 import sys | 11 import sys |
| 11 | 12 |
| 12 # This is hardcoded to be src/ relative to this script. | 13 # This is hardcoded to be src/ relative to this script. |
| 13 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 14 | 15 |
| 16 CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX' | |
| 17 CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox' | |
| 18 | |
| 19 | |
| 20 def 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 | |
| 36 def enable_sandbox_if_required(env): | |
| 37 """Checks enables the sandbox if it is required, otherwise it disables it.""" | |
| 38 if CHROME_SANDBOX_ENV in env: | |
| 39 chrome_sandbox_path = env[CHROME_SANDBOX_ENV] | |
| 40 else: | |
| 41 chrome_sandbox_path = CHROME_SANDBOX_PATH | |
|
M-A Ruel
2012/08/30 19:43:17
1 line:
chrome_sandbox_path = env.get(CHROME_SANDB
csharp
2012/08/30 20:17:48
Done.
| |
| 42 | |
| 43 if should_enable_sandbox(chrome_sandbox_path): | |
| 44 print 'Enabling sandbox. Setting environment variable:' | |
|
M-A Ruel
2012/08/30 19:43:17
I'd prefer no output except for errors. At worst,
csharp
2012/08/30 20:17:48
Hidden behind a verbose flag
| |
| 45 print ' %s="%s"' % (CHROME_SANDBOX_ENV, chrome_sandbox_path) | |
| 46 env[CHROME_SANDBOX_ENV] = chrome_sandbox_path | |
| 47 else: | |
| 48 print 'Disabling sandbox. Setting environment variable:' | |
| 49 print ' %s=""' % CHROME_SANDBOX_ENV | |
| 50 env[CHROME_SANDBOX_ENV] = '' | |
| 51 | |
| 15 | 52 |
| 16 def fix_python_path(cmd): | 53 def fix_python_path(cmd): |
| 17 """Returns the fixed command line to call the right python executable.""" | 54 """Returns the fixed command line to call the right python executable.""" |
| 18 out = cmd[:] | 55 out = cmd[:] |
| 19 if out[0] == 'python': | 56 if out[0] == 'python': |
| 20 out[0] = sys.executable | 57 out[0] = sys.executable |
| 21 elif out[0].endswith('.py'): | 58 elif out[0].endswith('.py'): |
| 22 out.insert(0, sys.executable) | 59 out.insert(0, sys.executable) |
| 23 return out | 60 return out |
| 24 | 61 |
| 25 | 62 |
| 26 def run_executable(cmd, env): | 63 def run_executable(cmd, env): |
| 27 """Runs an executable with: | 64 """Runs an executable with: |
| 28 - environment variable CR_SOURCE_ROOT set to the root directory. | 65 - environment variable CR_SOURCE_ROOT set to the root directory. |
| 29 - environment variable LANGUAGE to en_US.UTF-8. | 66 - environment variable LANGUAGE to en_US.UTF-8. |
| 67 - environment variable CHROME_DEVEL_SANDBOX set if need | |
| 30 - Reuses sys.executable automatically. | 68 - Reuses sys.executable automatically. |
| 31 """ | 69 """ |
| 32 # Many tests assume a English interface... | 70 # Many tests assume a English interface... |
| 33 env['LANG'] = 'en_US.UTF-8' | 71 env['LANG'] = 'en_US.UTF-8' |
| 34 # Used by base/base_paths_linux.cc as an override. Just make sure the default | 72 # Used by base/base_paths_linux.cc as an override. Just make sure the default |
| 35 # logic is used. | 73 # logic is used. |
| 36 env.pop('CR_SOURCE_ROOT', None) | 74 env.pop('CR_SOURCE_ROOT', None) |
| 75 enable_sandbox_if_required(env) | |
| 37 # Ensure paths are correctly separated on windows. | 76 # Ensure paths are correctly separated on windows. |
| 38 cmd[0] = cmd[0].replace('/', os.path.sep) | 77 cmd[0] = cmd[0].replace('/', os.path.sep) |
| 39 cmd = fix_python_path(cmd) | 78 cmd = fix_python_path(cmd) |
| 40 try: | 79 try: |
| 41 return subprocess.call(cmd, env=env) | 80 return subprocess.call(cmd, env=env) |
| 42 except OSError: | 81 except OSError: |
| 43 print >> sys.stderr, 'Failed to start %s' % cmd | 82 print >> sys.stderr, 'Failed to start %s' % cmd |
| 44 raise | 83 raise |
| 45 | 84 |
| 46 | 85 |
| 47 def main(): | 86 def main(): |
| 48 return run_executable(sys.argv[1:], os.environ.copy()) | 87 return run_executable(sys.argv[1:], os.environ.copy()) |
| 49 | 88 |
| 50 | 89 |
| 51 if __name__ == '__main__': | 90 if __name__ == '__main__': |
| 52 sys.exit(main()) | 91 sys.exit(main()) |
| OLD | NEW |