Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Mike Frysinger | ad8c6ca | 2014-02-03 11:28:45 -0500 | [diff] [blame] | 5 | """Fast alternative to `emerge-$BOARD autotest-all` |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 6 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 7 | Simple script to be run inside the chroot. Used as a fast approximation of |
| 8 | emerge-$board autotest-all, by simply rsync'ing changes from trunk to sysroot. |
| 9 | """ |
| 10 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 11 | import argparse |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 12 | from collections import namedtuple |
Aviv Keshet | fe54a8a | 2013-09-16 15:49:03 -0700 | [diff] [blame] | 13 | import glob |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 14 | import logging |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 15 | import os |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 16 | import re |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 17 | import sys |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 18 | |
Alex Klein | 727e320 | 2020-05-29 11:13:55 -0600 | [diff] [blame] | 19 | from chromite.lib import commandline |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 20 | from chromite.lib import constants |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 21 | from chromite.lib import cros_build_lib |
| 22 | from chromite.lib import git |
Aviv Keshet | 557e688 | 2013-04-25 13:26:09 -0700 | [diff] [blame] | 23 | from chromite.lib import osutils |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 24 | from chromite.lib.parser import package_info |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 25 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 26 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 27 | if cros_build_lib.IsInsideChroot(): |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 28 | # pylint: disable=import-error |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 29 | import portage |
| 30 | |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 31 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 32 | INCLUDE_PATTERNS_FILENAME = 'autotest-quickmerge-includepatterns' |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 33 | AUTOTEST_SYMLINK = 'autotest_lib' |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 34 | AUTOTEST_PROJECT_NAME = 'chromiumos/third_party/autotest' |
Aviv Keshet | 5f3cf72 | 2013-05-09 17:35:25 -0700 | [diff] [blame] | 35 | AUTOTEST_EBUILD = 'chromeos-base/autotest' |
Aviv Keshet | fe54a8a | 2013-09-16 15:49:03 -0700 | [diff] [blame] | 36 | DOWNGRADE_EBUILDS = ['chromeos-base/autotest'] |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 37 | |
Aviv Keshet | c73cfc3 | 2013-06-14 16:18:53 -0700 | [diff] [blame] | 38 | IGNORE_SUBDIRS = ['ExternalSource', |
| 39 | 'logs', |
| 40 | 'results', |
| 41 | 'site-packages'] |
| 42 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 43 | # Data structure describing a single rsync filesystem change. |
| 44 | # |
| 45 | # change_description: An 11 character string, the rsync change description |
| 46 | # for the particular file. |
| 47 | # absolute_path: The absolute path of the created or modified file. |
| 48 | ItemizedChange = namedtuple('ItemizedChange', ['change_description', |
| 49 | 'absolute_path']) |
| 50 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 51 | # Data structure describing the rsync new/modified files or directories. |
| 52 | # |
| 53 | # new_files: A list of ItemizedChange objects for new files. |
| 54 | # modified_files: A list of ItemizedChange objects for modified files. |
| 55 | # new_directories: A list of ItemizedChange objects for new directories. |
| 56 | ItemizedChangeReport = namedtuple('ItemizedChangeReport', |
| 57 | ['new_files', 'modified_files', |
| 58 | 'new_directories']) |
| 59 | |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 60 | |
Aviv Keshet | 84bdfc5 | 2013-06-04 13:19:38 -0700 | [diff] [blame] | 61 | class PortagePackageAPIError(Exception): |
| 62 | """Exception thrown when unable to retrieve a portage package API.""" |
| 63 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 64 | |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 65 | def GetStalePackageNames(change_list, autotest_sysroot): |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 66 | """Given a rsync change report, returns the names of stale test packages. |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 67 | |
| 68 | This function pulls out test package names for client-side tests, stored |
| 69 | within the client/site_tests directory tree, that had any files added or |
| 70 | modified and for whom any existing bzipped test packages may now be stale. |
| 71 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 72 | Args: |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 73 | change_list: A list of ItemizedChange objects corresponding to changed |
| 74 | or modified files. |
| 75 | autotest_sysroot: Absolute path of autotest in the sysroot, |
Aviv Keshet | 474469d | 2013-07-22 12:54:52 -0700 | [diff] [blame] | 76 | e.g. '/build/lumpy/usr/local/build/autotest' |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 77 | |
| 78 | Returns: |
| 79 | A list of test package names, eg ['factory_Leds', 'login_UserPolicyKeys']. |
| 80 | May contain duplicate entries if multiple files within a test directory |
| 81 | were modified. |
| 82 | """ |
| 83 | exp = os.path.abspath(autotest_sysroot) + r'/client/site_tests/(.*?)/.*' |
| 84 | matches = [re.match(exp, change.absolute_path) for change in change_list] |
| 85 | return [match.group(1) for match in matches if match] |
| 86 | |
| 87 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 88 | def ItemizeChangesFromRsyncOutput(rsync_output, destination_path): |
| 89 | """Convert the output of an rsync with `-i` to a ItemizedChangeReport object. |
| 90 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 91 | Args: |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 92 | rsync_output: String stdout of rsync command that was run with `-i` option. |
| 93 | destination_path: String absolute path of the destination directory for the |
| 94 | rsync operations. This argument is necessary because |
| 95 | rsync's output only gives the relative path of |
| 96 | touched/added files. |
| 97 | |
| 98 | Returns: |
| 99 | ItemizedChangeReport object giving the absolute paths of files that were |
| 100 | created or modified by rsync. |
| 101 | """ |
| 102 | modified_matches = re.findall(r'([.>]f[^+]{9}) (.*)', rsync_output) |
| 103 | new_matches = re.findall(r'(>f\+{9}) (.*)', rsync_output) |
| 104 | new_symlink_matches = re.findall(r'(cL\+{9}) (.*) -> .*', rsync_output) |
| 105 | new_dir_matches = re.findall(r'(cd\+{9}) (.*)', rsync_output) |
| 106 | |
| 107 | absolute_modified = [ItemizedChange(c, os.path.join(destination_path, f)) |
| 108 | for (c, f) in modified_matches] |
| 109 | |
| 110 | # Note: new symlinks are treated as new files. |
| 111 | absolute_new = [ItemizedChange(c, os.path.join(destination_path, f)) |
| 112 | for (c, f) in new_matches + new_symlink_matches] |
| 113 | |
| 114 | absolute_new_dir = [ItemizedChange(c, os.path.join(destination_path, f)) |
| 115 | for (c, f) in new_dir_matches] |
| 116 | |
| 117 | return ItemizedChangeReport(new_files=absolute_new, |
| 118 | modified_files=absolute_modified, |
| 119 | new_directories=absolute_new_dir) |
| 120 | |
| 121 | |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 122 | def GetPackageAPI(portage_root, package_cp): |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 123 | """Gets portage API handles for the given package. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 124 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 125 | Args: |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 126 | portage_root: Root directory of portage tree. Eg '/' or '/build/lumpy' |
Mike Frysinger | ad8c6ca | 2014-02-03 11:28:45 -0500 | [diff] [blame] | 127 | package_cp: A string similar to 'chromeos-base/autotest-tests'. |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 128 | |
| 129 | Returns: |
| 130 | Returns (package, vartree) tuple, where |
| 131 | package is of type portage.dbapi.vartree.dblink |
| 132 | vartree is of type portage.dbapi.vartree.vartree |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 133 | """ |
| 134 | if portage_root is None: |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 135 | # pylint: disable=no-member |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 136 | portage_root = portage.root |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 137 | # Ensure that portage_root ends with trailing slash. |
| 138 | portage_root = os.path.join(portage_root, '') |
| 139 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 140 | # Create a vartree object corresponding to portage_root. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 141 | trees = portage.create_trees(portage_root, portage_root) |
| 142 | vartree = trees[portage_root]['vartree'] |
| 143 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 144 | # List the matching installed packages in cpv format. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 145 | matching_packages = vartree.dbapi.cp_list(package_cp) |
| 146 | |
| 147 | if not matching_packages: |
Aviv Keshet | 84bdfc5 | 2013-06-04 13:19:38 -0700 | [diff] [blame] | 148 | raise PortagePackageAPIError('No matching package for %s in portage_root ' |
| 149 | '%s' % (package_cp, portage_root)) |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 150 | |
| 151 | if len(matching_packages) > 1: |
Aviv Keshet | 84bdfc5 | 2013-06-04 13:19:38 -0700 | [diff] [blame] | 152 | raise PortagePackageAPIError('Too many matching packages for %s in ' |
| 153 | 'portage_root %s' % (package_cp, |
| 154 | portage_root)) |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 155 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 156 | # Convert string match to package dblink. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 157 | package_cpv = matching_packages[0] |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 158 | package_split = package_info.SplitCPV(package_cpv) |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 159 | # pylint: disable=no-member |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 160 | package = portage.dblink(package_split.category, |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 161 | package_split.pv, settings=vartree.settings, |
| 162 | vartree=vartree) |
| 163 | |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 164 | return package, vartree |
| 165 | |
| 166 | |
| 167 | def DowngradePackageVersion(portage_root, package_cp, |
| 168 | downgrade_to_version='0'): |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 169 | """Downgrade the specified portage package version. |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 170 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 171 | Args: |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 172 | portage_root: Root directory of portage tree. Eg '/' or '/build/lumpy' |
Mike Frysinger | ad8c6ca | 2014-02-03 11:28:45 -0500 | [diff] [blame] | 173 | package_cp: A string similar to 'chromeos-base/autotest-tests'. |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 174 | downgrade_to_version: String version to downgrade to. Default: '0' |
| 175 | |
| 176 | Returns: |
Aviv Keshet | 557e688 | 2013-04-25 13:26:09 -0700 | [diff] [blame] | 177 | True on success. False on failure (nonzero return code from `mv` command). |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 178 | """ |
Aviv Keshet | 84bdfc5 | 2013-06-04 13:19:38 -0700 | [diff] [blame] | 179 | try: |
| 180 | package, _ = GetPackageAPI(portage_root, package_cp) |
| 181 | except PortagePackageAPIError: |
| 182 | # Unable to fetch a corresponding portage package API for this |
| 183 | # package_cp (either no such package, or name ambigious and matches). |
| 184 | # So, just fail out. |
| 185 | return False |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 186 | |
| 187 | source_directory = package.dbdir |
| 188 | destination_path = os.path.join( |
| 189 | package.dbroot, package_cp + '-' + downgrade_to_version) |
| 190 | if os.path.abspath(source_directory) == os.path.abspath(destination_path): |
Aviv Keshet | 557e688 | 2013-04-25 13:26:09 -0700 | [diff] [blame] | 191 | return True |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 192 | command = ['mv', source_directory, destination_path] |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 193 | code = cros_build_lib.sudo_run(command, check=False).returncode |
Aviv Keshet | 557e688 | 2013-04-25 13:26:09 -0700 | [diff] [blame] | 194 | return code == 0 |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 195 | |
| 196 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 197 | def UpdatePackageContents(change_report, package_cp, portage_root=None): |
| 198 | """Add newly created files/directors to package contents. |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 199 | |
| 200 | Given an ItemizedChangeReport, add the newly created files and directories |
| 201 | to the CONTENTS of an installed portage package, such that these files are |
| 202 | considered owned by that package. |
| 203 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 204 | Args: |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 205 | change_report: ItemizedChangeReport object for the changes to be |
| 206 | made to the package. |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 207 | package_cp: A string similar to 'chromeos-base/autotest-tests' giving |
| 208 | the package category and name of the package to be altered. |
| 209 | portage_root: Portage root path, corresponding to the board that |
| 210 | we are working on. Defaults to '/' |
| 211 | """ |
| 212 | package, vartree = GetPackageAPI(portage_root, package_cp) |
| 213 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 214 | # Append new contents to package contents dictionary. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 215 | contents = package.getcontents().copy() |
| 216 | for _, filename in change_report.new_files: |
| 217 | contents.setdefault(filename, (u'obj', '0', '0')) |
| 218 | for _, dirname in change_report.new_directories: |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 219 | # Strip trailing slashes if present. |
| 220 | contents.setdefault(dirname.rstrip('/'), (u'dir',)) |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 221 | |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 222 | # Write new contents dictionary to file. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 223 | vartree.dbapi.writeContentsToContentsFile(package, contents) |
| 224 | |
| 225 | |
Aviv Keshet | 1927675 | 2013-05-16 11:12:23 -0700 | [diff] [blame] | 226 | def RemoveBzipPackages(autotest_sysroot): |
| 227 | """Remove all bzipped test/dep/profiler packages from sysroot autotest. |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 228 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 229 | Args: |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 230 | autotest_sysroot: Absolute path of autotest in the sysroot, |
Aviv Keshet | 474469d | 2013-07-22 12:54:52 -0700 | [diff] [blame] | 231 | e.g. '/build/lumpy/usr/local/build/autotest' |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 232 | """ |
Aviv Keshet | 1927675 | 2013-05-16 11:12:23 -0700 | [diff] [blame] | 233 | osutils.RmDir(os.path.join(autotest_sysroot, 'packages'), |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 234 | ignore_missing=True) |
Aviv Keshet | 1927675 | 2013-05-16 11:12:23 -0700 | [diff] [blame] | 235 | osutils.SafeUnlink(os.path.join(autotest_sysroot, 'packages.checksum')) |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 236 | |
| 237 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 238 | def RsyncQuickmerge(source_path, sysroot_autotest_path, |
| 239 | include_pattern_file=None, pretend=False, |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame] | 240 | overwrite=False): |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 241 | """Run rsync quickmerge command, with specified arguments. |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 242 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 243 | Command will take form `rsync -a [options] --exclude=**.pyc |
| 244 | --exclude=**.pyo |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 245 | [optional --include-from include_pattern_file] |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 246 | --exclude=* [source_path] [sysroot_autotest_path]` |
| 247 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 248 | Args: |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 249 | source_path: Directory to rsync from. |
| 250 | sysroot_autotest_path: Directory to rsync too. |
| 251 | include_pattern_file: Optional pattern of files to include in rsync. |
Mike Frysinger | ad8c6ca | 2014-02-03 11:28:45 -0500 | [diff] [blame] | 252 | pretend: True to use the '-n' option to rsync, to perform dry run. |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 253 | overwrite: True to omit '-u' option, overwrite all files in sysroot, |
| 254 | not just older files. |
Aviv Keshet | 557e688 | 2013-04-25 13:26:09 -0700 | [diff] [blame] | 255 | |
| 256 | Returns: |
| 257 | The cros_build_lib.CommandResult object resulting from the rsync command. |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 258 | """ |
| 259 | command = ['rsync', '-a'] |
| 260 | |
Prathmesh Prabhu | 137266a | 2014-02-11 20:02:18 -0800 | [diff] [blame] | 261 | # For existing files, preserve destination permissions. This ensures that |
| 262 | # existing files end up with the file permissions set by the ebuilds. |
| 263 | # If this script copies over a file that does not exist in the destination |
| 264 | # tree, it will set the least restrictive permissions allowed in the |
| 265 | # destination tree. This could happen if the file copied is not installed by |
| 266 | # *any* ebuild, or if the ebuild that installs the file was never emerged. |
| 267 | command += ['--no-p', '--chmod=ugo=rwX'] |
| 268 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 269 | if pretend: |
| 270 | command += ['-n'] |
| 271 | |
| 272 | if not overwrite: |
| 273 | command += ['-u'] |
| 274 | |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame] | 275 | command += ['-i'] |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 276 | |
| 277 | command += ['--exclude=**.pyc'] |
| 278 | command += ['--exclude=**.pyo'] |
| 279 | |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 280 | # Always exclude the autotest symlink to avoid a possible recursion hole. |
| 281 | # The order here is (unfortunately) extremely important. |
| 282 | if AUTOTEST_SYMLINK not in source_path: |
| 283 | command += ['--exclude=%s/' % AUTOTEST_SYMLINK] |
| 284 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 285 | # Exclude files with a specific substring in their name, because |
| 286 | # they create an ambiguous itemized report. (see unit test file for details) |
| 287 | command += ['--exclude=** -> *'] |
| 288 | |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 289 | # Order seems important here, and this include must come before the possible |
| 290 | # exclude below... |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 291 | if include_pattern_file: |
| 292 | command += ['--include-from=%s' % include_pattern_file] |
| 293 | |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 294 | if AUTOTEST_SYMLINK in source_path: |
| 295 | command += ['-l'] |
| 296 | else: |
| 297 | command += ['-L', '--exclude=*'] |
Po-Hsien Wang | 745ac35 | 2018-04-13 12:25:41 -0700 | [diff] [blame] | 298 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 299 | command += [source_path, sysroot_autotest_path] |
| 300 | |
Mike Frysinger | 1385856 | 2020-02-25 16:07:55 -0500 | [diff] [blame] | 301 | return cros_build_lib.sudo_run(command, stdout=True, encoding='utf-8') |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 302 | |
| 303 | |
| 304 | def ParseArguments(argv): |
| 305 | """Parse command line arguments |
| 306 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 307 | Returns: |
| 308 | parsed arguments. |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 309 | """ |
Alex Klein | 727e320 | 2020-05-29 11:13:55 -0600 | [diff] [blame] | 310 | parser = commandline.ArgumentParser( |
| 311 | description='Perform a fast approximation to emerge-$board ' |
| 312 | 'autotest-all, by rsyncing source tree to sysroot.') |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 313 | |
Aviv Keshet | 0a366a0 | 2013-07-18 10:52:04 -0700 | [diff] [blame] | 314 | default_board = cros_build_lib.GetDefaultBoard() |
| 315 | parser.add_argument('--board', metavar='BOARD', default=default_board, |
| 316 | help='Board to perform quickmerge for. Default: ' + |
| 317 | (default_board or 'Not configured.')) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 318 | parser.add_argument('--pretend', action='store_true', |
| 319 | help='Dry run only, do not modify sysroot autotest.') |
| 320 | parser.add_argument('--overwrite', action='store_true', |
| 321 | help='Overwrite existing files even if newer.') |
Aviv Keshet | c73cfc3 | 2013-06-14 16:18:53 -0700 | [diff] [blame] | 322 | parser.add_argument('--force', action='store_true', |
Simran Basi | 8fc8899 | 2015-04-21 17:15:23 -0700 | [diff] [blame] | 323 | help=argparse.SUPPRESS) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 324 | |
Aviv Keshet | 474469d | 2013-07-22 12:54:52 -0700 | [diff] [blame] | 325 | # Used only if test_that is calling autotest_quickmerge and has detected that |
| 326 | # the sysroot autotest path is still in usr/local/autotest (ie the build |
| 327 | # pre-dates https://chromium-review.googlesource.com/#/c/62880/ ) |
| 328 | parser.add_argument('--legacy_path', action='store_true', |
| 329 | help=argparse.SUPPRESS) |
| 330 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 331 | return parser.parse_args(argv) |
| 332 | |
| 333 | |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 334 | def _maybe_add_autotest_symlink(src_paths, path, dest_path): |
| 335 | """If the symlink folders exists, add them to the src to quickmerge.""" |
| 336 | autotest_client_symlink = os.path.join(path, 'client', AUTOTEST_SYMLINK) |
| 337 | if os.path.exists(autotest_client_symlink): |
| 338 | src_paths.append((autotest_client_symlink, |
| 339 | os.path.join(dest_path, 'client/'))) |
| 340 | autotest_main_symlink = os.path.join(path, AUTOTEST_SYMLINK) |
| 341 | if os.path.exists(autotest_main_symlink): |
| 342 | src_paths.append((autotest_main_symlink, dest_path)) |
| 343 | |
| 344 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 345 | def main(argv): |
| 346 | cros_build_lib.AssertInsideChroot() |
| 347 | |
| 348 | args = ParseArguments(argv) |
| 349 | |
Ram Chandrasekar | 6975128 | 2022-02-25 21:07:36 +0000 | [diff] [blame] | 350 | if osutils.IsNonRootUser(): |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 351 | try: |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 352 | cros_build_lib.sudo_run([sys.executable] + sys.argv) |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 353 | except cros_build_lib.RunCommandError: |
| 354 | return 1 |
| 355 | return 0 |
| 356 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 357 | if not args.board: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 358 | print('No board specified. Aborting.') |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 359 | return 1 |
| 360 | |
| 361 | manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT) |
David James | e3b0606 | 2013-11-09 18:52:02 -0800 | [diff] [blame] | 362 | checkout = manifest.FindCheckout(AUTOTEST_PROJECT_NAME) |
Simran Basi | 348fccc | 2015-04-28 12:39:01 -0700 | [diff] [blame] | 363 | brillo_autotest_src_path = os.path.join(checkout.GetPath(absolute=True), '') |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 364 | |
| 365 | script_path = os.path.dirname(__file__) |
| 366 | include_pattern_file = os.path.join(script_path, INCLUDE_PATTERNS_FILENAME) |
| 367 | |
| 368 | # TODO: Determine the following string programatically. |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 369 | sysroot_path = os.path.join('/build', args.board, '') |
Aviv Keshet | 474469d | 2013-07-22 12:54:52 -0700 | [diff] [blame] | 370 | sysroot_autotest_path = os.path.join(sysroot_path, |
| 371 | constants.AUTOTEST_BUILD_PATH, '') |
| 372 | if args.legacy_path: |
| 373 | sysroot_autotest_path = os.path.join(sysroot_path, 'usr/local/autotest', |
| 374 | '') |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 375 | |
Simran Basi | 348fccc | 2015-04-28 12:39:01 -0700 | [diff] [blame] | 376 | # Generate the list of source paths to copy. |
| 377 | src_paths = {os.path.abspath(brillo_autotest_src_path)} |
| 378 | for quickmerge_file in glob.glob(os.path.join(sysroot_autotest_path, |
| 379 | 'quickmerge', '*', '*')): |
| 380 | try: |
| 381 | path = osutils.ReadFile(quickmerge_file).strip() |
| 382 | if path and os.path.exists(path): |
| 383 | src_paths.add(os.path.abspath(path)) |
| 384 | except IOError: |
| 385 | logging.error('Could not quickmerge for project: %s', |
| 386 | os.path.basename(quickmerge_file)) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 387 | |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 388 | # Autotest uses a circular symlink in client that *must* be added after the |
| 389 | # other sections of Autotest. |
| 390 | src_paths = list(src_paths) |
| 391 | |
| 392 | # All destination paths up to this point are the same, but other sources |
| 393 | # added below might have a different one. |
| 394 | src_dest_paths = [(src_path + '/', sysroot_autotest_path) |
| 395 | for src_path in src_paths] |
| 396 | |
| 397 | _maybe_add_autotest_symlink(src_dest_paths, brillo_autotest_src_path, |
| 398 | sysroot_autotest_path) |
Simran Basi | 348fccc | 2015-04-28 12:39:01 -0700 | [diff] [blame] | 399 | num_new_files = 0 |
| 400 | num_modified_files = 0 |
Derek Beckett | 1f19c9c | 2020-12-14 12:25:53 -0800 | [diff] [blame] | 401 | for src_path, dest_path in src_dest_paths: |
| 402 | rsync_output = RsyncQuickmerge(src_path, dest_path, |
Simran Basi | 348fccc | 2015-04-28 12:39:01 -0700 | [diff] [blame] | 403 | include_pattern_file, args.pretend, |
| 404 | args.overwrite) |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 405 | |
Simran Basi | 348fccc | 2015-04-28 12:39:01 -0700 | [diff] [blame] | 406 | if args.verbose: |
| 407 | logging.info(rsync_output.output) |
| 408 | change_report = ItemizeChangesFromRsyncOutput(rsync_output.output, |
| 409 | sysroot_autotest_path) |
| 410 | num_new_files = num_new_files + len(change_report.new_files) |
| 411 | num_modified_files = num_modified_files + len(change_report.modified_files) |
| 412 | if not args.pretend: |
| 413 | logging.info('Updating portage database.') |
| 414 | UpdatePackageContents(change_report, AUTOTEST_EBUILD, sysroot_path) |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame] | 415 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 416 | if not args.pretend: |
Aviv Keshet | fe54a8a | 2013-09-16 15:49:03 -0700 | [diff] [blame] | 417 | for logfile in glob.glob(os.path.join(sysroot_autotest_path, 'packages', |
| 418 | '*.log')): |
| 419 | try: |
| 420 | # Open file in a try-except block, for atomicity, instead of |
| 421 | # doing existence check. |
| 422 | with open(logfile, 'r') as f: |
| 423 | package_cp = f.readline().strip() |
| 424 | DOWNGRADE_EBUILDS.append(package_cp) |
| 425 | except IOError: |
| 426 | pass |
| 427 | |
Aviv Keshet | 3cc4e9e | 2013-04-24 10:47:23 -0700 | [diff] [blame] | 428 | for ebuild in DOWNGRADE_EBUILDS: |
Aviv Keshet | 557e688 | 2013-04-25 13:26:09 -0700 | [diff] [blame] | 429 | if not DowngradePackageVersion(sysroot_path, ebuild): |
Aviv Keshet | 3cc4e9e | 2013-04-24 10:47:23 -0700 | [diff] [blame] | 430 | logging.warning('Unable to downgrade package %s version number.', |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 431 | ebuild) |
Aviv Keshet | 1927675 | 2013-05-16 11:12:23 -0700 | [diff] [blame] | 432 | RemoveBzipPackages(sysroot_autotest_path) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 433 | |
Aviv Keshet | c73cfc3 | 2013-06-14 16:18:53 -0700 | [diff] [blame] | 434 | sentinel_filename = os.path.join(sysroot_autotest_path, |
| 435 | '.quickmerge_sentinel') |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 436 | cros_build_lib.run(['touch', sentinel_filename]) |
Aviv Keshet | c73cfc3 | 2013-06-14 16:18:53 -0700 | [diff] [blame] | 437 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 438 | if args.pretend: |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 439 | logging.info('The following message is pretend only. No filesystem ' |
Aviv Keshet | e7b2019 | 2013-04-24 14:05:53 -0700 | [diff] [blame] | 440 | 'changes made.') |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 441 | logging.info('Quickmerge complete. Created or modified %s files.', |
Simran Basi | 348fccc | 2015-04-28 12:39:01 -0700 | [diff] [blame] | 442 | num_new_files + num_modified_files) |
Aviv Keshet | e00caeb | 2013-04-17 14:03:25 -0700 | [diff] [blame] | 443 | |
| 444 | return 0 |