blob: fa4f1ddd0d480a118e146dbe3b9717d762ce3929 [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
chocobo@google.com4dc25812009-10-27 23:46:26 +00009import sys
Andrew de los Reyes52620802010-04-12 13:40:07 -070010import web
rtc@google.comded22402009-10-26 22:36:21 +000011
rtc@google.com21a5ca32009-11-04 18:23:23 +000012global updater
13updater = None
rtc@google.comded22402009-10-26 22:36:21 +000014
rtc@google.com64244662009-11-12 00:52:08 +000015global buildbot
16buildbot = None
17
rtc@google.comded22402009-10-26 22:36:21 +000018class index:
19 def GET(self):
rtc@google.com64244662009-11-12 00:52:08 +000020 pkgs = buildbot.GetPackages()
rtc@google.comded22402009-10-26 22:36:21 +000021 return render.index(pkgs)
22
23class update:
24 """
25 Processes updates from the client machine. If an update is found, the url
26 references a static link that can be served automagically from web.py.
27 """
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070028 def POST(self, args=None):
29 return updater.HandleUpdatePing(web.data(), args)
rtc@google.com21a5ca32009-11-04 18:23:23 +000030
rtc@google.com64244662009-11-12 00:52:08 +000031class build:
32 """
33 builds the package specified by the pkg parameter and returns the name
34 of the output file.
35 """
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080036 def POST(self):
rtc@google.com64244662009-11-12 00:52:08 +000037 input = web.input()
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070038 web.debug('emerging %s ' % input.pkg)
39 emerge_command = 'emerge-%s %s' % (input.board, input.pkg)
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080040 err = os.system(emerge_command)
41 if err != 0:
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070042 raise Exception('failed to execute %s' % emerge_command)
rtc@google.comded22402009-10-26 22:36:21 +000043
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070044if __name__ == '__main__':
45 usage = 'usage: %prog [options]'
46 parser = optparse.OptionParser(usage)
Sean O'Connore38ea152010-04-16 13:50:40 -070047 parser.add_option('--archive_dir', dest='archive_dir',
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070048 help='serve archived builds only.')
Andrew de los Reyes9223f132010-05-07 17:08:17 -070049 parser.add_option('--client_prefix', dest='client_prefix',
50 help='Required prefix for client software version.',
51 default='MementoSoftwareUpdate')
Andrew de los Reyes52620802010-04-12 13:40:07 -070052 parser.add_option('--factory_config', dest='factory_config',
53 help='Config file for serving images from factory floor.')
Sean O'Connor1f7fd362010-04-07 16:34:52 -070054 parser.add_option('-t', action='store_true', dest='test_image')
55 parser.add_option('-u', '--urlbase', dest='urlbase',
56 help='base URL, other than devserver, for update images.')
Andrew de los Reyes52620802010-04-12 13:40:07 -070057 parser.add_option('--validate_factory_config', action="store_true",
58 dest='validate_factory_config',
59 help='Validate factory config file, then exit.')
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070060 options, args = parser.parse_args()
61 # clean up the args, due to httpserver's hardcoded use of sys.argv
62 if options.archive_dir:
Andrew de los Reyes52620802010-04-12 13:40:07 -070063 sys.argv.remove('--archive_dir')
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070064 sys.argv.remove(options.archive_dir)
Andrew de los Reyes9223f132010-05-07 17:08:17 -070065 if '--client_prefix' in sys.argv:
66 sys.argv.remove('--client_prefix')
67 sys.argv.remove(options.client_prefix)
Andrew de los Reyes52620802010-04-12 13:40:07 -070068 if options.factory_config:
69 sys.argv.remove('--factory_config')
70 sys.argv.remove(options.factory_config)
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070071 if options.test_image:
72 sys.argv.remove('-t')
Sean O'Connor1f7fd362010-04-07 16:34:52 -070073 if options.urlbase:
74 sys.argv.remove('-u')
75 sys.argv.remove(options.urlbase)
Andrew de los Reyes52620802010-04-12 13:40:07 -070076 if options.validate_factory_config:
77 sys.argv.remove('--validate_factory_config')
rtc@google.com21a5ca32009-11-04 18:23:23 +000078
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070079 root_dir = os.path.realpath('%s/../..' %
80 os.path.dirname(os.path.abspath(sys.argv[0])))
81 if options.archive_dir:
82 static_dir = os.path.realpath(options.archive_dir)
83 assert os.path.exists(static_dir), '%s must exist.' % options.archive_dir
84 web.debug('using archive dir: %s' % static_dir)
85 else:
86 static_dir = os.path.realpath('%s/static' %
87 os.path.dirname(os.path.abspath(sys.argv[0])))
88 web.debug('dev root is %s' % root_dir)
89 os.system('mkdir -p %s' % static_dir)
90 web.debug('Serving images from %s' % static_dir)
rtc@google.com21a5ca32009-11-04 18:23:23 +000091
Andrew de los Reyes52620802010-04-12 13:40:07 -070092 updater = autoupdate.Autoupdate(
93 root_dir=root_dir,
94 static_dir=static_dir,
95 serve_only=options.archive_dir,
96 urlbase=options.urlbase,
97 test_image=options.test_image,
98 factory_config_path=options.factory_config,
99 validate_factory_config=options.validate_factory_config)
100 if options.validate_factory_config:
101 sys.exit(0)
rtc@google.com21a5ca32009-11-04 18:23:23 +0000102 urls = ('/', 'index',
rtc@google.com64244662009-11-12 00:52:08 +0000103 '/update', 'update',
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700104 '/update/(.+)', 'update',
rtc@google.com64244662009-11-12 00:52:08 +0000105 '/webbuild', 'webbuild',
106 '/build', 'build')
107
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700108 app = web.application(urls, globals(), autoreload=True)
rtc@google.com21a5ca32009-11-04 18:23:23 +0000109 render = web.template.render('templates/')
rtc@google.comded22402009-10-26 22:36:21 +0000110 app.run()