Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / build / android / buildbot / bb_run_bot.py
blob2b5f31aca1d787eb1d07bc02c5e7dbb6480ec044
1 #!/usr/bin/env python
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 import collections
8 import copy
9 import json
10 import os
11 import pipes
12 import re
13 import subprocess
14 import sys
16 import bb_utils
18 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
19 from pylib import constants
22 CHROMIUM_COVERAGE_BUCKET = 'chromium-code-coverage'
24 _BotConfig = collections.namedtuple(
25 'BotConfig', ['bot_id', 'host_obj', 'test_obj'])
27 HostConfig = collections.namedtuple(
28 'HostConfig',
29 ['script', 'host_steps', 'extra_args', 'extra_gyp_defines', 'target_arch'])
31 TestConfig = collections.namedtuple('Tests', ['script', 'tests', 'extra_args'])
34 def BotConfig(bot_id, host_object, test_object=None):
35 return _BotConfig(bot_id, host_object, test_object)
38 def DictDiff(d1, d2):
39 diff = []
40 for key in sorted(set(d1.keys() + d2.keys())):
41 if key in d1 and d1[key] != d2.get(key):
42 diff.append('- %s=%s' % (key, pipes.quote(d1[key])))
43 if key in d2 and d2[key] != d1.get(key):
44 diff.append('+ %s=%s' % (key, pipes.quote(d2[key])))
45 return '\n'.join(diff)
48 def GetEnvironment(host_obj, testing, extra_env_vars=None):
49 init_env = dict(os.environ)
50 init_env['GYP_GENERATORS'] = 'ninja'
51 if extra_env_vars:
52 init_env.update(extra_env_vars)
53 envsetup_cmd = '. build/android/envsetup.sh'
54 if testing:
55 # Skip envsetup to avoid presubmit dependence on android deps.
56 print 'Testing mode - skipping "%s"' % envsetup_cmd
57 envsetup_cmd = ':'
58 else:
59 print 'Running %s' % envsetup_cmd
60 proc = subprocess.Popen(['bash', '-exc',
61 envsetup_cmd + ' >&2; python build/android/buildbot/env_to_json.py'],
62 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
63 cwd=bb_utils.CHROME_SRC, env=init_env)
64 json_env, envsetup_output = proc.communicate()
65 if proc.returncode != 0:
66 print >> sys.stderr, 'FATAL Failure in envsetup.'
67 print >> sys.stderr, envsetup_output
68 sys.exit(1)
69 env = json.loads(json_env)
70 env['GYP_DEFINES'] = env.get('GYP_DEFINES', '') + \
71 ' OS=android fastbuild=1 use_goma=1 gomadir=%s' % bb_utils.GOMA_DIR
72 if host_obj.target_arch:
73 env['GYP_DEFINES'] += ' target_arch=%s' % host_obj.target_arch
74 extra_gyp = host_obj.extra_gyp_defines
75 if extra_gyp:
76 env['GYP_DEFINES'] += ' %s' % extra_gyp
77 if re.search('(asan|clang)=1', extra_gyp):
78 env.pop('CXX_target', None)
80 # Bots checkout chrome in /b/build/slave/<name>/build/src
81 build_internal_android = os.path.abspath(os.path.join(
82 bb_utils.CHROME_SRC, '..', '..', '..', '..', '..', 'build_internal',
83 'scripts', 'slave', 'android'))
84 if os.path.exists(build_internal_android):
85 env['PATH'] = os.pathsep.join([build_internal_android, env['PATH']])
86 return env
89 def GetCommands(options, bot_config):
90 """Get a formatted list of commands.
92 Args:
93 options: Options object.
94 bot_config: A BotConfig named tuple.
95 host_step_script: Host step script.
96 device_step_script: Device step script.
97 Returns:
98 list of Command objects.
99 """
100 property_args = bb_utils.EncodeProperties(options)
101 commands = [[bot_config.host_obj.script,
102 '--steps=%s' % ','.join(bot_config.host_obj.host_steps)] +
103 property_args + (bot_config.host_obj.extra_args or [])]
105 test_obj = bot_config.test_obj
106 if test_obj:
107 run_test_cmd = [test_obj.script] + property_args
108 for test in test_obj.tests:
109 run_test_cmd.extend(['-f', test])
110 if test_obj.extra_args:
111 run_test_cmd.extend(test_obj.extra_args)
112 commands.append(run_test_cmd)
113 return commands
116 def GetBotStepMap():
117 compile_step = ['compile']
118 chrome_proxy_tests = ['chrome_proxy']
119 python_unittests = ['python_unittests']
120 std_host_tests = ['check_webview_licenses']
121 std_build_steps = ['compile', 'zip_build']
122 std_test_steps = ['extract_build']
123 std_tests = ['ui', 'unit']
124 telemetry_tests = ['telemetry_perf_unittests']
125 telemetry_tests_user_build = ['telemetry_unittests',
126 'telemetry_perf_unittests']
127 trial_tests = [
128 'base_junit_tests',
129 'components_browsertests',
130 'gfx_unittests',
132 flakiness_server = (
133 '--flakiness-server=%s' % constants.UPSTREAM_FLAKINESS_SERVER)
134 experimental = ['--experimental']
135 bisect_chrome_output_dir = os.path.abspath(
136 os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir,
137 os.pardir, 'bisect', 'src', 'out'))
138 B = BotConfig
139 H = (lambda steps, extra_args=None, extra_gyp=None, target_arch=None:
140 HostConfig('build/android/buildbot/bb_host_steps.py', steps, extra_args,
141 extra_gyp, target_arch))
142 T = (lambda tests, extra_args=None:
143 TestConfig('build/android/buildbot/bb_device_steps.py', tests,
144 extra_args))
146 bot_configs = [
147 # Main builders
148 B('main-builder-dbg', H(std_build_steps + std_host_tests)),
149 B('main-builder-rel', H(std_build_steps)),
150 B('main-clang-builder',
151 H(compile_step, extra_gyp='clang=1 component=shared_library')),
152 B('main-clobber', H(compile_step)),
153 B('main-tests-rel', H(std_test_steps),
154 T(std_tests + telemetry_tests + chrome_proxy_tests,
155 ['--cleanup', flakiness_server])),
156 B('main-tests', H(std_test_steps),
157 T(std_tests, ['--cleanup', flakiness_server])),
159 # Other waterfalls
160 B('asan-builder-tests', H(compile_step,
161 extra_gyp='asan=1 component=shared_library'),
162 T(std_tests, ['--asan', '--asan-symbolize'])),
163 B('blink-try-builder', H(compile_step)),
164 B('chromedriver-fyi-tests-dbg', H(std_test_steps),
165 T(['chromedriver'],
166 ['--install=ChromeShell', '--install=ChromeDriverWebViewShell',
167 '--skip-wipe', '--disable-location', '--cleanup'])),
168 B('fyi-x86-builder-dbg',
169 H(compile_step + std_host_tests, experimental, target_arch='ia32')),
170 B('fyi-builder-dbg',
171 H(std_build_steps + std_host_tests, experimental,
172 extra_gyp='emma_coverage=1')),
173 B('x86-builder-dbg',
174 H(compile_step + std_host_tests, target_arch='ia32')),
175 B('fyi-builder-rel', H(std_build_steps, experimental)),
176 B('fyi-tests', H(std_test_steps),
177 T(std_tests + python_unittests,
178 ['--experimental', flakiness_server,
179 '--coverage-bucket', CHROMIUM_COVERAGE_BUCKET,
180 '--cleanup'])),
181 B('user-build-fyi-tests-dbg', H(std_test_steps),
182 T(sorted(telemetry_tests_user_build + trial_tests))),
183 B('fyi-component-builder-tests-dbg',
184 H(compile_step, extra_gyp='component=shared_library'),
185 T(std_tests, ['--experimental', flakiness_server])),
186 B('gpu-builder-tests-dbg',
187 H(compile_step),
188 T(['gpu'], ['--install=ContentShell'])),
189 # Pass empty T([]) so that logcat monitor and device status check are run.
190 B('perf-bisect-builder-tests-dbg',
191 H(['bisect_perf_regression']),
192 T([], ['--chrome-output-dir', bisect_chrome_output_dir])),
193 B('perf-tests-rel', H(std_test_steps),
194 T([], ['--install=ChromeShell', '--cleanup'])),
195 B('webkit-latest-webkit-tests', H(std_test_steps),
196 T(['webkit_layout', 'webkit'], ['--cleanup', '--auto-reconnect'])),
197 B('webkit-latest-contentshell', H(compile_step),
198 T(['webkit_layout'], ['--auto-reconnect'])),
199 B('builder-unit-tests', H(compile_step), T(['unit'])),
201 # Generic builder config (for substring match).
202 B('builder', H(std_build_steps)),
205 bot_map = dict((config.bot_id, config) for config in bot_configs)
207 # These bots have identical configuration to ones defined earlier.
208 copy_map = [
209 ('lkgr-clobber', 'main-clobber'),
210 ('try-builder-dbg', 'main-builder-dbg'),
211 ('try-builder-rel', 'main-builder-rel'),
212 ('try-clang-builder', 'main-clang-builder'),
213 ('try-fyi-builder-dbg', 'fyi-builder-dbg'),
214 ('try-x86-builder-dbg', 'x86-builder-dbg'),
215 ('try-tests-rel', 'main-tests-rel'),
216 ('try-tests', 'main-tests'),
217 ('try-fyi-tests', 'fyi-tests'),
218 ('webkit-latest-tests', 'main-tests'),
220 for to_id, from_id in copy_map:
221 assert to_id not in bot_map
222 # pylint: disable=W0212
223 bot_map[to_id] = copy.deepcopy(bot_map[from_id])._replace(bot_id=to_id)
225 # Trybots do not upload to flakiness dashboard. They should be otherwise
226 # identical in configuration to their trunk building counterparts.
227 test_obj = bot_map[to_id].test_obj
228 if to_id.startswith('try') and test_obj:
229 extra_args = test_obj.extra_args
230 if extra_args and flakiness_server in extra_args:
231 extra_args.remove(flakiness_server)
232 return bot_map
235 # Return an object from the map, looking first for an exact id match.
236 # If this fails, look for an id which is a substring of the specified id.
237 # Choose the longest of all substring matches.
238 # pylint: disable=W0622
239 def GetBestMatch(id_map, id):
240 config = id_map.get(id)
241 if not config:
242 substring_matches = [x for x in id_map.iterkeys() if x in id]
243 if substring_matches:
244 max_id = max(substring_matches, key=len)
245 print 'Using config from id="%s" (substring match).' % max_id
246 config = id_map[max_id]
247 return config
250 def GetRunBotOptParser():
251 parser = bb_utils.GetParser()
252 parser.add_option('--bot-id', help='Specify bot id directly.')
253 parser.add_option('--testing', action='store_true',
254 help='For testing: print, but do not run commands')
256 return parser
259 def GetBotConfig(options, bot_step_map):
260 bot_id = options.bot_id or options.factory_properties.get('android_bot_id')
261 if not bot_id:
262 print (sys.stderr,
263 'A bot id must be specified through option or factory_props.')
264 return
266 bot_config = GetBestMatch(bot_step_map, bot_id)
267 if not bot_config:
268 print 'Error: config for id="%s" cannot be inferred.' % bot_id
269 return bot_config
272 def RunBotCommands(options, commands, env):
273 print 'Environment changes:'
274 print DictDiff(dict(os.environ), env)
276 for command in commands:
277 print bb_utils.CommandToString(command)
278 sys.stdout.flush()
279 if options.testing:
280 env['BUILDBOT_TESTING'] = '1'
281 return_code = subprocess.call(command, cwd=bb_utils.CHROME_SRC, env=env)
282 if return_code != 0:
283 return return_code
286 def main(argv):
287 proc = subprocess.Popen(
288 ['/bin/hostname', '-f'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
289 hostname_stdout, hostname_stderr = proc.communicate()
290 if proc.returncode == 0:
291 print 'Running on: ' + hostname_stdout
292 else:
293 print >> sys.stderr, 'WARNING: failed to run hostname'
294 print >> sys.stderr, hostname_stdout
295 print >> sys.stderr, hostname_stderr
296 sys.exit(1)
298 parser = GetRunBotOptParser()
299 options, args = parser.parse_args(argv[1:])
300 if args:
301 parser.error('Unused args: %s' % args)
303 bot_config = GetBotConfig(options, GetBotStepMap())
304 if not bot_config:
305 sys.exit(1)
307 print 'Using config:', bot_config
309 commands = GetCommands(options, bot_config)
310 for command in commands:
311 print 'Will run: ', bb_utils.CommandToString(command)
312 print
314 env = GetEnvironment(bot_config.host_obj, options.testing)
315 return RunBotCommands(options, commands, env)
318 if __name__ == '__main__':
319 sys.exit(main(sys.argv))