blob: 58bb6289cfe394986b0cd2896230f68ddfbebffe [file] [log] [blame]
rtc@google.comded22402009-10-26 22:36:21 +00001# Copyright (c) 2009 The Chromium OS 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
5import autoupdate
Sean O'Connor14b6a0a2010-03-20 23:23:48 -07006import buildutil
7import optparse
rtc@google.comded22402009-10-26 22:36:21 +00008import os
Sean O'Connor14b6a0a2010-03-20 23:23:48 -07009import SimpleHTTPServer
rtc@google.comded22402009-10-26 22:36:21 +000010import web
chocobo@google.com4dc25812009-10-27 23:46:26 +000011import sys
rtc@google.comded22402009-10-26 22:36:21 +000012
rtc@google.com21a5ca32009-11-04 18:23:23 +000013global updater
14updater = None
rtc@google.comded22402009-10-26 22:36:21 +000015
rtc@google.com64244662009-11-12 00:52:08 +000016global buildbot
17buildbot = None
18
rtc@google.comded22402009-10-26 22:36:21 +000019class index:
20 def GET(self):
rtc@google.com64244662009-11-12 00:52:08 +000021 pkgs = buildbot.GetPackages()
rtc@google.comded22402009-10-26 22:36:21 +000022 return render.index(pkgs)
23
24class update:
25 """
26 Processes updates from the client machine. If an update is found, the url
27 references a static link that can be served automagically from web.py.
28 """
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070029 def POST(self, args=None):
30 return updater.HandleUpdatePing(web.data(), args)
rtc@google.com21a5ca32009-11-04 18:23:23 +000031
rtc@google.com64244662009-11-12 00:52:08 +000032class build:
33 """
34 builds the package specified by the pkg parameter and returns the name
35 of the output file.
36 """
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080037 def POST(self):
rtc@google.com64244662009-11-12 00:52:08 +000038 input = web.input()
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070039 web.debug('emerging %s ' % input.pkg)
40 emerge_command = 'emerge-%s %s' % (input.board, input.pkg)
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080041 err = os.system(emerge_command)
42 if err != 0:
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070043 raise Exception('failed to execute %s' % emerge_command)
rtc@google.comded22402009-10-26 22:36:21 +000044
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070045if __name__ == '__main__':
46 usage = 'usage: %prog [options]'
47 parser = optparse.OptionParser(usage)
48 parser.add_option('-a', '--archive_dir', dest='archive_dir',
49 help='serve archived builds only.')
Sean O'Connor1f7fd362010-04-07 16:34:52 -070050 parser.add_option('-t', action='store_true', dest='test_image')
51 parser.add_option('-u', '--urlbase', dest='urlbase',
52 help='base URL, other than devserver, for update images.')
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070053 options, args = parser.parse_args()
54 # clean up the args, due to httpserver's hardcoded use of sys.argv
55 if options.archive_dir:
56 sys.argv.remove('-a')
57 sys.argv.remove(options.archive_dir)
58 if options.test_image:
59 sys.argv.remove('-t')
Sean O'Connor1f7fd362010-04-07 16:34:52 -070060 if options.urlbase:
61 sys.argv.remove('-u')
62 sys.argv.remove(options.urlbase)
rtc@google.com21a5ca32009-11-04 18:23:23 +000063
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070064 root_dir = os.path.realpath('%s/../..' %
65 os.path.dirname(os.path.abspath(sys.argv[0])))
66 if options.archive_dir:
67 static_dir = os.path.realpath(options.archive_dir)
68 assert os.path.exists(static_dir), '%s must exist.' % options.archive_dir
69 web.debug('using archive dir: %s' % static_dir)
70 else:
71 static_dir = os.path.realpath('%s/static' %
72 os.path.dirname(os.path.abspath(sys.argv[0])))
73 web.debug('dev root is %s' % root_dir)
74 os.system('mkdir -p %s' % static_dir)
75 web.debug('Serving images from %s' % static_dir)
rtc@google.com21a5ca32009-11-04 18:23:23 +000076
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070077 updater = autoupdate.Autoupdate(root_dir=root_dir,
78 static_dir=static_dir,
79 serve_only=options.archive_dir,
Sean O'Connor1f7fd362010-04-07 16:34:52 -070080 urlbase=options.urlbase,
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070081 test_image=options.test_image)
rtc@google.com21a5ca32009-11-04 18:23:23 +000082 urls = ('/', 'index',
rtc@google.com64244662009-11-12 00:52:08 +000083 '/update', 'update',
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070084 '/update/(.+)', 'update',
rtc@google.com64244662009-11-12 00:52:08 +000085 '/webbuild', 'webbuild',
86 '/build', 'build')
87
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070088 app = web.application(urls, globals(), autoreload=True)
rtc@google.com21a5ca32009-11-04 18:23:23 +000089 render = web.template.render('templates/')
rtc@google.comded22402009-10-26 22:36:21 +000090 app.run()