OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 |
| 3 # Copyright (c) 2010 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 """This module uprevs Chrome for cbuildbot.""" |
| 8 |
| 9 import optparse |
| 10 import os |
| 11 import re |
| 12 import sys |
| 13 import urllib |
| 14 |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 16 import cros_mark_as_stable |
| 17 |
| 18 sys.path.append(os.path.join(os.path.dirname(__file__), '../lib')) |
| 19 from cros_build_lib import RunCommand, Info, Warning |
| 20 |
| 21 BASE_CHROME_SVN_URL = 'http://src.chromium.org/svn' |
| 22 |
| 23 # Command for which chrome ebuild to uprev. |
| 24 TIP_OF_TRUNK, LATEST_RELEASE, STICKY = 'tot', 'latest_release', 'sticky_release' |
| 25 CHROME_REV = [TIP_OF_TRUNK, LATEST_RELEASE, STICKY] |
| 26 |
| 27 # Helper regex's for finding ebuilds. |
| 28 _CHROME_VERSION_REGEX = '\d+\.\d+\.\d+\.\d+' |
| 29 _NON_STICKY_REGEX = '%s[(_rc.*)|(_alpha.*)]+' % _CHROME_VERSION_REGEX |
| 30 |
| 31 # Dir where all the action happens. |
| 32 _CHROME_OVERLAY_DIR = ('%(srcroot)s/third_party/chromiumos-overlay' |
| 33 '/chromeos-base/chromeos-chrome') |
| 34 |
| 35 # Different than cros_mark so devs don't have local collisions. |
| 36 _STABLE_BRANCH_NAME = 'chrome_stabilizing_branch' |
| 37 |
| 38 _GIT_COMMIT_MESSAGE = ('Marking %(chrome_rev)s for chrome ebuild with version ' |
| 39 '%(chrome_version)s as stable.') |
| 40 |
| 41 |
| 42 def _GetSvnUrl(): |
| 43 """Returns the path to the svn url for the given chrome branch.""" |
| 44 return os.path.join(BASE_CHROME_SVN_URL, 'trunk') |
| 45 |
| 46 |
| 47 def _GetTipOfTrunkSvnRevision(): |
| 48 """Returns the current svn revision for the chrome tree.""" |
| 49 svn_url = _GetSvnUrl() |
| 50 svn_info = RunCommand(['svn', 'info', svn_url], redirect_stdout=True) |
| 51 |
| 52 revision_re = re.compile('^Revision:\s+(\d+).*') |
| 53 for line in svn_info.splitlines(): |
| 54 match = revision_re.search(line) |
| 55 if match: |
| 56 return match.group(1) |
| 57 |
| 58 raise Exception('Could not find revision information from %s' % svn_url) |
| 59 |
| 60 |
| 61 def _GetTipOfTrunkVersion(): |
| 62 """Returns the current Chrome version.""" |
| 63 svn_url = _GetSvnUrl() |
| 64 chrome_version_file = urllib.urlopen(os.path.join(svn_url, 'src', 'chrome', |
| 65 'VERSION')) |
| 66 chrome_version_info = chrome_version_file.read() |
| 67 chrome_version_file.close() |
| 68 |
| 69 # Sanity check. |
| 70 if '404 Not Found' in chrome_version_info: |
| 71 raise Exception('Url %s does not have version file.' % svn_url) |
| 72 |
| 73 chrome_version_array = [] |
| 74 |
| 75 for line in chrome_version_info.splitlines(): |
| 76 chrome_version_array.append(line.rpartition('=')[2]) |
| 77 |
| 78 return '.'.join(chrome_version_array) |
| 79 |
| 80 |
| 81 def _GetLatestRelease(branch=None): |
| 82 """Gets the latest release version from the buildspec_url for the branch. |
| 83 |
| 84 Args: |
| 85 branch: If set, gets the latest release for branch, otherwise latest |
| 86 release. |
| 87 Returns: |
| 88 Latest version string. |
| 89 """ |
| 90 buildspec_url = 'http://src.chromium.org/svn/releases' |
| 91 svn_ls = RunCommand(['svn', 'ls', buildspec_url], redirect_stdout=True) |
| 92 sorted_ls = RunCommand(['sort', '--version-sort'], input=svn_ls, |
| 93 redirect_stdout=True) |
| 94 if branch: |
| 95 chrome_version_re = re.compile('^%s\.\d+.*' % branch) |
| 96 else: |
| 97 chrome_version_re = re.compile('^[0-9]\..*') |
| 98 for chrome_version in sorted_ls.splitlines(): |
| 99 if chrome_version_re.match(chrome_version): |
| 100 current_version = chrome_version |
| 101 |
| 102 return current_version.rstrip('/') |
| 103 |
| 104 |
| 105 def _GetStickyVersion(stable_ebuilds): |
| 106 """Discovers the sticky version from the current stable_ebuilds.""" |
| 107 sticky_ebuilds = [] |
| 108 non_sticky_re = re.compile(_NON_STICKY_REGEX) |
| 109 for ebuild in stable_ebuilds: |
| 110 if not non_sticky_re.match(ebuild.version): |
| 111 sticky_ebuilds.append(ebuild) |
| 112 |
| 113 if not sticky_ebuilds: |
| 114 raise Exception('No sticky ebuilds found') |
| 115 elif len(sticky_ebuilds) > 1: |
| 116 Warning('More than one sticky ebuild found') |
| 117 |
| 118 return cros_mark_as_stable.BestEBuild(sticky_ebuilds).chrome_version |
| 119 |
| 120 |
| 121 class ChromeEBuild(cros_mark_as_stable.EBuild): |
| 122 """Thin sub-class of EBuild that adds a chrome_version field.""" |
| 123 chrome_version_re = re.compile('.*chromeos-chrome-(%s|9999).*' % ( |
| 124 _CHROME_VERSION_REGEX)) |
| 125 chrome_version = '' |
| 126 |
| 127 def __init__(self, path): |
| 128 cros_mark_as_stable.EBuild.__init__(self, path) |
| 129 re_match = self.chrome_version_re.match(self.ebuild_path_no_revision) |
| 130 if re_match: |
| 131 self.chrome_version = re_match.group(1) |
| 132 |
| 133 def __cmp__(self, other): |
| 134 """Use ebuild paths for comparison.""" |
| 135 if self.ebuild_path == other.ebuild_path: |
| 136 return 0 |
| 137 elif self.ebuild_path > other.ebuild_path: |
| 138 return 1 |
| 139 else: |
| 140 return (-1) |
| 141 |
| 142 |
| 143 def FindChromeCandidates(overlay_dir): |
| 144 """Return a tuple of chrome's unstable ebuild and stable ebuilds. |
| 145 |
| 146 Args: |
| 147 overlay_dir: The path to chrome's portage overlay dir. |
| 148 Returns: |
| 149 Tuple [unstable_ebuild, stable_ebuilds]. |
| 150 Raises: |
| 151 Exception: if no unstable ebuild exists for Chrome. |
| 152 """ |
| 153 stable_ebuilds = [] |
| 154 unstable_ebuilds = [] |
| 155 for path in [ |
| 156 os.path.join(overlay_dir, entry) for entry in os.listdir(overlay_dir)]: |
| 157 if path.endswith('.ebuild'): |
| 158 ebuild = ChromeEBuild(path) |
| 159 if not ebuild.chrome_version: |
| 160 Warning('Poorly formatted ebuild found at %s' % path) |
| 161 else: |
| 162 if not ebuild.is_stable: |
| 163 unstable_ebuilds.append(ebuild) |
| 164 else: |
| 165 stable_ebuilds.append(ebuild) |
| 166 |
| 167 # Apply some sanity checks. |
| 168 if not unstable_ebuilds: |
| 169 raise Exception('Missing 9999 ebuild for %s' % overlay_dir) |
| 170 if not stable_ebuilds: |
| 171 Warning('Missing stable ebuild for %s' % overlay_dir) |
| 172 |
| 173 return cros_mark_as_stable.BestEBuild(unstable_ebuilds), stable_ebuilds |
| 174 |
| 175 |
| 176 def FindChromeUprevCandidate(stable_ebuilds, chrome_rev, sticky_branch): |
| 177 """Finds the Chrome uprev candidate for the given chrome_rev. |
| 178 |
| 179 Using the pre-flight logic, this means the stable ebuild you are uprevving |
| 180 from. The difference here is that the version could be different and in |
| 181 that case we want to find it to delete it. |
| 182 |
| 183 Args: |
| 184 stable_ebuilds: A list of stable ebuilds. |
| 185 chrome_rev: The chrome_rev designating which candidate to find. |
| 186 sticky_branch: The the branch that is currently sticky with Major/Minor |
| 187 components. For example: 9.0.553 |
| 188 Returns: |
| 189 Returns the EBuild, otherwise None if none found. |
| 190 """ |
| 191 candidates = [] |
| 192 if chrome_rev == TIP_OF_TRUNK: |
| 193 chrome_branch_re = re.compile('%s.*_alpha.*' % _CHROME_VERSION_REGEX) |
| 194 for ebuild in stable_ebuilds: |
| 195 if chrome_branch_re.search(ebuild.version): |
| 196 candidates.append(ebuild) |
| 197 |
| 198 elif chrome_rev == STICKY: |
| 199 chrome_branch_re = re.compile('%s\.\d+.*_rc.*' % sticky_branch) |
| 200 for ebuild in stable_ebuilds: |
| 201 if chrome_branch_re.search(ebuild.version): |
| 202 candidates.append(ebuild) |
| 203 |
| 204 else: |
| 205 chrome_branch_re = re.compile('%s.*_rc.*' % _CHROME_VERSION_REGEX) |
| 206 for ebuild in stable_ebuilds: |
| 207 if chrome_branch_re.search(ebuild.version) and ( |
| 208 not ebuild.chrome_version.startswith(sticky_branch)): |
| 209 candidates.append(ebuild) |
| 210 |
| 211 if candidates: |
| 212 return cros_mark_as_stable.BestEBuild(candidates) |
| 213 else: |
| 214 return None |
| 215 |
| 216 |
| 217 def MarkChromeEBuildAsStable(stable_candidate, unstable_ebuild, chrome_rev, |
| 218 chrome_version, commit, overlay_dir): |
| 219 """Uprevs the chrome ebuild specified by chrome_rev. |
| 220 |
| 221 This is the main function that uprevs the chrome_rev from a stable candidate |
| 222 to its new version. |
| 223 |
| 224 Args: |
| 225 stable_candidate: ebuild that corresponds to the stable ebuild we are |
| 226 revving from. If None, builds the a new ebuild given the version |
| 227 and logic for chrome_rev type with revision set to 1. |
| 228 unstable_ebuild: ebuild corresponding to the unstable ebuild for chrome. |
| 229 chrome_rev: one of CHROME_REV |
| 230 TIP_OF_TRUNK - Requires commit value. Revs the ebuild for the TOT |
| 231 version and uses the portage suffix of _alpha. |
| 232 LATEST_RELEASE - This uses the portage suffix of _rc as they are release |
| 233 candidates for the next sticky version. |
| 234 STICKY - Revs the sticky version. |
| 235 chrome_version: The \d.\d.\d.\d version of Chrome. |
| 236 commit: Used with TIP_OF_TRUNK. The svn revision of chrome. |
| 237 overlay_dir: Path to the chromeos-chrome package dir. |
| 238 """ |
| 239 base_path = os.path.join(overlay_dir, 'chromeos-chrome-%s' % chrome_version) |
| 240 # Case where we have the last stable candidate with same version just rev. |
| 241 if stable_candidate and stable_candidate.chrome_version == chrome_version: |
| 242 new_ebuild_path = '%s-r%d.ebuild' % ( |
| 243 stable_candidate.ebuild_path_no_revision, |
| 244 stable_candidate.current_revision + 1) |
| 245 else: |
| 246 if chrome_rev == TIP_OF_TRUNK: |
| 247 portage_suffix = '_alpha' |
| 248 else: |
| 249 portage_suffix = '_rc' |
| 250 |
| 251 new_ebuild_path = base_path + ('%s-r1.ebuild' % portage_suffix) |
| 252 |
| 253 cros_mark_as_stable.EBuildStableMarker.MarkAsStable( |
| 254 unstable_ebuild.ebuild_path, new_ebuild_path, 'CROS_SVN_COMMIT', commit) |
| 255 RunCommand(['git', 'add', new_ebuild_path]) |
| 256 if stable_candidate: |
| 257 RunCommand(['git', 'rm', stable_candidate.ebuild_path]) |
| 258 |
| 259 cros_mark_as_stable.EBuildStableMarker.CommitChange( |
| 260 _GIT_COMMIT_MESSAGE % {'chrome_rev': chrome_rev, |
| 261 'chrome_version': chrome_version}) |
| 262 |
| 263 |
| 264 def main(argv): |
| 265 usage = '%s OPTIONS commit|clean|push' |
| 266 parser = optparse.OptionParser(usage) |
| 267 parser.add_option('-c', '--chrome_rev', default=None, |
| 268 help='One of %s' % CHROME_REV) |
| 269 parser.add_option('-s', '--srcroot', default='.', |
| 270 help='Path to the src directory') |
| 271 parser.add_option('-t', '--tracking_branch', default='cros/master', |
| 272 help='Branch we are tracking changes against') |
| 273 (options, argv) = parser.parse_args(argv) |
| 274 |
| 275 if len(argv) != 2 or argv[1] not in ( |
| 276 cros_mark_as_stable.COMMAND_DICTIONARY.keys()): |
| 277 parser.error('Arguments are invalid, see usage.') |
| 278 |
| 279 command = argv[1] |
| 280 overlay_dir = os.path.abspath(_CHROME_OVERLAY_DIR % |
| 281 {'srcroot': options.srcroot}) |
| 282 |
| 283 os.chdir(overlay_dir) |
| 284 if command == 'clean': |
| 285 cros_mark_as_stable.Clean(options.tracking_branch) |
| 286 return |
| 287 elif command == 'push': |
| 288 cros_mark_as_stable.PushChange(_STABLE_BRANCH_NAME, options.tracking_branch) |
| 289 return |
| 290 |
| 291 if not options.chrome_rev or options.chrome_rev not in CHROME_REV: |
| 292 parser.error('Commit requires type set to one of %s.' % CHROME_REV) |
| 293 |
| 294 chrome_rev = options.chrome_rev |
| 295 version_to_uprev = None |
| 296 commit_to_use = None |
| 297 |
| 298 (unstable_ebuild, stable_ebuilds) = FindChromeCandidates(overlay_dir) |
| 299 sticky_version = _GetStickyVersion(stable_ebuilds) |
| 300 sticky_branch = sticky_version.rpartition('.')[0] |
| 301 |
| 302 if chrome_rev == TIP_OF_TRUNK: |
| 303 version_to_uprev = _GetTipOfTrunkVersion() |
| 304 commit_to_use = _GetTipOfTrunkSvnRevision() |
| 305 elif chrome_rev == LATEST_RELEASE: |
| 306 version_to_uprev = _GetLatestRelease() |
| 307 else: |
| 308 version_to_uprev = _GetLatestRelease(sticky_branch) |
| 309 |
| 310 stable_candidate = FindChromeUprevCandidate(stable_ebuilds, chrome_rev, |
| 311 sticky_branch) |
| 312 # There are some cases we don't need to do anything. Check for them. |
| 313 if stable_candidate and (version_to_uprev == stable_candidate.chrome_version |
| 314 and not commit_to_use): |
| 315 Info('Found nothing to do for chrome_rev %s with version %s.' % ( |
| 316 chrome_rev, version_to_uprev)) |
| 317 else: |
| 318 work_branch = cros_mark_as_stable.GitBranch( |
| 319 _STABLE_BRANCH_NAME, options.tracking_branch) |
| 320 work_branch.CreateBranch() |
| 321 try: |
| 322 MarkChromeEBuildAsStable(stable_candidate, unstable_ebuild, chrome_rev, |
| 323 version_to_uprev, commit_to_use, overlay_dir) |
| 324 except: |
| 325 work_branch.Delete() |
| 326 raise |
| 327 |
| 328 |
| 329 if __name__ == '__main__': |
| 330 main(sys.argv) |
OLD | NEW |