Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / build / android / buildbot / bb_utils.py
blobb78fec036b69fe8e04edeeaea6c85ae3b73a4ca7
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import json
6 import optparse
7 import os
8 import subprocess
9 import sys
11 import bb_annotations
13 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
14 from devil.utils import cmd_helper
15 from pylib import constants
18 TESTING = 'BUILDBOT_TESTING' in os.environ
20 BB_BUILD_DIR = os.path.abspath(
21 os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir,
22 os.pardir, os.pardir, os.pardir, os.pardir))
24 CHROME_SRC = os.path.abspath(
25 os.path.join(os.path.dirname(__file__), '..', '..', '..'))
27 # TODO: Figure out how to merge this with pylib.cmd_helper.OutDirectory().
28 CHROME_OUT_DIR = os.path.join(CHROME_SRC, 'out')
30 GOMA_DIR = os.environ.get('GOMA_DIR', os.path.join(BB_BUILD_DIR, 'goma'))
32 GSUTIL_PATH = os.path.join(BB_BUILD_DIR, 'third_party', 'gsutil', 'gsutil')
34 def CommandToString(command):
35 """Returns quoted command that can be run in bash shell."""
36 return ' '.join(cmd_helper.SingleQuote(c) for c in command)
39 def SpawnCmd(command, stdout=None, cwd=CHROME_SRC):
40 """Spawn a process without waiting for termination."""
41 print '>', CommandToString(command)
42 sys.stdout.flush()
43 if TESTING:
44 class MockPopen(object):
45 @staticmethod
46 def wait():
47 return 0
48 @staticmethod
49 def communicate():
50 return '', ''
51 return MockPopen()
52 return subprocess.Popen(command, cwd=cwd, stdout=stdout)
55 def RunCmd(command, flunk_on_failure=True, halt_on_failure=False,
56 warning_code=constants.WARNING_EXIT_CODE, stdout=None,
57 cwd=CHROME_SRC):
58 """Run a command relative to the chrome source root."""
59 code = SpawnCmd(command, stdout, cwd).wait()
60 print '<', CommandToString(command)
61 if code != 0:
62 print 'ERROR: process exited with code %d' % code
63 if code != warning_code and flunk_on_failure:
64 bb_annotations.PrintError()
65 else:
66 bb_annotations.PrintWarning()
67 # Allow steps to have both halting (i.e. 1) and non-halting exit codes.
68 if code != warning_code and halt_on_failure:
69 print 'FATAL %d != %d' % (code, warning_code)
70 sys.exit(1)
71 return code
74 def GetParser():
75 def ConvertJson(option, _, value, parser):
76 setattr(parser.values, option.dest, json.loads(value))
77 parser = optparse.OptionParser()
78 parser.add_option('--build-properties', action='callback',
79 callback=ConvertJson, type='string', default={},
80 help='build properties in JSON format')
81 parser.add_option('--factory-properties', action='callback',
82 callback=ConvertJson, type='string', default={},
83 help='factory properties in JSON format')
84 return parser
87 def EncodeProperties(options):
88 return ['--factory-properties=%s' % json.dumps(options.factory_properties),
89 '--build-properties=%s' % json.dumps(options.build_properties)]
92 def RunSteps(steps, step_cmds, options):
93 unknown_steps = set(steps) - set(step for step, _ in step_cmds)
94 if unknown_steps:
95 print >> sys.stderr, 'FATAL: Unknown steps %s' % list(unknown_steps)
96 sys.exit(1)
98 for step, cmd in step_cmds:
99 if step in steps:
100 cmd(options)