David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2009-2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Package builder for the dev server.""" |
| 8 | import os |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 9 | from portage import dbapi |
| 10 | from portage import xpak |
| 11 | import portage |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 12 | import subprocess |
Andrew de los Reyes | 2066687 | 2011-02-17 10:43:07 -0800 | [diff] [blame] | 13 | import sys |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 14 | import tempfile |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 15 | |
| 16 | import cherrypy |
| 17 | |
| 18 | |
| 19 | def _OutputOf(command): |
| 20 | """Runs command, a list of arguments beginning with an executable. |
| 21 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 22 | Args: |
| 23 | command: A list of arguments, beginning with the executable |
| 24 | Returns: |
| 25 | The output of the command |
| 26 | Raises: |
| 27 | subprocess.CalledProcessError if the command fails |
| 28 | """ |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 29 | command_name = ' '.join(command) |
| 30 | cherrypy.log('Executing: ' + command_name, 'BUILD') |
| 31 | |
| 32 | p = subprocess.Popen(command, stdout=subprocess.PIPE) |
| 33 | output_blob = p.communicate()[0] |
| 34 | if p.returncode != 0: |
| 35 | raise subprocess.CalledProcessError(p.returncode, command_name) |
| 36 | return output_blob |
| 37 | |
| 38 | |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 39 | def _FilterInstallMaskFromPackage(in_path, out_path): |
| 40 | """Filter files matching DEFAULT_INSTALL_MASK out of a tarball. |
| 41 | |
| 42 | Args: |
| 43 | in_path: Unfiltered tarball. |
| 44 | out_path: Location to write filtered tarball. |
| 45 | """ |
| 46 | |
| 47 | # Grab metadata about package in xpak format. |
Yusuke Sato | e88ee54 | 2012-08-28 13:39:48 -0700 | [diff] [blame] | 48 | my_xpak = xpak.xpak_mem(xpak.tbz2(in_path).get_data()) |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 49 | |
| 50 | # Build list of files to exclude. The tar command uses a slightly |
| 51 | # different exclude format than gmerge, so it needs to be adjusted |
| 52 | # appropriately. |
Yusuke Sato | e88ee54 | 2012-08-28 13:39:48 -0700 | [diff] [blame] | 53 | masks = os.environ['DEFAULT_INSTALL_MASK'].split() |
| 54 | # Look for complete paths matching the specified pattern. Leading slashes |
| 55 | # are removed so that the paths are relative. Trailing slashes are removed |
| 56 | # so that we delete the directory itself when the '/usr/include/' path is |
| 57 | # given. |
| 58 | masks = [mask.strip('/') for mask in masks] |
| 59 | masks = ['--exclude="' + mask + '"' for mask in masks] |
| 60 | excludes = ' '.join(masks) |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 61 | |
| 62 | gmerge_dir = os.path.dirname(out_path) |
| 63 | subprocess.check_call(['mkdir', '-p', gmerge_dir]) |
| 64 | |
| 65 | tmpd = tempfile.mkdtemp() |
| 66 | try: |
| 67 | # Extract package to temporary directory (excluding masked files). |
| 68 | cmd = ('pbzip2 -dc --ignore-trailing-garbage=1 %s' |
| 69 | ' | sudo tar -x -C %s %s --wildcards') |
| 70 | subprocess.check_call(cmd % (in_path, tmpd, excludes), shell=True) |
| 71 | |
| 72 | # Build filtered version of package. |
| 73 | cmd = 'sudo tar -c --use-compress-program=pbzip2 -C %s . > %s' |
| 74 | subprocess.check_call(cmd % (tmpd, out_path), shell=True) |
| 75 | finally: |
| 76 | subprocess.check_call(['sudo', 'rm', '-rf', tmpd]) |
| 77 | |
| 78 | # Copy package metadata over to new package file. |
Yusuke Sato | e88ee54 | 2012-08-28 13:39:48 -0700 | [diff] [blame] | 79 | xpak.tbz2(out_path).recompose_mem(my_xpak) |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 80 | |
| 81 | |
Ryan Cui | 0af7a91 | 2012-06-18 18:00:47 -0700 | [diff] [blame] | 82 | def UpdateGmergeBinhost(board, pkg, deep): |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 83 | """Add pkg to our gmerge-specific binhost. |
| 84 | |
| 85 | Files matching DEFAULT_INSTALL_MASK are not included in the tarball. |
| 86 | """ |
| 87 | |
| 88 | root = '/build/%s/' % board |
Ryan Cui | 0af7a91 | 2012-06-18 18:00:47 -0700 | [diff] [blame] | 89 | gmerge_pkgdir = os.path.join(root, 'gmerge-packages') |
| 90 | stripped_link = os.path.join(root, 'stripped-packages') |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 91 | |
| 92 | # Create gmerge pkgdir and give us permission to write to it. |
| 93 | subprocess.check_call(['sudo', 'mkdir', '-p', gmerge_pkgdir]) |
Ryan Cui | 0af7a91 | 2012-06-18 18:00:47 -0700 | [diff] [blame] | 94 | subprocess.check_call(['sudo', 'ln', '-snf', os.path.basename(gmerge_pkgdir), |
| 95 | stripped_link]) |
| 96 | |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 97 | username = os.environ['PORTAGE_USERNAME'] |
| 98 | subprocess.check_call(['sudo', 'chown', username, gmerge_pkgdir]) |
| 99 | |
| 100 | # Load databases. |
| 101 | trees = portage.create_trees(config_root=root, target_root=root) |
| 102 | vardb = trees[root]['vartree'].dbapi |
| 103 | bintree = trees[root]['bintree'] |
| 104 | bintree.populate() |
| 105 | gmerge_tree = dbapi.bintree.binarytree(root, gmerge_pkgdir, |
| 106 | settings=bintree.settings) |
| 107 | gmerge_tree.populate() |
| 108 | |
David James | 3556d22 | 2011-05-20 15:58:41 -0700 | [diff] [blame] | 109 | if deep: |
| 110 | # If we're in deep mode, fill in the binhost completely. |
| 111 | gmerge_matches = set(gmerge_tree.dbapi.cpv_all()) |
| 112 | bindb_matches = set(bintree.dbapi.cpv_all()) |
| 113 | installed_matches = set(vardb.cpv_all()) & bindb_matches |
| 114 | else: |
| 115 | # Otherwise, just fill in the requested package. |
| 116 | gmerge_matches = set(gmerge_tree.dbapi.match(pkg)) |
| 117 | bindb_matches = set(bintree.dbapi.match(pkg)) |
| 118 | installed_matches = set(vardb.match(pkg)) & bindb_matches |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 119 | |
| 120 | # Remove any stale packages that exist in the local binhost but are not |
| 121 | # installed anymore. |
| 122 | if bindb_matches - installed_matches: |
| 123 | subprocess.check_call(['eclean-%s' % board, '-d', 'packages']) |
| 124 | |
| 125 | # Remove any stale packages that exist in the gmerge binhost but are not |
| 126 | # installed anymore. |
| 127 | changed = False |
| 128 | for pkg in gmerge_matches - installed_matches: |
| 129 | gmerge_path = gmerge_tree.getname(pkg) |
| 130 | if os.path.exists(gmerge_path): |
| 131 | os.unlink(gmerge_path) |
| 132 | changed = True |
| 133 | |
| 134 | # Copy any installed packages that have been rebuilt to the gmerge binhost. |
| 135 | for pkg in installed_matches: |
| 136 | build_time, = bintree.dbapi.aux_get(pkg, ['BUILD_TIME']) |
| 137 | build_path = bintree.getname(pkg) |
| 138 | gmerge_path = gmerge_tree.getname(pkg) |
| 139 | |
| 140 | # If a package exists in the gmerge binhost with the same build time, |
| 141 | # don't rebuild it. |
| 142 | if pkg in gmerge_matches and os.path.exists(gmerge_path): |
| 143 | old_build_time, = gmerge_tree.dbapi.aux_get(pkg, ['BUILD_TIME']) |
| 144 | if old_build_time == build_time: |
| 145 | continue |
| 146 | |
David James | 3556d22 | 2011-05-20 15:58:41 -0700 | [diff] [blame] | 147 | cherrypy.log('Filtering install mask from %s' % pkg, 'BUILD') |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 148 | _FilterInstallMaskFromPackage(build_path, gmerge_path) |
| 149 | changed = True |
| 150 | |
| 151 | # If the gmerge binhost was changed, update the Packages file to match. |
| 152 | if changed: |
| 153 | env_copy = os.environ.copy() |
| 154 | env_copy['PKGDIR'] = gmerge_pkgdir |
| 155 | env_copy['ROOT'] = root |
| 156 | env_copy['PORTAGE_CONFIGROOT'] = root |
| 157 | cmd = ['/usr/lib/portage/bin/emaint', '-f', 'binhost'] |
| 158 | subprocess.check_call(cmd, env=env_copy) |
| 159 | |
| 160 | return bool(installed_matches) |
| 161 | |
| 162 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 163 | class Builder(object): |
| 164 | """Builds packages for the devserver.""" |
| 165 | |
| 166 | def _ShouldBeWorkedOn(self, board, pkg): |
| 167 | """Is pkg a package that could be worked on, but is not?""" |
David James | 0bc33fd | 2011-03-02 13:33:38 -0800 | [diff] [blame] | 168 | if pkg in _OutputOf(['cros_workon', '--board=' + board, 'list']): |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 169 | return False |
| 170 | |
| 171 | # If it's in the list of possible workon targets, we should be working on it |
| 172 | return pkg in _OutputOf([ |
David James | 0bc33fd | 2011-03-02 13:33:38 -0800 | [diff] [blame] | 173 | 'cros_workon', '--board=' + board, 'list', '--all']) |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 174 | |
| 175 | def SetError(self, text): |
| 176 | cherrypy.response.status = 500 |
| 177 | cherrypy.log(text, 'BUILD') |
| 178 | return text |
| 179 | |
| 180 | def Build(self, board, pkg, additional_args): |
| 181 | """Handles a build request from the cherrypy server.""" |
Chris Sosa | dda923d | 2011-04-13 13:12:01 -0700 | [diff] [blame] | 182 | cherrypy.log('Additional build request arguments: ' + str(additional_args), |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 183 | 'BUILD') |
| 184 | |
Chris Sosa | dda923d | 2011-04-13 13:12:01 -0700 | [diff] [blame] | 185 | def _AppendStrToEnvVar(env, var, additional_string): |
| 186 | env[var] = env.get(var, '') + ' ' + additional_string |
| 187 | cherrypy.log('%s flags modified to %s' % (var, env[var]), 'BUILD') |
| 188 | |
| 189 | env_copy = os.environ.copy() |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 190 | if 'use' in additional_args: |
Chris Sosa | dda923d | 2011-04-13 13:12:01 -0700 | [diff] [blame] | 191 | _AppendStrToEnvVar(env_copy, 'USE', additional_args['use']) |
| 192 | |
| 193 | if 'features' in additional_args: |
| 194 | _AppendStrToEnvVar(env_copy, 'FEATURES', additional_args['features']) |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 195 | |
| 196 | try: |
| 197 | if (self._ShouldBeWorkedOn(board, pkg) and |
| 198 | not additional_args.get('accept_stable')): |
| 199 | return self.SetError( |
| 200 | 'Package is not cros_workon\'d on the devserver machine.\n' |
| 201 | 'Either start working on the package or pass --accept_stable ' |
| 202 | 'to gmerge') |
| 203 | |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 204 | # If user did not supply -n, we want to rebuild the package. |
| 205 | usepkg = additional_args.get('usepkg') |
| 206 | if not usepkg: |
| 207 | rc = subprocess.call(['emerge-%s' % board, pkg], env=env_copy) |
| 208 | if rc != 0: |
| 209 | return self.SetError('Could not emerge ' + pkg) |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 210 | |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 211 | # Sync gmerge binhost. |
David James | 3556d22 | 2011-05-20 15:58:41 -0700 | [diff] [blame] | 212 | deep = additional_args.get('deep') |
Ryan Cui | 0af7a91 | 2012-06-18 18:00:47 -0700 | [diff] [blame] | 213 | if not UpdateGmergeBinhost(board, pkg, deep): |
David James | ed079b1 | 2011-05-17 14:53:15 -0700 | [diff] [blame] | 214 | return self.SetError('Package %s is not installed' % pkg) |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 215 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 216 | return 'Success\n' |
| 217 | except OSError, e: |
| 218 | return self.SetError('Could not execute build command: ' + str(e)) |