Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 2 | # 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 Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 6 | """Update the CHROMEOS_LKGM file in a chromium repository.""" |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Mike Frysinger | a94f125 | 2019-09-12 02:39:28 -0400 | [diff] [blame] | 10 | import distutils.version # pylint: disable=import-error,no-name-in-module |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 11 | import os |
| 12 | |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 13 | from chromite.cbuildbot import manifest_version |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 14 | from chromite.lib import chrome_committer |
Achuith Bhandarkar | f561c15 | 2018-08-27 19:05:39 -0700 | [diff] [blame] | 15 | from chromite.lib import commandline |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 16 | from chromite.lib import constants |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_logging as logging |
Ben Pastene | 5a40b10 | 2019-11-22 17:06:31 -0800 | [diff] [blame^] | 18 | from chromite.lib import gerrit |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 19 | from chromite.lib import osutils |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 20 | |
| 21 | |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 22 | class LKGMNotValid(chrome_committer.CommitError): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 23 | """Raised if the LKGM version is unset or not newer than the current value.""" |
| 24 | |
| 25 | |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 26 | class LKGMFileNotFound(chrome_committer.CommitError): |
| 27 | """Raised if the LKGM file is not found.""" |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 28 | |
| 29 | |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 30 | class ChromeLKGMCommitter(object): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 31 | """Committer object responsible for obtaining a new LKGM and committing it.""" |
| 32 | |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 33 | # 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 Pastene | 8672346 | 2018-04-26 11:07:27 -0700 | [diff] [blame] | 42 | # 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 Mukai | 599dad1 | 2019-01-02 16:43:10 -0800 | [diff] [blame] | 44 | # TBR. TRANSLATION_OWNERS is necesssary to parse CHROMEOS_OWNERS file since |
| 45 | # it has the reference. |
Ben Pastene | 8672346 | 2018-04-26 11:07:27 -0700 | [diff] [blame] | 46 | _NEEDED_FILES = [ |
| 47 | constants.PATH_TO_CHROME_CHROMEOS_OWNERS, |
| 48 | constants.PATH_TO_CHROME_LKGM, |
Jun Mukai | 599dad1 | 2019-01-02 16:43:10 -0800 | [diff] [blame] | 49 | 'tools/translation/TRANSLATION_OWNERS', |
Ben Pastene | 8672346 | 2018-04-26 11:07:27 -0700 | [diff] [blame] | 50 | ] |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 51 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 52 | def __init__(self, args): |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 53 | self._committer = chrome_committer.ChromeCommitter(args) |
| 54 | |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 55 | # Strip any chrome branch from the lkgm version. |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 56 | self._lkgm = manifest_version.VersionInfo(args.lkgm).VersionString() |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 57 | self._old_lkgm = None |
| 58 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 59 | if not self._lkgm: |
| 60 | raise LKGMNotValid('LKGM not provided.') |
| 61 | |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 62 | logging.info('lkgm=%s', self._lkgm) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 63 | |
| 64 | def Run(self): |
Ben Pastene | 5a40b10 | 2019-11-22 17:06:31 -0800 | [diff] [blame^] | 65 | self.CloseOldLKGMRolls() |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 66 | self._committer.Cleanup() |
Ben Pastene | 8672346 | 2018-04-26 11:07:27 -0700 | [diff] [blame] | 67 | self._committer.Checkout(self._NEEDED_FILES) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 68 | self.UpdateLKGM() |
| 69 | self.CommitNewLKGM() |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 70 | self._committer.Upload() |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 71 | |
| 72 | def CheckoutChrome(self): |
| 73 | """Checks out chrome into tmp checkout_dir.""" |
Ben Pastene | 8672346 | 2018-04-26 11:07:27 -0700 | [diff] [blame] | 74 | self._committer.Checkout(self._NEEDED_FILES) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 75 | |
| 76 | @property |
| 77 | def lkgm_file(self): |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 78 | return self._committer.FullPath(constants.PATH_TO_CHROME_LKGM) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 79 | |
Ben Pastene | 5a40b10 | 2019-11-22 17:06:31 -0800 | [diff] [blame^] | 80 | 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 Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 100 | def UpdateLKGM(self): |
| 101 | """Updates the LKGM file with the new version.""" |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 102 | 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 Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 107 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 108 | lv = distutils.version.LooseVersion |
Mike Frysinger | 266e4ff | 2018-07-14 00:41:05 -0400 | [diff] [blame] | 109 | if self._old_lkgm is not None and lv(self._lkgm) <= lv(self._old_lkgm): |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 110 | raise LKGMNotValid( |
| 111 | 'LKGM version (%s) is not newer than current version (%s).' % |
| 112 | (self._lkgm, self._old_lkgm)) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 113 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 114 | logging.info('Updating LKGM version: %s (was %s),', |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 115 | self._lkgm, self._old_lkgm) |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 116 | osutils.WriteFile(lkgm_file, self._lkgm) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 117 | |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 118 | 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 Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 130 | def CommitNewLKGM(self): |
| 131 | """Commits the new LKGM file using our template commit message.""" |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 132 | self._committer.Commit([constants.PATH_TO_CHROME_LKGM], |
| 133 | self.ComposeCommitMsg()) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 134 | |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 135 | |
Achuith Bhandarkar | b4f6998 | 2018-02-27 15:59:25 -0800 | [diff] [blame] | 136 | def GetArgs(argv): |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 137 | """Returns a dictionary of parsed args. |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 138 | |
Achuith Bhandarkar | b4f6998 | 2018-02-27 15:59:25 -0800 | [diff] [blame] | 139 | Args: |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 140 | argv: raw command line. |
| 141 | |
| 142 | Returns: |
| 143 | Dictionary of parsed args. |
Achuith Bhandarkar | b4f6998 | 2018-02-27 15:59:25 -0800 | [diff] [blame] | 144 | """ |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 145 | committer_parser = chrome_committer.ChromeCommitter.GetParser() |
Achuith Bhandarkar | f561c15 | 2018-08-27 19:05:39 -0700 | [diff] [blame] | 146 | parser = commandline.ArgumentParser(description=__doc__, |
| 147 | parents=[committer_parser], |
| 148 | add_help=False, logging=False) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 149 | parser.add_argument('--lkgm', required=True, |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame] | 150 | help='LKGM version to update to.') |
Achuith Bhandarkar | b4f6998 | 2018-02-27 15:59:25 -0800 | [diff] [blame] | 151 | return parser.parse_args(argv) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 152 | |
| 153 | def main(argv): |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 154 | ChromeLKGMCommitter(GetArgs(argv)).Run() |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 155 | return 0 |