Index: devserver.py |
diff --git a/devserver.py b/devserver.py |
index 7d744c59f3aeb6d44201ccd6d1b777ee152e426d..7de95fa8b423946145cda55a11ad08170f98cc74 100755 |
--- a/devserver.py |
+++ b/devserver.py |
@@ -9,6 +9,7 @@ |
import cherrypy |
import optparse |
import os |
+import subprocess |
import sys |
import autoupdate |
@@ -69,7 +70,85 @@ def _PrepareToServeUpdatesOnly(image_dir): |
'DEVSERVER') |
-class DevServerRoot: |
+def _OutputOf(command): |
+ """Runs command, a list of arguments beginning with an executable. |
+ |
+ If the executable begins with "scripts/", the path is adjusted to |
+ the scripts directory of this chroot. |
+ |
+ Args: |
+ A list of arguments, beginning with the executable |
+ Returns: |
+ A list of lines returned by the command |
+""" |
+ |
sosa
2011/01/14 23:03:54
Don't need this extra line
rochberg
2011/01/15 06:16:48
Done.
|
+ scripts = 'scripts/' |
+ if command[0].find(scripts) == 0: |
+ command[0] = command[0].replace(scripts, '../../' + scripts) |
+ cherrypy.log('Executing: ' + ' '.join(command), 'BUILD') |
+ output_blob = subprocess.Popen(command, |
+ stdout=subprocess.PIPE).communicate()[0] |
+ return [line.rstrip() for line in output_blob.split('\n')] |
+ |
sosa
2011/01/14 23:03:54
2 lines between top level defs
|
+def _IsIn(needle, haystack_list): |
sosa
2011/01/14 23:03:54
Isn't this equivalent to needle in haystick_list?
rochberg
2011/01/15 06:16:48
The way the code is structured now, the relevant t
|
+ for line in haystack_list: |
+ if needle in line: |
+ return True |
+ return False |
+ |
sosa
2011/01/14 23:03:54
2 lines between top level defs
rochberg
2011/01/15 06:16:48
Done.
|
+class Builder(object): |
sosa
2011/01/14 23:03:54
Should probably move this into its own module.
rochberg
2011/01/15 06:16:48
Done.
|
+ def _ShouldBeWorkedOn(self, board, pkg): |
+ """Is pkg a package that could be worked on, but is not?""" |
+ if _IsIn(pkg, _OutputOf([ |
+ 'scripts/cros_workon', '--board=' + board, 'list'])): |
+ return False |
+ |
+ # If it's in the list of possible workon targets, we should be working on it |
+ return _IsIn(pkg, _OutputOf([ |
+ 'scripts/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): |
+ cherrypy.log('Additional build request arguments: '+ str(additional_args), |
+ 'BUILD') |
+ |
+ original_use = os.environ.get('USE', '') |
+ if 'use' in additional_args: |
+ os.environ['USE'] = original_use + ' ' + additional_args['use'] |
+ cherrypy.log("USE flags modified to " + os.environ['USE'], 'BUILD') |
+ |
+ try: |
+ 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 = subprocess.call(['emerge-%s' % board, pkg]) |
+ if rc != 0: |
+ return self.SetError('Could not emerge ' + pkg) |
+ |
+ cherrypy.log('ecleaning %s' % pkg, 'BUILD') |
+ rc = subprocess.call(['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)) |
+ |
+ finally: |
+ os.environ['USE'] = original_use |
+ |
+ |
+class DevServerRoot(object): |
"""The Root Class for the Dev Server. |
CherryPy works as follows: |
@@ -81,17 +160,12 @@ class DevServerRoot: |
cherrypy uses the update method and puts the extra paths in args. |
""" |
- def build(self, board, pkg): |
+ def __init__(self): |
+ self._builder = Builder() |
+ |
+ def build(self, board, pkg, **kwargs): |
"""Builds the package specified.""" |
- cherrypy.log('emerging %s' % pkg, 'BUILD') |
- emerge_command = 'emerge-%s %s' % (board, pkg) |
- err = os.system(emerge_command) |
- if err != 0: |
- raise Exception('failed to execute %s' % emerge_command) |
- eclean_command = 'eclean-%s -d packages' % board |
- err = os.system(eclean_command) |
- if err != 0: |
- raise Exception('failed to execute %s' % emerge_command) |
+ return self._builder.Build(board, pkg, kwargs) |
def index(self): |
return 'Welcome to the Dev Server!' |