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