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