Home | History | Annotate | Download | only in buildbot
      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.
      4 
      5 import json
      6 import optparse
      7 import os
      8 import pipes
      9 import subprocess
     10 import sys
     11 
     12 import bb_annotations
     13 
     14 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
     15 from pylib import constants
     16 
     17 
     18 TESTING = 'BUILDBOT_TESTING' in os.environ
     19 
     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))
     23 
     24 CHROME_SRC = os.path.abspath(
     25     os.path.join(os.path.dirname(__file__), '..', '..', '..'))
     26 
     27 GOMA_DIR = os.environ.get('GOMA_DIR', os.path.join(BB_BUILD_DIR, 'goma'))
     28 
     29 def CommandToString(command):
     30   """Returns quoted command that can be run in bash shell."""
     31   return ' '.join(map(pipes.quote, command))
     32 
     33 
     34 def SpawnCmd(command, stdout=None, cwd=CHROME_SRC):
     35   """Spawn a process without waiting for termination."""
     36   print '>', CommandToString(command)
     37   sys.stdout.flush()
     38   if TESTING:
     39     class MockPopen(object):
     40       @staticmethod
     41       def wait():
     42         return 0
     43     return MockPopen()
     44   return subprocess.Popen(command, cwd=cwd, stdout=stdout)
     45 
     46 
     47 def RunCmd(command, flunk_on_failure=True, halt_on_failure=False,
     48            warning_code=constants.WARNING_EXIT_CODE, stdout=None,
     49            cwd=CHROME_SRC):
     50   """Run a command relative to the chrome source root."""
     51   code = SpawnCmd(command, stdout, cwd).wait()
     52   print '<', CommandToString(command)
     53   if code != 0:
     54     print 'ERROR: process exited with code %d' % code
     55     if code != warning_code and flunk_on_failure:
     56       bb_annotations.PrintError()
     57     else:
     58       bb_annotations.PrintWarning()
     59     # Allow steps to have both halting (i.e. 1) and non-halting exit codes.
     60     if code != warning_code and halt_on_failure:
     61       print 'FATAL %d != %d' % (code, warning_code)
     62       sys.exit(1)
     63   return code
     64 
     65 
     66 def GetParser():
     67   def ConvertJson(option, _, value, parser):
     68     setattr(parser.values, option.dest, json.loads(value))
     69   parser = optparse.OptionParser()
     70   parser.add_option('--build-properties', action='callback',
     71                     callback=ConvertJson, type='string', default={},
     72                     help='build properties in JSON format')
     73   parser.add_option('--factory-properties', action='callback',
     74                     callback=ConvertJson, type='string', default={},
     75                     help='factory properties in JSON format')
     76   return parser
     77 
     78 
     79 def EncodeProperties(options):
     80   return ['--factory-properties=%s' % json.dumps(options.factory_properties),
     81           '--build-properties=%s' % json.dumps(options.build_properties)]
     82 
     83 
     84 def RunSteps(steps, step_cmds, options):
     85   unknown_steps = set(steps) - set(step for step, _ in step_cmds)
     86   if unknown_steps:
     87     print >> sys.stderr, 'FATAL: Unknown steps %s' % list(unknown_steps)
     88     sys.exit(1)
     89 
     90   for step, cmd in step_cmds:
     91     if step in steps:
     92       cmd(options)
     93