blob: 7a6af9cc62475c9c36f669cf2f0c10e67b0757ff [file] [log] [blame]
# Copyright (c) 2009-2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Package builder for the dev server."""
import os
import subprocess
import sys
import cherrypy
def _OutputOf(command):
"""Runs command, a list of arguments beginning with an executable.
command: A list of arguments, beginning with the executable
The output of the command
subprocess.CalledProcessError if the command fails
command_name = ' '.join(command)
cherrypy.log('Executing: ' + command_name, 'BUILD')
p = subprocess.Popen(command, stdout=subprocess.PIPE)
output_blob = p.communicate()[0]
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, command_name)
return output_blob
class Builder(object):
"""Builds packages for the devserver."""
def _ShouldBeWorkedOn(self, board, pkg):
"""Is pkg a package that could be worked on, but is not?"""
if pkg in _OutputOf(['cros_workon', '--board=' + board, 'list']):
return False
# If it's in the list of possible workon targets, we should be working on it
return pkg in _OutputOf([
'cros_workon', '--board=' + board, 'list', '--all'])
def SetError(self, text):
cherrypy.response.status = 500
cherrypy.log(text, 'BUILD')
return text
def Build(self, board, pkg, additional_args):
"""Handles a build request from the cherrypy server."""
cherrypy.log('Additional build request arguments: ' + str(additional_args),
def _AppendStrToEnvVar(env, var, additional_string):
env[var] = env.get(var, '') + ' ' + additional_string
cherrypy.log('%s flags modified to %s' % (var, env[var]), 'BUILD')
env_copy = os.environ.copy()
if 'use' in additional_args:
_AppendStrToEnvVar(env_copy, 'USE', additional_args['use'])
if 'features' in additional_args:
_AppendStrToEnvVar(env_copy, 'FEATURES', additional_args['features'])
if (self._ShouldBeWorkedOn(board, pkg) and
not additional_args.get('accept_stable')):
return self.SetError(
'Package is not cros_workon\'d on the devserver machine.\n'
'Either start working on the package or pass --accept_stable '
'to gmerge')
rc =['emerge-%s' % board, pkg], env=env_copy)
if rc != 0:
return self.SetError('Could not emerge ' + pkg)
cherrypy.log('ecleaning %s' % pkg, 'BUILD')
rc =['eclean-' + board, '-d', 'packages'])
if rc != 0:
return self.SetError('eclean failed')
cherrypy.log('eclean complete %s' % pkg, 'BUILD')
return 'Success\n'
except OSError, e:
return self.SetError('Could not execute build command: ' + str(e))