blob: 8b305d7a513d6678c8b790cf2d3322c894388275 [file] [log] [blame]
Chris Sosadad0d322011-01-31 16:37:33 -08001#!/usr/bin/python
2
Chris Sosa9ba47752012-02-27 15:27:37 -08003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosadad0d322011-01-31 16:37:33 -08004# 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
9After calling, it prints outs CHROME_VERSION_ATOM=(version atom string). A
10caller could then use this atom with emerge to build the newly uprevved version
11of Chrome e.g.
12
13./cros_mark_chrome_as_stable tot
14Returns chrome-base/chromeos-chrome-8.0.552.0_alpha_r1
15
16emerge-x86-generic =chrome-base/chromeos-chrome-8.0.552.0_alpha_r1
17"""
18
Chris Sosa8be39132011-04-14 12:09:24 -070019import filecmp
Chris Sosadad0d322011-01-31 16:37:33 -080020import optparse
21import os
22import re
Chris Masone592cab52011-08-02 14:05:48 -070023import sys
David James20af1962012-08-02 14:02:44 -070024import socket
petermayo@chromium.org163b3372011-09-12 02:06:05 -040025import time
Chris Sosadad0d322011-01-31 16:37:33 -080026
Mike Frysinger6cb624a2012-05-24 18:17:38 -040027from chromite.buildbot import constants
J. Richard Barnettef6697cf2011-11-18 12:42:08 -080028from chromite.buildbot import portage_utilities
Brian Harring1b8c4c82012-05-29 23:03:04 -070029from chromite.lib import cros_build_lib
J. Richard Barnetted422f622011-11-17 09:39:46 -080030from chromite.lib.cros_build_lib import RunCommand, Info, Warning
Mike Frysinger6cb624a2012-05-24 18:17:38 -040031from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080032
David James20af1962012-08-02 14:02:44 -070033if socket.getfqdn().endswith('.golo.chromium.org'):
34 BASE_CHROME_SVN_URL = 'svn://svn-mirror.golo.chromium.org/chrome'
35else:
36 BASE_CHROME_SVN_URL = 'http://src.chromium.org/svn'
Chris Sosadad0d322011-01-31 16:37:33 -080037
Chris Sosadad0d322011-01-31 16:37:33 -080038# Helper regex's for finding ebuilds.
39_CHROME_VERSION_REGEX = '\d+\.\d+\.\d+\.\d+'
40_NON_STICKY_REGEX = '%s[(_rc.*)|(_alpha.*)]+' % _CHROME_VERSION_REGEX
41
42# Dir where all the action happens.
43_CHROME_OVERLAY_DIR = ('%(srcroot)s/third_party/chromiumos-overlay'
44 '/chromeos-base/chromeos-chrome')
45
46_GIT_COMMIT_MESSAGE = ('Marking %(chrome_rev)s for chrome ebuild with version '
47 '%(chrome_version)s as stable.')
48
Chris Masone592cab52011-08-02 14:05:48 -070049# URLs that print lists of chrome revisions between two versions of the browser.
50_CHROME_VERSION_URL = ('http://omahaproxy.appspot.com/changelog?'
51 'old_version=%(old)s&new_version=%(new)s')
Chris Sosadd611df2012-02-03 15:26:23 -080052
53# Only print links when we rev these types.
54_REV_TYPES_FOR_LINKS = [constants.CHROME_REV_LATEST,
55 constants.CHROME_REV_STICKY]
Chris Masone592cab52011-08-02 14:05:48 -070056
57_CHROME_SVN_TAG = 'CROS_SVN_COMMIT'
Chris Sosadad0d322011-01-31 16:37:33 -080058
Peter Mayof65b8412011-08-26 01:22:21 -040059def _GetSvnUrl(base_url):
Chris Sosadad0d322011-01-31 16:37:33 -080060 """Returns the path to the svn url for the given chrome branch."""
Peter Mayof65b8412011-08-26 01:22:21 -040061 return os.path.join(base_url, 'trunk')
Chris Sosadad0d322011-01-31 16:37:33 -080062
63
Peter Mayof65b8412011-08-26 01:22:21 -040064def _GetTipOfTrunkSvnRevision(base_url):
Chris Sosadad0d322011-01-31 16:37:33 -080065 """Returns the current svn revision for the chrome tree."""
Peter Mayof65b8412011-08-26 01:22:21 -040066 svn_url = _GetSvnUrl(base_url)
J. Richard Barnetted422f622011-11-17 09:39:46 -080067 svn_info = RunCommand(['svn', 'info', svn_url], redirect_stdout=True).output
Chris Sosadad0d322011-01-31 16:37:33 -080068
69 revision_re = re.compile('^Revision:\s+(\d+).*')
70 for line in svn_info.splitlines():
71 match = revision_re.search(line)
72 if match:
73 svn_revision = match.group(1)
petermayo@chromium.org163b3372011-09-12 02:06:05 -040074 Info('Found SVN Revision %s' % svn_revision)
Chris Sosadad0d322011-01-31 16:37:33 -080075 return svn_revision
76
77 raise Exception('Could not find revision information from %s' % svn_url)
78
79
petermayo@chromium.org163b3372011-09-12 02:06:05 -040080def _GetVersionContents(chrome_version_info):
Peter Mayo177500f2011-09-09 17:25:23 -040081 """Returns the current Chromium version, from the contents of a VERSION file.
Chris Sosadad0d322011-01-31 16:37:33 -080082
Peter Mayo177500f2011-09-09 17:25:23 -040083 Args:
84 chrome_version_info: The contents of a chromium VERSION file.
85 """
Chris Sosadad0d322011-01-31 16:37:33 -080086 chrome_version_array = []
Chris Sosadad0d322011-01-31 16:37:33 -080087 for line in chrome_version_info.splitlines():
88 chrome_version_array.append(line.rpartition('=')[2])
89
90 return '.'.join(chrome_version_array)
91
petermayo@chromium.org163b3372011-09-12 02:06:05 -040092def _GetSpecificVersionUrl(base_url, revision, time_to_wait=600):
93 """Returns the Chromium version, from a repository URL and version.
Peter Mayo177500f2011-09-09 17:25:23 -040094
95 Args:
96 base_url: URL for the root of the chromium checkout.
petermayo@chromium.org163b3372011-09-12 02:06:05 -040097 revision: the SVN revision we want to use.
98 time_to_wait: the minimum period before abandoning our wait for the
99 desired revision to be present.
Peter Mayo177500f2011-09-09 17:25:23 -0400100 """
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400101 svn_url = os.path.join(_GetSvnUrl(base_url), 'src', 'chrome', 'VERSION')
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400102 if not revision or not (int(revision) > 0):
103 raise Exception('Revision must be positive, got %s' % revision)
104
105 start = time.time()
106 # Use the fact we are SVN, hence ordered.
107 # Dodge the fact it will silently ignore the revision if it is not
108 # yet known. (i.e. too high)
109 repo_version = _GetTipOfTrunkSvnRevision(base_url)
110 while revision > repo_version:
111 if time.time() - start > time_to_wait:
112 raise Exception('Timeout Exceeeded')
113
114 Info('Repository only has version %s, looking for %s. Sleeping...' %
115 (repo_version, revision))
116 time.sleep(30)
117 repo_version = _GetTipOfTrunkSvnRevision(base_url)
118
119 chrome_version_info = RunCommand(
120 ['svn', 'cat', '-r', revision, svn_url],
121 redirect_stdout=True,
122 error_message='Could not read version file at %s revision %s.' %
J. Richard Barnetted422f622011-11-17 09:39:46 -0800123 (svn_url, revision)).output
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400124
125 return _GetVersionContents(chrome_version_info)
126
Peter Mayo177500f2011-09-09 17:25:23 -0400127
128def _GetTipOfTrunkVersionFile(root):
129 """Returns the current Chromium version, from a file in a checkout.
130
131 Args:
132 root: path to the root of the chromium checkout.
133 """
134 version_file = os.path.join(root, 'src', 'chrome', 'VERSION')
135 chrome_version_info = RunCommand(
136 ['cat', version_file],
137 redirect_stdout=True,
J. Richard Barnetted422f622011-11-17 09:39:46 -0800138 error_message='Could not read version file at %s.' % version_file).output
Peter Mayo177500f2011-09-09 17:25:23 -0400139
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400140 return _GetVersionContents(chrome_version_info)
Peter Mayo177500f2011-09-09 17:25:23 -0400141
Peter Mayof65b8412011-08-26 01:22:21 -0400142def _GetLatestRelease(base_url, branch=None):
Chris Sosadad0d322011-01-31 16:37:33 -0800143 """Gets the latest release version from the buildspec_url for the branch.
144
145 Args:
146 branch: If set, gets the latest release for branch, otherwise latest
147 release.
148 Returns:
149 Latest version string.
150 """
Peter Mayof65b8412011-08-26 01:22:21 -0400151 buildspec_url = os.path.join(base_url, 'releases')
J. Richard Barnetted422f622011-11-17 09:39:46 -0800152 svn_ls = RunCommand(['svn', 'ls', buildspec_url],
153 redirect_stdout=True).output
Peter Mayoad8173d2011-11-08 16:18:23 -0500154 sorted_ls = RunCommand(['sort', '--version-sort', '-r'], input=svn_ls,
J. Richard Barnetted422f622011-11-17 09:39:46 -0800155 redirect_stdout=True).output
Chris Sosadad0d322011-01-31 16:37:33 -0800156 if branch:
157 chrome_version_re = re.compile('^%s\.\d+.*' % branch)
158 else:
159 chrome_version_re = re.compile('^[0-9]+\..*')
Chris Sosadad0d322011-01-31 16:37:33 -0800160
Peter Mayoad8173d2011-11-08 16:18:23 -0500161 for chrome_version in sorted_ls.splitlines():
J. Richard Barnetted422f622011-11-17 09:39:46 -0800162 if chrome_version_re.match(chrome_version):
163 deps_url = os.path.join(buildspec_url, chrome_version, 'DEPS')
164 deps_check = RunCommand(['svn', 'ls', deps_url],
Brian Harringfa310772012-09-22 21:38:39 -0700165 error_code_ok=True,
J. Richard Barnetted422f622011-11-17 09:39:46 -0800166 redirect_stdout=True).output
167 if deps_check == 'DEPS\n':
168 return chrome_version.rstrip('/')
Peter Mayoad8173d2011-11-08 16:18:23 -0500169
170 return None
Chris Sosadad0d322011-01-31 16:37:33 -0800171
172
173def _GetStickyEBuild(stable_ebuilds):
174 """Returns the sticky ebuild."""
175 sticky_ebuilds = []
176 non_sticky_re = re.compile(_NON_STICKY_REGEX)
177 for ebuild in stable_ebuilds:
178 if not non_sticky_re.match(ebuild.version):
179 sticky_ebuilds.append(ebuild)
180
181 if not sticky_ebuilds:
182 raise Exception('No sticky ebuilds found')
183 elif len(sticky_ebuilds) > 1:
184 Warning('More than one sticky ebuild found')
185
J. Richard Barnettef6697cf2011-11-18 12:42:08 -0800186 return portage_utilities.BestEBuild(sticky_ebuilds)
Chris Sosadad0d322011-01-31 16:37:33 -0800187
188
J. Richard Barnettef6697cf2011-11-18 12:42:08 -0800189class ChromeEBuild(portage_utilities.EBuild):
Chris Sosadad0d322011-01-31 16:37:33 -0800190 """Thin sub-class of EBuild that adds a chrome_version field."""
191 chrome_version_re = re.compile('.*chromeos-chrome-(%s|9999).*' % (
192 _CHROME_VERSION_REGEX))
193 chrome_version = ''
194
195 def __init__(self, path):
J. Richard Barnettef6697cf2011-11-18 12:42:08 -0800196 portage_utilities.EBuild.__init__(self, path)
Chris Sosadad0d322011-01-31 16:37:33 -0800197 re_match = self.chrome_version_re.match(self.ebuild_path_no_revision)
198 if re_match:
199 self.chrome_version = re_match.group(1)
200
Chris Sosadad0d322011-01-31 16:37:33 -0800201 def __str__(self):
202 return self.ebuild_path
203
204
205def FindChromeCandidates(overlay_dir):
206 """Return a tuple of chrome's unstable ebuild and stable ebuilds.
207
208 Args:
209 overlay_dir: The path to chrome's portage overlay dir.
210 Returns:
211 Tuple [unstable_ebuild, stable_ebuilds].
212 Raises:
213 Exception: if no unstable ebuild exists for Chrome.
214 """
215 stable_ebuilds = []
216 unstable_ebuilds = []
217 for path in [
218 os.path.join(overlay_dir, entry) for entry in os.listdir(overlay_dir)]:
219 if path.endswith('.ebuild'):
220 ebuild = ChromeEBuild(path)
221 if not ebuild.chrome_version:
222 Warning('Poorly formatted ebuild found at %s' % path)
223 else:
224 if '9999' in ebuild.version:
225 unstable_ebuilds.append(ebuild)
226 else:
227 stable_ebuilds.append(ebuild)
228
229 # Apply some sanity checks.
230 if not unstable_ebuilds:
231 raise Exception('Missing 9999 ebuild for %s' % overlay_dir)
232 if not stable_ebuilds:
233 Warning('Missing stable ebuild for %s' % overlay_dir)
234
J. Richard Barnettef6697cf2011-11-18 12:42:08 -0800235 return portage_utilities.BestEBuild(unstable_ebuilds), stable_ebuilds
Chris Sosadad0d322011-01-31 16:37:33 -0800236
237
238def FindChromeUprevCandidate(stable_ebuilds, chrome_rev, sticky_branch):
239 """Finds the Chrome uprev candidate for the given chrome_rev.
240
241 Using the pre-flight logic, this means the stable ebuild you are uprevving
242 from. The difference here is that the version could be different and in
243 that case we want to find it to delete it.
244
245 Args:
246 stable_ebuilds: A list of stable ebuilds.
247 chrome_rev: The chrome_rev designating which candidate to find.
248 sticky_branch: The the branch that is currently sticky with Major/Minor
Chris Sosa9ba47752012-02-27 15:27:37 -0800249 components. For example: 9.0.553. Can be None but not if chrome_rev
250 is CHROME_REV_STICKY.
Chris Sosadad0d322011-01-31 16:37:33 -0800251 Returns:
252 Returns the EBuild, otherwise None if none found.
253 """
254 candidates = []
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400255 if chrome_rev in [constants.CHROME_REV_LOCAL, constants.CHROME_REV_TOT,
256 constants.CHROME_REV_SPEC]:
257 # These are labelled alpha, for historic reasons,
Peter Mayo177500f2011-09-09 17:25:23 -0400258 # not just for the fun of confusion.
Chris Sosadad0d322011-01-31 16:37:33 -0800259 chrome_branch_re = re.compile('%s.*_alpha.*' % _CHROME_VERSION_REGEX)
260 for ebuild in stable_ebuilds:
261 if chrome_branch_re.search(ebuild.version):
262 candidates.append(ebuild)
263
Ryan Cuic6e097d2011-06-16 12:23:14 -0700264 elif chrome_rev == constants.CHROME_REV_STICKY:
Chris Sosa9ba47752012-02-27 15:27:37 -0800265 assert sticky_branch is not None
Chris Sosadad0d322011-01-31 16:37:33 -0800266 chrome_branch_re = re.compile('%s\..*' % sticky_branch)
267 for ebuild in stable_ebuilds:
268 if chrome_branch_re.search(ebuild.version):
269 candidates.append(ebuild)
270
271 else:
272 chrome_branch_re = re.compile('%s.*_rc.*' % _CHROME_VERSION_REGEX)
273 for ebuild in stable_ebuilds:
Chris Sosa9ba47752012-02-27 15:27:37 -0800274 if chrome_branch_re.search(ebuild.version):
Chris Sosadad0d322011-01-31 16:37:33 -0800275 candidates.append(ebuild)
276
277 if candidates:
J. Richard Barnettef6697cf2011-11-18 12:42:08 -0800278 return portage_utilities.BestEBuild(candidates)
Chris Sosadad0d322011-01-31 16:37:33 -0800279 else:
280 return None
281
Chris Masone592cab52011-08-02 14:05:48 -0700282def _AnnotateAndPrint(text, url):
283 """Add buildbot trappings to print <a href='url'>text</a> in the waterfall.
284
285 Args:
286 text: Anchor text for the link
287 url: the URL to which to link
288 """
Don Garrette60de902011-10-17 18:52:05 -0700289 print >> sys.stderr, '\n@@@STEP_LINK@%(text)s@%(url)s@@@' % { 'text': text,
Chris Masone592cab52011-08-02 14:05:48 -0700290 'url': url }
291
292def GetChromeRevisionLinkFromVersions(old_chrome_version, chrome_version):
293 """Return appropriately formatted link to revision info, given versions
294
295 Given two chrome version strings (e.g. 9.0.533.0), generate a link to a
296 page that prints the Chromium revisions between those two versions.
297
298 Args:
299 old_chrome_version: version to diff from
300 chrome_version: version to which to diff
301 Returns:
302 The desired URL.
303 """
304 return _CHROME_VERSION_URL % { 'old': old_chrome_version,
305 'new': chrome_version }
306
Chris Masone592cab52011-08-02 14:05:48 -0700307def GetChromeRevisionListLink(old_chrome, new_chrome, chrome_rev):
308 """Returns a link to the list of revisions between two Chromium versions
309
310 Given two ChromeEBuilds and the kind of rev we're doing, generate a
311 link to a page that prints the Chromium changes between those two
312 revisions, inclusive.
313
314 Args:
315 old_chrome: ebuild for the version to diff from
316 new_chrome: ebuild for the version to which to diff
317 chrome_rev: one of constants.VALID_CHROME_REVISIONS
318 Returns:
319 The desired URL.
320 """
Chris Sosadd611df2012-02-03 15:26:23 -0800321 assert chrome_rev in _REV_TYPES_FOR_LINKS
322 return GetChromeRevisionLinkFromVersions(old_chrome.chrome_version,
323 new_chrome.chrome_version)
Chris Sosadad0d322011-01-31 16:37:33 -0800324
325def MarkChromeEBuildAsStable(stable_candidate, unstable_ebuild, chrome_rev,
Chris Sosa9ba47752012-02-27 15:27:37 -0800326 chrome_version, commit, overlay_dir):
Chris Sosadad0d322011-01-31 16:37:33 -0800327 """Uprevs the chrome ebuild specified by chrome_rev.
328
329 This is the main function that uprevs the chrome_rev from a stable candidate
330 to its new version.
331
332 Args:
333 stable_candidate: ebuild that corresponds to the stable ebuild we are
334 revving from. If None, builds the a new ebuild given the version
335 and logic for chrome_rev type with revision set to 1.
336 unstable_ebuild: ebuild corresponding to the unstable ebuild for chrome.
Peter Mayo177500f2011-09-09 17:25:23 -0400337 chrome_rev: one of constants.VALID_CHROME_REVISIONS or LOCAL
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400338 constants.CHROME_REV_SPEC - Requires commit value. Revs the ebuild for
339 the specified version and uses the portage suffix of _alpha.
Ryan Cuic6e097d2011-06-16 12:23:14 -0700340 constants.CHROME_REV_TOT - Requires commit value. Revs the ebuild for
341 the TOT version and uses the portage suffix of _alpha.
Peter Mayo177500f2011-09-09 17:25:23 -0400342 constants.CHROME_REV_LOCAL - Requires a chrome_root. Revs the ebuild for
343 the local version and uses the portage suffix of _alpha.
Ryan Cuic6e097d2011-06-16 12:23:14 -0700344 constants.CHROME_REV_LATEST - This uses the portage suffix of _rc as they
345 are release candidates for the next sticky version.
346 constants.CHROME_REV_STICKY - Revs the sticky version.
Chris Sosadad0d322011-01-31 16:37:33 -0800347 chrome_version: The \d.\d.\d.\d version of Chrome.
Ryan Cuic6e097d2011-06-16 12:23:14 -0700348 commit: Used with constants.CHROME_REV_TOT. The svn revision of chrome.
Chris Sosadad0d322011-01-31 16:37:33 -0800349 overlay_dir: Path to the chromeos-chrome package dir.
Chris Sosadad0d322011-01-31 16:37:33 -0800350 Returns:
351 Full portage version atom (including rc's, etc) that was revved.
352 """
Chris Sosa8be39132011-04-14 12:09:24 -0700353 def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
354 """Returns True if the new ebuild is redundant.
355
356 This is True if there if the current stable ebuild is the exact same copy
357 of the new one OR the chrome versions are the same and we're revving
Ryan Cuic6e097d2011-06-16 12:23:14 -0700358 constants.CHROME_REV_LATEST (as we don't care about 9999 changes for it).
Chris Sosa8be39132011-04-14 12:09:24 -0700359 """
360 if not stable_ebuild:
361 return False
362
363 if stable_candidate.chrome_version == new_ebuild.chrome_version:
Ryan Cuic6e097d2011-06-16 12:23:14 -0700364 if chrome_rev == constants.CHROME_REV_LATEST:
Chris Sosa8be39132011-04-14 12:09:24 -0700365 return True
366 else:
367 return filecmp.cmp(
368 new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False)
369
Chris Sosadad0d322011-01-31 16:37:33 -0800370 base_path = os.path.join(overlay_dir, 'chromeos-chrome-%s' % chrome_version)
371 # Case where we have the last stable candidate with same version just rev.
372 if stable_candidate and stable_candidate.chrome_version == chrome_version:
373 new_ebuild_path = '%s-r%d.ebuild' % (
374 stable_candidate.ebuild_path_no_revision,
375 stable_candidate.current_revision + 1)
376 else:
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400377 if chrome_rev in [constants.CHROME_REV_LOCAL, constants.CHROME_REV_TOT,
378 constants.CHROME_REV_SPEC]:
Chris Sosadad0d322011-01-31 16:37:33 -0800379 portage_suffix = '_alpha'
380 else:
381 portage_suffix = '_rc'
382
383 new_ebuild_path = base_path + ('%s-r1.ebuild' % portage_suffix)
384
385 # Mark latest release and sticky branches as stable.
Peter Mayo177500f2011-09-09 17:25:23 -0400386 mark_stable = chrome_rev not in [constants.CHROME_REV_TOT,
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400387 constants.CHROME_REV_SPEC,
Peter Mayo177500f2011-09-09 17:25:23 -0400388 constants.CHROME_REV_LOCAL]
Chris Sosadad0d322011-01-31 16:37:33 -0800389
David Jamesa6792552012-04-03 10:05:35 -0700390 chrome_variables = dict()
391 if commit:
392 chrome_variables[_CHROME_SVN_TAG] = commit
393
J. Richard Barnette0cfc5052011-11-28 14:31:39 -0800394 portage_utilities.EBuild.MarkAsStable(
David James065b2012012-04-01 14:51:11 -0700395 unstable_ebuild.ebuild_path, new_ebuild_path,
David Jamesa6792552012-04-03 10:05:35 -0700396 chrome_variables, make_stable=mark_stable)
Chris Sosadad0d322011-01-31 16:37:33 -0800397 new_ebuild = ChromeEBuild(new_ebuild_path)
Chris Sosa8be39132011-04-14 12:09:24 -0700398
399 # Determine whether this is ebuild is redundant.
400 if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
401 Info('Previous ebuild with same version found and ebuild is redundant.')
402 os.unlink(new_ebuild_path)
403 return None
Chris Sosadad0d322011-01-31 16:37:33 -0800404
Chris Sosadd611df2012-02-03 15:26:23 -0800405 if stable_candidate and chrome_rev in _REV_TYPES_FOR_LINKS:
Chris Masone592cab52011-08-02 14:05:48 -0700406 _AnnotateAndPrint('Chromium revisions',
407 GetChromeRevisionListLink(stable_candidate,
408 new_ebuild,
409 chrome_rev))
410
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400411 RunCommand(['git', 'add', new_ebuild_path], cwd=overlay_dir)
Chris Sosa9ba47752012-02-27 15:27:37 -0800412 if stable_candidate and not stable_candidate.IsSticky():
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400413 RunCommand(['git', 'rm', stable_candidate.ebuild_path], cwd=overlay_dir)
Chris Sosadad0d322011-01-31 16:37:33 -0800414
J. Richard Barnette0cfc5052011-11-28 14:31:39 -0800415 portage_utilities.EBuild.CommitChange(
Chris Sosadad0d322011-01-31 16:37:33 -0800416 _GIT_COMMIT_MESSAGE % {'chrome_rev': chrome_rev,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400417 'chrome_version': chrome_version},
418 overlay_dir)
Chris Sosadad0d322011-01-31 16:37:33 -0800419
Chris Sosadad0d322011-01-31 16:37:33 -0800420 return '%s-%s' % (new_ebuild.package, new_ebuild.version)
421
422
Chris Sosad53f8fd2011-10-21 15:15:19 -0700423def ParseMaxRevision(revision_list):
424 """Returns the max revision from a list of url@revision string."""
425 revision_re = re.compile('.*@(\d+)')
426
427 def RevisionKey(revision):
428 return revision_re.match(revision).group(1)
429
430 max_revision = max(revision_list.split(), key=RevisionKey)
431 return max_revision.rpartition('@')[2]
432
433
Mike Frysinger368bbb72012-05-23 15:57:10 -0400434def main(argv):
Chris Sosa0f295aa2011-10-21 14:10:28 -0700435 usage_options = '|'.join(constants.VALID_CHROME_REVISIONS)
Ryan Cuic6e097d2011-06-16 12:23:14 -0700436 usage = '%s OPTIONS [%s]' % (__file__, usage_options)
Chris Sosadad0d322011-01-31 16:37:33 -0800437 parser = optparse.OptionParser(usage)
David Jamescc09c9b2012-01-26 22:10:13 -0800438 parser.add_option('-b', '--boards', default='x86-generic')
Peter Mayof65b8412011-08-26 01:22:21 -0400439 parser.add_option('-c', '--chrome_url', default=BASE_CHROME_SVN_URL)
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400440 parser.add_option('-f', '--force_revision', default=None)
Chris Sosadad0d322011-01-31 16:37:33 -0800441 parser.add_option('-s', '--srcroot', default=os.path.join(os.environ['HOME'],
442 'trunk', 'src'),
443 help='Path to the src directory')
444 parser.add_option('-t', '--tracking_branch', default='cros/master',
445 help='Branch we are tracking changes against')
446 (options, args) = parser.parse_args()
447
Ryan Cuic6e097d2011-06-16 12:23:14 -0700448 if len(args) != 1 or args[0] not in constants.VALID_CHROME_REVISIONS:
449 parser.error('Commit requires arg set to one of %s.'
450 % constants.VALID_CHROME_REVISIONS)
Chris Sosadad0d322011-01-31 16:37:33 -0800451
452 overlay_dir = os.path.abspath(_CHROME_OVERLAY_DIR %
453 {'srcroot': options.srcroot})
454 chrome_rev = args[0]
455 version_to_uprev = None
456 commit_to_use = None
Chris Sosa9ba47752012-02-27 15:27:37 -0800457 sticky_branch = None
Chris Sosadad0d322011-01-31 16:37:33 -0800458
459 (unstable_ebuild, stable_ebuilds) = FindChromeCandidates(overlay_dir)
Chris Sosadad0d322011-01-31 16:37:33 -0800460
Peter Mayo177500f2011-09-09 17:25:23 -0400461 if chrome_rev == constants.CHROME_REV_LOCAL:
462 if 'CHROME_ROOT' in os.environ:
463 chrome_root = os.environ['CHROME_ROOT']
464 else:
465 chrome_root = os.path.join(os.environ['HOME'], 'chrome_root')
466
467 version_to_uprev = _GetTipOfTrunkVersionFile(chrome_root)
468 commit_to_use = 'Unknown'
469 Info('Using local source, versioning is untrustworthy.')
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400470 elif chrome_rev == constants.CHROME_REV_SPEC:
471 commit_to_use = options.force_revision
Chris Sosad53f8fd2011-10-21 15:15:19 -0700472 if '@' in commit_to_use: commit_to_use = ParseMaxRevision(commit_to_use)
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400473 version_to_uprev = _GetSpecificVersionUrl(options.chrome_url,
474 commit_to_use)
Peter Mayo177500f2011-09-09 17:25:23 -0400475 elif chrome_rev == constants.CHROME_REV_TOT:
Peter Mayof65b8412011-08-26 01:22:21 -0400476 commit_to_use = _GetTipOfTrunkSvnRevision(options.chrome_url)
petermayo@chromium.org163b3372011-09-12 02:06:05 -0400477 version_to_uprev = _GetSpecificVersionUrl(options.chrome_url,
478 commit_to_use)
Ryan Cuic6e097d2011-06-16 12:23:14 -0700479 elif chrome_rev == constants.CHROME_REV_LATEST:
Peter Mayof65b8412011-08-26 01:22:21 -0400480 version_to_uprev = _GetLatestRelease(options.chrome_url)
Chris Sosadad0d322011-01-31 16:37:33 -0800481 else:
Chris Sosa9ba47752012-02-27 15:27:37 -0800482 sticky_ebuild = _GetStickyEBuild(stable_ebuilds)
483 sticky_version = sticky_ebuild.chrome_version
484 sticky_branch = sticky_version.rpartition('.')[0]
Peter Mayof65b8412011-08-26 01:22:21 -0400485 version_to_uprev = _GetLatestRelease(options.chrome_url, sticky_branch)
Chris Sosadad0d322011-01-31 16:37:33 -0800486
487 stable_candidate = FindChromeUprevCandidate(stable_ebuilds, chrome_rev,
488 sticky_branch)
489
490 if stable_candidate:
491 Info('Stable candidate found %s' % stable_candidate)
492 else:
493 Info('No stable candidate found.')
494
Chris Sosa8049f3b2011-05-02 20:09:28 -0700495 tracking_branch = 'remotes/m/%s' % os.path.basename(options.tracking_branch)
Brian Harring1b8c4c82012-05-29 23:03:04 -0700496 existing_branch = cros_build_lib.GetCurrentBranch(overlay_dir)
Ryan Cui05a31ba2011-05-31 17:47:37 -0700497 work_branch = cros_mark_as_stable.GitBranch(constants.STABLE_EBUILD_BRANCH,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400498 tracking_branch, overlay_dir)
Chris Sosadad0d322011-01-31 16:37:33 -0800499 work_branch.CreateBranch()
David Jamesd6708c02012-03-22 10:49:15 -0700500
501 # In the case of uprevving overlays that have patches applied to them,
502 # include the patched changes in the stabilizing branch.
503 if existing_branch:
504 RunCommand(['git', 'rebase', existing_branch], cwd=overlay_dir)
505
Chris Sosadad0d322011-01-31 16:37:33 -0800506 chrome_version_atom = MarkChromeEBuildAsStable(
507 stable_candidate, unstable_ebuild, chrome_rev, version_to_uprev,
Chris Sosa9ba47752012-02-27 15:27:37 -0800508 commit_to_use, overlay_dir)
Chris Sosadad0d322011-01-31 16:37:33 -0800509 # Explicit print to communicate to caller.
510 if chrome_version_atom:
David Jamescc09c9b2012-01-26 22:10:13 -0800511 cros_mark_as_stable.CleanStalePackages(options.boards.split(':'),
512 [chrome_version_atom])
Chris Sosadad0d322011-01-31 16:37:33 -0800513 print 'CHROME_VERSION_ATOM=%s' % chrome_version_atom