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