blob: 1496e00503ba410fcd4ed31cd8aa437bfd8f3441 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07002# Copyright 2017 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
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -07006"""Update the CHROMEOS_LKGM file in a chromium repository."""
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07007
8from __future__ import print_function
9
Mike Frysingera94f1252019-09-12 02:39:28 -040010import distutils.version # pylint: disable=import-error,no-name-in-module
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070011import os
12
Steven Bennetts4bc322a2017-08-28 09:37:39 -070013from chromite.cbuildbot import manifest_version
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080014from chromite.lib import chrome_committer
Achuith Bhandarkarf561c152018-08-27 19:05:39 -070015from chromite.lib import commandline
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070016from chromite.lib import constants
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070017from chromite.lib import cros_logging as logging
Ben Pastene5a40b102019-11-22 17:06:31 -080018from chromite.lib import gerrit
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070019from chromite.lib import osutils
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070020
21
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080022class LKGMNotValid(chrome_committer.CommitError):
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070023 """Raised if the LKGM version is unset or not newer than the current value."""
24
25
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080026class LKGMFileNotFound(chrome_committer.CommitError):
27 """Raised if the LKGM file is not found."""
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070028
29
Achuith Bhandarkar1b9180f2018-02-22 19:12:09 -080030class ChromeLKGMCommitter(object):
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070031 """Committer object responsible for obtaining a new LKGM and committing it."""
32
Ben Pastened3b93d42019-10-10 09:56:29 -070033 # The list of trybots we require LKGM updates to run and pass on before
34 # landing. Since they're internal trybots, the CQ won't automatically trigger
35 # them, so we have to explicitly tell it to.
36 _PRESUBMIT_BOTS = [
37 'chromeos-betty-chrome',
38 'chromeos-betty-pi-arc-chrome',
39 'chromeos-eve-compile-chrome',
40 'chromeos-kevin-compile-chrome',
41 ]
Ben Pastene86723462018-04-26 11:07:27 -070042 # Files needed in a local checkout to successfully update the LKGM. The OWNERS
43 # file allows the --tbr-owners mechanism to select an appropriate OWNER to
Jun Mukai599dad12019-01-02 16:43:10 -080044 # TBR. TRANSLATION_OWNERS is necesssary to parse CHROMEOS_OWNERS file since
45 # it has the reference.
Ben Pastene86723462018-04-26 11:07:27 -070046 _NEEDED_FILES = [
47 constants.PATH_TO_CHROME_CHROMEOS_OWNERS,
48 constants.PATH_TO_CHROME_LKGM,
Jun Mukai599dad12019-01-02 16:43:10 -080049 'tools/translation/TRANSLATION_OWNERS',
Ben Pastene86723462018-04-26 11:07:27 -070050 ]
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070051
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070052 def __init__(self, args):
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080053 self._committer = chrome_committer.ChromeCommitter(args)
54
Steven Bennetts4bc322a2017-08-28 09:37:39 -070055 # Strip any chrome branch from the lkgm version.
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070056 self._lkgm = manifest_version.VersionInfo(args.lkgm).VersionString()
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070057 self._old_lkgm = None
58
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070059 if not self._lkgm:
60 raise LKGMNotValid('LKGM not provided.')
61
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080062 logging.info('lkgm=%s', self._lkgm)
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070063
64 def Run(self):
Ben Pastene5a40b102019-11-22 17:06:31 -080065 self.CloseOldLKGMRolls()
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080066 self._committer.Cleanup()
Ben Pastene86723462018-04-26 11:07:27 -070067 self._committer.Checkout(self._NEEDED_FILES)
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070068 self.UpdateLKGM()
69 self.CommitNewLKGM()
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080070 self._committer.Upload()
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070071
72 def CheckoutChrome(self):
73 """Checks out chrome into tmp checkout_dir."""
Ben Pastene86723462018-04-26 11:07:27 -070074 self._committer.Checkout(self._NEEDED_FILES)
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070075
76 @property
77 def lkgm_file(self):
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080078 return self._committer.FullPath(constants.PATH_TO_CHROME_LKGM)
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070079
Ben Pastene5a40b102019-11-22 17:06:31 -080080 def CloseOldLKGMRolls(self):
81 """Closes all open LKGM roll CLs that were last modified >24 hours ago.
82
83 Any roll that hasn't passed the CQ in 24 hours is likely broken and can be
84 discarded.
85 """
86 query_params = {
87 'project': constants.CHROMIUM_SRC_PROJECT,
88 'branch': 'master',
89 'author': self._committer.author,
90 'file': constants.PATH_TO_CHROME_LKGM,
91 'age': '1d',
92 'status': 'open',
93 }
94 gerrit_helper = gerrit.GetCrosExternal()
95 for open_issue in gerrit_helper.Query(**query_params):
96 logging.info(
97 'Closing old LKGM roll crrev.com/c/%s', open_issue.gerrit_number)
98 gerrit_helper.AbandonChange(open_issue)
99
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700100 def UpdateLKGM(self):
101 """Updates the LKGM file with the new version."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -0800102 lkgm_file = self.lkgm_file
103 if not os.path.exists(lkgm_file):
104 raise LKGMFileNotFound('%s is an invalid file' % lkgm_file)
105
106 self._old_lkgm = osutils.ReadFile(lkgm_file)
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700107
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700108 lv = distutils.version.LooseVersion
Mike Frysinger266e4ff2018-07-14 00:41:05 -0400109 if self._old_lkgm is not None and lv(self._lkgm) <= lv(self._old_lkgm):
Achuith Bhandarkar1b9180f2018-02-22 19:12:09 -0800110 raise LKGMNotValid(
111 'LKGM version (%s) is not newer than current version (%s).' %
112 (self._lkgm, self._old_lkgm))
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700113
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700114 logging.info('Updating LKGM version: %s (was %s),',
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700115 self._lkgm, self._old_lkgm)
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -0800116 osutils.WriteFile(lkgm_file, self._lkgm)
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700117
Ben Pastened3b93d42019-10-10 09:56:29 -0700118 def ComposeCommitMsg(self):
119 """Constructs and returns the commit message for the LKGM update."""
120 commit_msg_template = (
121 'LKGM %(version)s for chromeos.'
122 '\n\n%(cq_includes)s'
123 '\nBUG=762641')
124 cq_includes = ''
125 for bot in self._PRESUBMIT_BOTS:
126 cq_includes += 'CQ_INCLUDE_TRYBOTS=luci.chrome.try:%s\n' % bot
127 return commit_msg_template % dict(
128 version=self._lkgm, cq_includes=cq_includes)
129
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700130 def CommitNewLKGM(self):
131 """Commits the new LKGM file using our template commit message."""
Ben Pastened3b93d42019-10-10 09:56:29 -0700132 self._committer.Commit([constants.PATH_TO_CHROME_LKGM],
133 self.ComposeCommitMsg())
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700134
Achuith Bhandarkar1b9180f2018-02-22 19:12:09 -0800135
Achuith Bhandarkarb4f69982018-02-27 15:59:25 -0800136def GetArgs(argv):
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700137 """Returns a dictionary of parsed args.
Achuith Bhandarkar1b9180f2018-02-22 19:12:09 -0800138
Achuith Bhandarkarb4f69982018-02-27 15:59:25 -0800139 Args:
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700140 argv: raw command line.
141
142 Returns:
143 Dictionary of parsed args.
Achuith Bhandarkarb4f69982018-02-27 15:59:25 -0800144 """
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -0800145 committer_parser = chrome_committer.ChromeCommitter.GetParser()
Achuith Bhandarkarf561c152018-08-27 19:05:39 -0700146 parser = commandline.ArgumentParser(description=__doc__,
147 parents=[committer_parser],
148 add_help=False, logging=False)
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700149 parser.add_argument('--lkgm', required=True,
Mike Frysinger80de5012019-08-01 14:10:53 -0400150 help='LKGM version to update to.')
Achuith Bhandarkarb4f69982018-02-27 15:59:25 -0800151 return parser.parse_args(argv)
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700152
153def main(argv):
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -0700154 ChromeLKGMCommitter(GetArgs(argv)).Run()
Steven Bennettsddf9bcd2017-06-14 14:07:43 -0700155 return 0