Move prefs::kLastPolicyStatisticsUpdate to the policy component.
[chromium-blink-merge.git] / build / android / buildbot / bb_utils.py
blobf901103310ed1a5150f57b734da502b57d5572e1
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 pipes
9 import subprocess
10 import sys
12 import bb_annotations
14 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
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(map(pipes.quote, 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 return MockPopen()
49 return subprocess.Popen(command, cwd=cwd, stdout=stdout)
52 def RunCmd(command, flunk_on_failure=True, halt_on_failure=False,
53 warning_code=constants.WARNING_EXIT_CODE, stdout=None,
54 cwd=CHROME_SRC):
55 """Run a command relative to the chrome source root."""
56 code = SpawnCmd(command, stdout, cwd).wait()
57 print '<', CommandToString(command)
58 if code != 0:
59 print 'ERROR: process exited with code %d' % code
60 if code != warning_code and flunk_on_failure:
61 bb_annotations.PrintError()
62 else:
63 bb_annotations.PrintWarning()
64 # Allow steps to have both halting (i.e. 1) and non-halting exit codes.
65 if code != warning_code and halt_on_failure:
66 print 'FATAL %d != %d' % (code, warning_code)
67 sys.exit(1)
68 return code
71 def GetParser():
72 def ConvertJson(option, _, value, parser):
73 setattr(parser.values, option.dest, json.loads(value))
74 parser = optparse.OptionParser()
75 parser.add_option('--build-properties', action='callback',
76 callback=ConvertJson, type='string', default={},
77 help='build properties in JSON format')
78 parser.add_option('--factory-properties', action='callback',
79 callback=ConvertJson, type='string', default={},
80 help='factory properties in JSON format')
81 return parser
84 def EncodeProperties(options):
85 return ['--factory-properties=%s' % json.dumps(options.factory_properties),
86 '--build-properties=%s' % json.dumps(options.build_properties)]
89 def RunSteps(steps, step_cmds, options):
90 unknown_steps = set(steps) - set(step for step, _ in step_cmds)
91 if unknown_steps:
92 print >> sys.stderr, 'FATAL: Unknown steps %s' % list(unknown_steps)
93 sys.exit(1)
95 for step, cmd in step_cmds:
96 if step in steps:
97 cmd(options)