summaryrefslogtreecommitdiffstats
path: root/build/android/buildbot/bb_utils.py
blob: 74b27923da8a7b910b62e246baaaa12449231bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import optparse
import os
import pipes
import subprocess
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from pylib import buildbot_report


TESTING = 'BUILDBOT_TESTING' in os.environ

BB_BUILD_DIR = os.path.abspath(
    os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir,
    os.pardir, os.pardir, os.pardir, os.pardir))

CHROME_SRC = os.path.abspath(
    os.path.join(os.path.dirname(__file__), '..', '..', '..'))

GOMA_DIR = os.environ.get('GOMA_DIR', os.path.join(BB_BUILD_DIR, 'goma'))

def CommandToString(command):
  """Returns quoted command that can be run in bash shell."""
  return ' '.join(map(pipes.quote, command))


def SpawnCmd(command):
  """Spawn a process without waiting for termination."""
  print '>', CommandToString(command)
  sys.stdout.flush()
  if TESTING:
    class MockPopen(object):
      @staticmethod
      def wait():
        return 0
    return MockPopen()
  return subprocess.Popen(command, cwd=CHROME_SRC)


def RunCmd(command, flunk_on_failure=True, halt_on_failure=False,
           warning_code=88):
  """Run a command relative to the chrome source root."""
  code = SpawnCmd(command).wait()
  print '<', CommandToString(command)
  if code != 0:
    print 'ERROR: process exited with code %d' % code
    if code != warning_code and flunk_on_failure:
      buildbot_report.PrintError()
    else:
      buildbot_report.PrintWarning()
    # Allow steps to have both halting (i.e. 1) and non-halting exit codes.
    if code != warning_code and halt_on_failure:
      print 'FATAL %d != %d' % (code, warning_code)
      sys.exit(1)
  return code


def GetParser():
  def ConvertJson(option, _, value, parser):
    setattr(parser.values, option.dest, json.loads(value))
  parser = optparse.OptionParser()
  parser.add_option('--build-properties', action='callback',
                    callback=ConvertJson, type='string', default={},
                    help='build properties in JSON format')
  parser.add_option('--factory-properties', action='callback',
                    callback=ConvertJson, type='string', default={},
                    help='factory properties in JSON format')
  return parser


def EncodeProperties(options):
  return ['--factory-properties=%s' % json.dumps(options.factory_properties),
          '--build-properties=%s' % json.dumps(options.build_properties)]


def RunSteps(all_steps, options):
  if not options.steps:
    return

  steps = options.steps.split(',')
  unknown_steps = set(steps) - set(step for step, _ in all_steps)
  if unknown_steps:
    print >> sys.stderr, 'FATAL: Unknown steps %s' % list(unknown_steps)
    sys.exit(1)

  for step, cmd in all_steps:
    if step in steps:
      cmd(options)