blob: 8aca1cd73dc6557cf3603cb98dfca496163ce860 [file] [log] [blame]
David Rochberg7c79a812011-01-19 14:24:45 -05001#!/usr/bin/env python
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -08002
David Rochberg7c79a812011-01-19 14:24:45 -05003# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -08004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
David Rochberg7c79a812011-01-19 14:24:45 -05007"""Build packages on a host machine, then install them on the local target.
Chris Sosa136418c2010-11-10 16:27:14 -08008
David Rochberg7c79a812011-01-19 14:24:45 -05009Contacts a devserver (trunk/src/platform/dev/devserver.py) and
10requests that it build a package, then performs a binary install of
11that package on the local machine.
12"""
Chris Sosa4a1e8192010-12-13 14:22:41 -080013
David Rochberg7c79a812011-01-19 14:24:45 -050014import optparse
15import os
16import shutil
17import subprocess
18import sys
19import urllib
20import urllib2
Chris Sosa4c9b2f92010-12-13 16:09:06 -080021
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080022
David Rochberg7c79a812011-01-19 14:24:45 -050023class GMerger(object):
24 """emerges a package from the devserver.
Chris Sosa136418c2010-11-10 16:27:14 -080025
David Rochberg7c79a812011-01-19 14:24:45 -050026 NB: Must be instantiated using with, e.g.:
27 with GMerger(open('/etc/lsb-release').readlines()) as merger:
28 in order to remount /tmp as executable.
29 """
Chris Sosa4a1e8192010-12-13 14:22:41 -080030
David Rochberg7c79a812011-01-19 14:24:45 -050031 def __init__(self, lsb_release_lines):
32 self.lsb_release = self.ParseLsbRelease(lsb_release_lines)
33 try:
34 self.devkit_url = self.lsb_release['CHROMEOS_DEVSERVER']
35 self.board_name = self.lsb_release['CHROMEOS_RELEASE_BOARD']
36 except KeyError, e:
37 sys.exit('Could not find /etc/lsb_release value: ' + e)
jglasgowcc71f3a2010-03-12 14:30:21 -050038
David Rochberg7c79a812011-01-19 14:24:45 -050039 def RemountOrChangeRoot(self, environ):
40 """Remount the root filesystem rw; install into /usr/local if this fails."""
41 rc = subprocess.call(['mount', '-o', 'remount,rw', '/'])
42 if rc == 0:
43 return
44 answer = raw_input(
45 'Could not mount / as writable. Install into /usr/local? (Y/n)')
46 if answer[0] not in 'Yy':
47 sys.exit('Better safe than sorry.')
48 environ['ROOT'] = '/usr/local'
Chris Sosa605fe882010-04-22 17:01:32 -070049
David Rochberg7c79a812011-01-19 14:24:45 -050050 def ParseLsbRelease(self, lsb_release_lines):
51 """Convert a list of KEY=VALUE lines to a dictionary."""
52 partitioned_lines = [line.rstrip().partition('=')
53 for line in lsb_release_lines]
54 return dict([(fields[0], fields[2]) for fields in partitioned_lines])
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080055
David Rochberg7c79a812011-01-19 14:24:45 -050056 def SetupPortageEnvironment(self, environ):
57 """Setup portage to use stateful partition and fetch from dev server."""
David Jamesed079b12011-05-17 14:53:15 -070058 binhost_prefix = '%s/static/pkgroot/%s' % (self.devkit_url, self.board_name)
David Jamese4f73a42011-05-19 12:18:33 -070059 binhost = '%s/packages' % binhost_prefix
60 if not FLAGS.include_masked_files:
61 binhost += ' %s/gmerge-packages' % binhost_prefix
David Rochberg7c79a812011-01-19 14:24:45 -050062 environ.update({
63 'PORTDIR': '/usr/local/portage',
64 'PKGDIR': '/usr/local/portage',
65 'DISTDIR': '/usr/local/portage/distfiles',
David Jamese4f73a42011-05-19 12:18:33 -070066 'PORTAGE_BINHOST': binhost,
David Rochberg7c79a812011-01-19 14:24:45 -050067 'PORTAGE_TMPDIR': '/tmp',
68 'CONFIG_PROTECT': '-*',
69 'FEATURES': '-sandbox',
70 'ACCEPT_KEYWORDS': 'arm x86 ~arm ~x86',
71 'ROOT': '/',
72 })
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080073
David Rochberg7c79a812011-01-19 14:24:45 -050074 def GeneratePackageRequest(self, package_name):
75 """Build the POST string that conveys our options to the devserver."""
76 post_data = {'board': self.board_name,
77 'pkg': package_name,
David Jamesed079b12011-05-17 14:53:15 -070078 'features': os.environ.get('FEATURES'),
79 'use': os.environ.get('USE'),
80 'accept_stable': FLAGS.accept_stable or '',
81 'usepkg': FLAGS.usepkg or '',
David Rochberg7c79a812011-01-19 14:24:45 -050082 }
83 post_data = dict([(key, value) for (key, value) in post_data.iteritems()
David Jamesed079b12011-05-17 14:53:15 -070084 if value is not None])
David Rochberg7c79a812011-01-19 14:24:45 -050085 return urllib.urlencode(post_data)
Frank Swiderskidc130812010-10-08 15:42:28 -070086
David Rochberg7c79a812011-01-19 14:24:45 -050087 def RequestPackageBuild(self, package_name):
88 """Contacts devserver to request a build."""
89 try:
90 result = urllib2.urlopen(self.devkit_url + '/build',
91 data=self.GeneratePackageRequest(package_name))
92 print result.read()
93 result.close()
Mandeep Singh Bainesea6b7a52010-08-17 14:03:57 -070094
David Rochberg7c79a812011-01-19 14:24:45 -050095 except urllib2.HTTPError, e:
96 # The exception includes the content, which is the error mesage
97 sys.exit(e.read())
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080098
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080099
David Rochberg7c79a812011-01-19 14:24:45 -0500100def main():
101 global FLAGS
102 parser = optparse.OptionParser(usage='usage: %prog [options] package_name')
103 parser.add_option('--accept_stable',
104 action='store_true', dest='accept_stable', default=False,
105 help=('Build even if a cros_workon package is not '
106 'using the live package'))
David Jamese4f73a42011-05-19 12:18:33 -0700107 parser.add_option('--include_masked_files',
108 action='store_true', dest='include_masked_files',
109 default=False, help=('Include masked files in package '
110 '(e.g. debug symbols)'))
David Jamesed079b12011-05-17 14:53:15 -0700111 parser.add_option('-n', '--usepkg',
112 action='store_true', dest='usepkg', default=False,
113 help='Use binary packages.')
David Rochberg7c79a812011-01-19 14:24:45 -0500114
115 (FLAGS, remaining_arguments) = parser.parse_args()
116 if len(remaining_arguments) != 1:
117 parser.print_help()
118 sys.exit('Need exactly one package name')
119
120 package_name = remaining_arguments[0]
121
122 try:
123 subprocess.check_call(['mount', '-o', 'remount,exec', '/tmp'])
124 merger = GMerger(open('/etc/lsb-release').readlines())
David Jamesed079b12011-05-17 14:53:15 -0700125 merger.RequestPackageBuild(package_name)
David Rochberg7c79a812011-01-19 14:24:45 -0500126
127 print 'Emerging ', package_name
Chris Sosadda923d2011-04-13 13:12:01 -0700128 merger.SetupPortageEnvironment(os.environ)
129 merger.RemountOrChangeRoot(os.environ)
David Rochberg7c79a812011-01-19 14:24:45 -0500130 subprocess.check_call([
131 'emerge', '--getbinpkgonly', '--usepkgonly', package_name])
David Jamesed079b12011-05-17 14:53:15 -0700132 subprocess.check_call([
133 'rm', '-rf', '/usr/local/portage'])
David Rochberg7c79a812011-01-19 14:24:45 -0500134 finally:
135 subprocess.call(['mount', '-o', 'remount,noexec', '/tmp'])
136
137
138if __name__ == '__main__':
139 main()