OLD | NEW |
---|---|
(Empty) | |
1 #!/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 """Runs the test with xvfb on linux. | |
7 | |
8 For simplicity in gyp targets, this script has no effect on the other platforms. | |
9 """ | |
10 | |
11 import os | |
12 import platform | |
13 import signal | |
14 import subprocess | |
15 import sys | |
16 | |
17 import test_env | |
18 | |
19 | |
20 def kill(pid): | |
21 """Kills a process and traps exception if the process doesn't exist anymore. | |
22 """ | |
23 # If the process doesn't exist, it raises an exception that we can ignore. | |
24 try: | |
25 os.kill(pid, signal.SIGKILL) | |
26 except OSError: | |
27 pass | |
28 | |
29 | |
30 def get_xvfb_path(server_dir): | |
31 """Figures out which X server to use.""" | |
32 xvfb_path = os.path.join(server_dir, 'Xvfb.' + platform.architecture()[0]) | |
33 if not os.path.exists(xvfb_path): | |
34 xvfb_path = os.path.join(server_dir, 'Xvfb') | |
35 if not os.path.exists(xvfb_path): | |
36 print >> sys.stderr, ( | |
37 'No Xvfb found in designated server path: %s' % server_dir) | |
38 raise Exception('No virtual server') | |
39 return xvfb_path | |
40 | |
41 | |
42 def start_xvfb(xvfb_path, display): | |
43 """Starts a virtual X server that we run the tests in. | |
44 | |
45 This makes it so we can run the tests even if we didn't start the tests from | |
46 an X session. | |
47 | |
48 Args: | |
49 xvfb_path: Path to Xvfb. | |
50 """ | |
51 proc = subprocess.Popen( | |
52 [xvfb_path, display, '-screen', '0', '1024x768x24', '-ac'], | |
53 stdout=subprocess.PIPE, | |
54 stderr=subprocess.STDOUT) | |
55 return proc.pid | |
56 | |
57 | |
58 def wait_for_xvfb(xdisplaycheck, env): | |
59 """Waits for xvfb to be fully initialized by using xdisplaycheck.""" | |
60 try: | |
61 subprocess.check_call( | |
62 [xdisplaycheck], | |
63 stdout=subprocess.PIPE, | |
64 stderr=subprocess.STDOUT, | |
65 env=env) | |
66 except subprocess.CalledProcessError: | |
67 print >> sys.stderr, 'Xvfb failed to load properly.' | |
68 return False | |
69 return True | |
70 | |
71 | |
72 def run_executable(cmd, build_dir, env): | |
73 """Runs an executable within a xvfb buffer on linux. | |
74 | |
75 Runs the test normally on other platforms. Requires that both xvfb and icewm | |
76 are installed on linux. | |
77 """ | |
78 pid = None | |
79 xvfb = 'Xvfb' | |
80 try: | |
81 if sys.platform == 'linux2': | |
82 # Defaults to X display 9. | |
83 display = ':9' | |
84 pid = start_xvfb(xvfb, display) | |
85 env['DISPLAY'] = display | |
86 if not wait_for_xvfb(os.path.join(build_dir, 'xdisplaycheck'), env): | |
87 return 3 | |
88 # Some ChromeOS tests need a window manager. Technically, it could be | |
89 # another script but that would be overkill. | |
90 subprocess.Popen( | |
91 'icewm', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) | |
92 return test_env.run_executable(cmd, env) | |
Roger Tawa OOO till Jul 10th
2012/03/07 19:51:27
should this be indented two more? the comment at
M-A Ruel
2012/03/07 20:21:05
I fixed the comment to be more sensical.
| |
93 finally: | |
94 if pid: | |
95 kill(pid) | |
96 | |
97 | |
98 def main(): | |
99 if len(sys.argv) < 3: | |
100 print >> sys.stderr, ( | |
101 'Usage: xvfb.py [path to build_dir] [command args...]') | |
102 return 2 | |
103 return run_executable(sys.argv[2:], sys.argv[1], os.environ.copy()) | |
104 | |
105 | |
106 if __name__ == "__main__": | |
107 sys.exit(main()) | |
OLD | NEW |