OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011 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 """ Initialize the environment variables and start the buildbot slave. | |
7 """ | |
8 | |
9 import os | |
10 import subprocess | |
11 import sys | |
12 | |
13 | |
14 def remove_all_vars_except(dictionary, keep): | |
15 """Remove all keys from the specified dictionary except those in !keep|""" | |
16 for key in set(dictionary.keys()) - set(keep): | |
17 dictionary.pop(key) | |
18 | |
19 | |
20 def Reboot(): | |
21 print "Rebooting..." | |
22 if sys.platform.startswith('win'): | |
23 subprocess.call(['shutdown', '-r', '-f', '-t', '1']) | |
24 elif sys.platform in ('darwin', 'posix', 'linux2'): | |
25 subprocess.call(['sudo', 'shutdown', '-r', 'now']) | |
26 else: | |
27 raise NotImplementedError('Implement Reboot function') | |
28 | |
29 | |
30 def HotPatchSlaveBuilder(): | |
31 """We could override the SlaveBuilder class but it's way simpler to just | |
32 hotpatch it.""" | |
33 from buildbot.slave.bot import SlaveBuilder | |
34 old_remote_shutdown = SlaveBuilder.remote_shutdown | |
35 | |
36 def rebooting_remote_shutdown(self): | |
37 old_remote_shutdown(self) | |
38 Reboot() | |
39 | |
40 SlaveBuilder.remote_shutdown = rebooting_remote_shutdown | |
41 | |
42 | |
43 def main(): | |
44 # change the current directory to the directory of the script. | |
45 os.chdir(sys.path[0]) | |
46 | |
47 # Make sure the current python path is absolute. | |
48 paths = os.environ['PYTHONPATH'].split(os.pathsep) | |
49 os.environ['PYTHONPATH'] = '' | |
50 for path in paths: | |
51 os.environ['PYTHONPATH'] += os.path.abspath(path) | |
52 os.environ['PYTHONPATH'] += os.pathsep | |
53 | |
54 # Update the python path. | |
55 parent_dir = os.path.abspath(os.path.pardir) | |
56 chromium_buildbot_dir = os.path.join( | |
57 parent_dir, os.path.pardir, os.path.pardir, | |
58 'third_party', 'chromium_buildbot') | |
59 root = os.path.dirname(parent_dir) | |
60 python_path = [ | |
61 os.path.join(chromium_buildbot_dir, 'site_config'), | |
62 os.path.join(chromium_buildbot_dir, 'scripts'), | |
63 os.path.join(chromium_buildbot_dir, 'scripts', 'release'), | |
64 os.path.join(chromium_buildbot_dir, 'third_party'), | |
65 os.path.join(parent_dir), | |
66 '.', # Include the current working directory by default. | |
67 ] | |
68 os.environ['PYTHONPATH'] += os.pathsep.join(python_path) | |
69 print os.environ['PYTHONPATH'] | |
70 | |
71 # Add these in from of the PATH too. | |
72 new_path = python_path | |
73 new_path.extend(sys.path) | |
74 sys.path = new_path | |
75 | |
76 os.environ['CHROME_HEADLESS'] = '1' | |
77 os.environ['PAGER'] = 'cat' | |
78 | |
79 # Platform-specific initialization. | |
80 | |
81 if sys.platform.startswith('win'): | |
82 # list of all variables that we want to keep | |
83 env_var = [ | |
84 'APPDATA', | |
85 'BUILDBOT_ARCHIVE_FORCE_SSH', | |
86 'CHROME_HEADLESS', | |
87 'CHROMIUM_BUILD', | |
88 'COMSPEC', | |
89 'COMPUTERNAME', | |
90 'DXSDK_DIR', | |
91 'HOMEDRIVE', | |
92 'HOMEPATH', | |
93 'LOCALAPPDATA', | |
94 'NUMBER_OF_PROCESSORS', | |
95 'OS', | |
96 'PATH', | |
97 'PATHEXT', | |
98 'PROCESSOR_ARCHITECTURE', | |
99 'PROCESSOR_ARCHITEW6432', | |
100 'PROGRAMFILES', | |
101 'PROGRAMW6432', | |
102 'PYTHONPATH', | |
103 'SYSTEMDRIVE', | |
104 'SYSTEMROOT', | |
105 'TEMP', | |
106 'TMP', | |
107 'USERNAME', | |
108 'USERDOMAIN', | |
109 'USERPROFILE', | |
110 'VS100COMNTOOLS', | |
111 'WINDIR', | |
112 ] | |
113 | |
114 remove_all_vars_except(os.environ, env_var) | |
115 | |
116 # extend the env variables with the chrome-specific settings. | |
117 depot_tools = os.path.join(chromium_buildbot_dir, '..', 'depot_tools') | |
118 # Reuse the python executable used to start this script. | |
119 python = os.path.dirname(sys.executable) | |
120 system32 = os.path.join(os.environ['SYSTEMROOT'], 'system32') | |
121 wbem = os.path.join(system32, 'WBEM') | |
122 slave_path = [depot_tools, python, system32, wbem] | |
123 # build_internal/tools contains tools we can't redistribute. | |
124 tools = os.path.join(chromium_buildbot_dir, '..', 'build_internal', 'tools') | |
125 if os.path.isdir(tools): | |
126 slave_path.append(os.path.abspath(tools)) | |
127 os.environ['PATH'] = os.pathsep.join(slave_path) | |
128 os.environ['LOGNAME'] = os.environ['USERNAME'] | |
129 | |
130 elif sys.platform in ('darwin', 'posix', 'linux2'): | |
131 # list of all variables that we want to keep | |
132 env_var = [ | |
133 'CCACHE_DIR', | |
134 'CHROME_ALLOCATOR', | |
135 'CHROME_HEADLESS', | |
136 'DISPLAY', | |
137 'DISTCC_DIR', | |
138 'HOME', | |
139 'HOSTNAME', | |
140 'LANG', | |
141 'LOGNAME', | |
142 'PAGER', | |
143 'PATH', | |
144 'PWD', | |
145 'PYTHONPATH', | |
146 'SHELL', | |
147 'SSH_AGENT_PID', | |
148 'SSH_AUTH_SOCK', | |
149 'SSH_CLIENT', | |
150 'SSH_CONNECTION', | |
151 'SSH_TTY', | |
152 'USER', | |
153 'USERNAME', | |
154 ] | |
155 | |
156 remove_all_vars_except(os.environ, env_var) | |
157 | |
158 depot_tools = os.path.join(chromium_buildbot_dir, '..', 'depot_tools') | |
159 | |
160 slave_path = [depot_tools, '/usr/bin', '/bin', | |
161 '/usr/sbin', '/sbin', '/usr/local/bin'] | |
162 os.environ['PATH'] = os.pathsep.join(slave_path) | |
163 | |
164 elif sys.platform == 'cygwin': | |
165 #TODO(maruel): Implement me. | |
166 pass | |
167 else: | |
168 raise NotImplementedError('Unknown platform') | |
169 | |
170 # Run the slave. | |
171 HotPatchSlaveBuilder() | |
172 import twisted.scripts.twistd as twistd | |
173 twistd.run() | |
174 | |
175 | |
176 if '__main__' == __name__: | |
177 main() | |
OLD | NEW |