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