David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 2 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 7 | """Build packages on a host machine, then install them on the local target. |
Chris Sosa | 136418c | 2010-11-10 16:27:14 -0800 | [diff] [blame] | 8 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 9 | Contacts a devserver (trunk/src/platform/dev/devserver.py) and |
| 10 | requests that it build a package, then performs a binary install of |
| 11 | that package on the local machine. |
| 12 | """ |
Chris Sosa | 4a1e819 | 2010-12-13 14:22:41 -0800 | [diff] [blame] | 13 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 14 | import optparse |
| 15 | import os |
| 16 | import shutil |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import urllib |
| 20 | import urllib2 |
Chris Sosa | 4c9b2f9 | 2010-12-13 16:09:06 -0800 | [diff] [blame] | 21 | |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 22 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 23 | class GMerger(object): |
| 24 | """emerges a package from the devserver. |
Chris Sosa | 136418c | 2010-11-10 16:27:14 -0800 | [diff] [blame] | 25 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 26 | 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 Sosa | 4a1e819 | 2010-12-13 14:22:41 -0800 | [diff] [blame] | 30 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 31 | 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) |
jglasgow | cc71f3a | 2010-03-12 14:30:21 -0500 | [diff] [blame] | 38 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 39 | 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 Sosa | 605fe88 | 2010-04-22 17:01:32 -0700 | [diff] [blame] | 49 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 50 | 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 Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 55 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 56 | def SetupPortageEnvironment(self, environ): |
| 57 | """Setup portage to use stateful partition and fetch from dev server.""" |
| 58 | environ.update({ |
| 59 | 'PORTDIR': '/usr/local/portage', |
| 60 | 'PKGDIR': '/usr/local/portage', |
| 61 | 'DISTDIR': '/usr/local/portage/distfiles', |
| 62 | 'PORTAGE_BINHOST': '%s/static/pkgroot/%s/packages' % ( |
| 63 | self.devkit_url, self.board_name), |
| 64 | 'PORTAGE_TMPDIR': '/tmp', |
| 65 | 'CONFIG_PROTECT': '-*', |
| 66 | 'FEATURES': '-sandbox', |
| 67 | 'ACCEPT_KEYWORDS': 'arm x86 ~arm ~x86', |
| 68 | 'ROOT': '/', |
| 69 | }) |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 70 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 71 | def GeneratePackageRequest(self, package_name): |
| 72 | """Build the POST string that conveys our options to the devserver.""" |
| 73 | post_data = {'board': self.board_name, |
| 74 | 'pkg': package_name, |
| 75 | 'use': FLAGS.use, |
| 76 | 'accept_stable': FLAGS.accept_stable, |
| 77 | } |
| 78 | post_data = dict([(key, value) for (key, value) in post_data.iteritems() |
| 79 | if value]) |
| 80 | return urllib.urlencode(post_data) |
Frank Swiderski | dc13081 | 2010-10-08 15:42:28 -0700 | [diff] [blame] | 81 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 82 | def RequestPackageBuild(self, package_name): |
| 83 | """Contacts devserver to request a build.""" |
| 84 | try: |
| 85 | result = urllib2.urlopen(self.devkit_url + '/build', |
| 86 | data=self.GeneratePackageRequest(package_name)) |
| 87 | print result.read() |
| 88 | result.close() |
Mandeep Singh Baines | ea6b7a5 | 2010-08-17 14:03:57 -0700 | [diff] [blame] | 89 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 90 | except urllib2.HTTPError, e: |
| 91 | # The exception includes the content, which is the error mesage |
| 92 | sys.exit(e.read()) |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 93 | |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 94 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 95 | def main(): |
| 96 | global FLAGS |
| 97 | parser = optparse.OptionParser(usage='usage: %prog [options] package_name') |
| 98 | parser.add_option('--accept_stable', |
| 99 | action='store_true', dest='accept_stable', default=False, |
| 100 | help=('Build even if a cros_workon package is not ' |
| 101 | 'using the live package')) |
| 102 | parser.add_option('-n', '--no_devserver', |
| 103 | action='store_false', dest='call_devserver', default=True, |
| 104 | help='Do not actually ask the server to build') |
| 105 | parser.add_option('--use', '--USE', |
| 106 | dest='use', default=None, |
| 107 | help='USE flags to pass to emerge on the server') |
| 108 | |
| 109 | (FLAGS, remaining_arguments) = parser.parse_args() |
| 110 | if len(remaining_arguments) != 1: |
| 111 | parser.print_help() |
| 112 | sys.exit('Need exactly one package name') |
| 113 | |
| 114 | package_name = remaining_arguments[0] |
| 115 | |
| 116 | try: |
| 117 | subprocess.check_call(['mount', '-o', 'remount,exec', '/tmp']) |
| 118 | merger = GMerger(open('/etc/lsb-release').readlines()) |
| 119 | merger.SetupPortageEnvironment(os.environ) |
| 120 | merger.RemountOrChangeRoot(os.environ) |
| 121 | if FLAGS.call_devserver: |
| 122 | merger.RequestPackageBuild(package_name) |
| 123 | else: |
| 124 | print 'Not requesting fresh build on server---installing whatever we find' |
| 125 | |
| 126 | print 'Emerging ', package_name |
| 127 | subprocess.check_call([ |
| 128 | 'emerge', '--getbinpkgonly', '--usepkgonly', package_name]) |
| 129 | finally: |
| 130 | subprocess.call(['mount', '-o', 'remount,noexec', '/tmp']) |
| 131 | |
| 132 | |
| 133 | if __name__ == '__main__': |
| 134 | main() |