Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Mike Frysinger | 110750a | 2012-03-26 14:19:20 -0400 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This module uprevs a given package's ebuild to the next revision.""" |
| 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 10 | import os |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 11 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 12 | from chromite.lib import constants |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 13 | from chromite.lib import commandline |
Chris Sosa | c13bba5 | 2011-05-24 15:14:09 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_logging as logging |
David James | 97d9587 | 2012-11-16 15:09:56 -0800 | [diff] [blame] | 16 | from chromite.lib import git |
Mike Frysinger | fddaeb5 | 2012-11-20 11:17:31 -0500 | [diff] [blame] | 17 | from chromite.lib import osutils |
David James | 6450a0a | 2012-12-04 07:59:53 -0800 | [diff] [blame] | 18 | from chromite.lib import parallel |
Alex Deymo | 075c229 | 2014-09-04 18:31:50 -0700 | [diff] [blame] | 19 | from chromite.lib import portage_util |
Lann Martin | b26e129 | 2018-08-09 13:59:19 -0600 | [diff] [blame] | 20 | from chromite.lib import repo_util |
Chris Sosa | c13bba5 | 2011-05-24 15:14:09 -0700 | [diff] [blame] | 21 | |
Gabe Black | 71e963e | 2014-10-28 20:19:59 -0700 | [diff] [blame] | 22 | # Commit message subject for uprevving Portage packages. |
| 23 | GIT_COMMIT_SUBJECT = 'Marking set of ebuilds as stable' |
David James | 15ed130 | 2013-04-25 09:21:19 -0700 | [diff] [blame] | 24 | |
David James | 29e86d5 | 2013-04-19 09:41:29 -0700 | [diff] [blame] | 25 | # Commit message for uprevving Portage packages. |
| 26 | _GIT_COMMIT_MESSAGE = 'Marking 9999 ebuild for %s as stable.' |
| 27 | |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 28 | # Dictionary of valid commands with usage information. |
| 29 | COMMAND_DICTIONARY = { |
Mike Frysinger | 5cd8c74 | 2013-10-11 14:43:01 -0400 | [diff] [blame] | 30 | 'commit': 'Marks given ebuilds as stable locally', |
| 31 | 'push': 'Pushes previous marking of ebuilds to remote repo', |
| 32 | } |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 33 | |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 34 | |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 35 | # ======================= Global Helper Functions ======================== |
| 36 | |
| 37 | |
David James | 41124af | 2015-06-04 21:13:25 -0700 | [diff] [blame] | 38 | def CleanStalePackages(srcroot, boards, package_atoms): |
Chris Sosa | bf15387 | 2011-04-28 14:21:09 -0700 | [diff] [blame] | 39 | """Cleans up stale package info from a previous build. |
Mike Frysinger | 5cd8c74 | 2013-10-11 14:43:01 -0400 | [diff] [blame] | 40 | |
Chris Sosa | bf15387 | 2011-04-28 14:21:09 -0700 | [diff] [blame] | 41 | Args: |
David James | 41124af | 2015-06-04 21:13:25 -0700 | [diff] [blame] | 42 | srcroot: Root directory of the source tree. |
David James | cc09c9b | 2012-01-26 22:10:13 -0800 | [diff] [blame] | 43 | boards: Boards to clean the packages from. |
Mike Frysinger | de5ab0e | 2013-03-21 20:48:36 -0400 | [diff] [blame] | 44 | package_atoms: A list of package atoms to unmerge. |
Chris Sosa | bf15387 | 2011-04-28 14:21:09 -0700 | [diff] [blame] | 45 | """ |
David James | 15ed130 | 2013-04-25 09:21:19 -0700 | [diff] [blame] | 46 | if package_atoms: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame^] | 47 | logging.info('Cleaning up stale packages %s.', package_atoms) |
David James | 15ed130 | 2013-04-25 09:21:19 -0700 | [diff] [blame] | 48 | |
Mike Frysinger | b7ab9b8 | 2012-04-04 16:22:43 -0400 | [diff] [blame] | 49 | # First unmerge all the packages for a board, then eclean it. |
| 50 | # We need these two steps to run in order (unmerge/eclean), |
| 51 | # but we can let all the boards run in parallel. |
| 52 | def _CleanStalePackages(board): |
| 53 | if board: |
| 54 | suffix = '-' + board |
| 55 | runcmd = cros_build_lib.RunCommand |
| 56 | else: |
| 57 | suffix = '' |
| 58 | runcmd = cros_build_lib.SudoRunCommand |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 59 | |
David James | 59a0a2b | 2013-03-22 14:04:44 -0700 | [diff] [blame] | 60 | emerge, eclean = 'emerge' + suffix, 'eclean' + suffix |
| 61 | if not osutils.FindMissingBinaries([emerge, eclean]): |
David James | 63841a8 | 2014-01-16 14:39:24 -0800 | [diff] [blame] | 62 | if package_atoms: |
| 63 | # If nothing was found to be unmerged, emerge will exit(1). |
| 64 | result = runcmd([emerge, '-q', '--unmerge'] + package_atoms, |
David James | 41124af | 2015-06-04 21:13:25 -0700 | [diff] [blame] | 65 | enter_chroot=True, extra_env={'CLEAN_DELAY': '0'}, |
| 66 | error_code_ok=True, cwd=srcroot) |
David James | 63841a8 | 2014-01-16 14:39:24 -0800 | [diff] [blame] | 67 | if not result.returncode in (0, 1): |
| 68 | raise cros_build_lib.RunCommandError('unexpected error', result) |
David James | 59a0a2b | 2013-03-22 14:04:44 -0700 | [diff] [blame] | 69 | runcmd([eclean, '-d', 'packages'], |
David James | 41124af | 2015-06-04 21:13:25 -0700 | [diff] [blame] | 70 | cwd=srcroot, enter_chroot=True, |
David James | 59a0a2b | 2013-03-22 14:04:44 -0700 | [diff] [blame] | 71 | redirect_stdout=True, redirect_stderr=True) |
Mike Frysinger | b7ab9b8 | 2012-04-04 16:22:43 -0400 | [diff] [blame] | 72 | |
| 73 | tasks = [] |
David James | cc09c9b | 2012-01-26 22:10:13 -0800 | [diff] [blame] | 74 | for board in boards: |
Mike Frysinger | b7ab9b8 | 2012-04-04 16:22:43 -0400 | [diff] [blame] | 75 | tasks.append([board]) |
| 76 | tasks.append([None]) |
| 77 | |
David James | 6450a0a | 2012-12-04 07:59:53 -0800 | [diff] [blame] | 78 | parallel.RunTasksInProcessPool(_CleanStalePackages, tasks) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 79 | |
| 80 | |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 81 | # TODO(build): This code needs to be gutted and rebased to cros_build_lib. |
| 82 | def _DoWeHaveLocalCommits(stable_branch, tracking_branch, cwd): |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 83 | """Returns true if there are local commits.""" |
David James | 97d9587 | 2012-11-16 15:09:56 -0800 | [diff] [blame] | 84 | output = git.RunGit( |
Paul Hobbs | 72d8e39 | 2015-10-21 17:24:23 -0700 | [diff] [blame] | 85 | cwd, ['rev-parse', stable_branch, tracking_branch]).output.split() |
Brian Harring | 5b86b5e | 2012-05-25 18:27:40 -0700 | [diff] [blame] | 86 | return output[0] != output[1] |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 87 | |
| 88 | |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 89 | # ======================= End Global Helper Functions ======================== |
| 90 | |
| 91 | |
Ningning Xia | 5200906 | 2016-05-09 14:33:51 -0700 | [diff] [blame] | 92 | def PushChange(stable_branch, tracking_branch, dryrun, cwd, |
| 93 | staging_branch=None): |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 94 | """Pushes commits in the stable_branch to the remote git repository. |
| 95 | |
David James | ee2da62 | 2012-02-23 09:32:16 -0800 | [diff] [blame] | 96 | Pushes local commits from calls to CommitChange to the remote git |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 97 | repository specified by current working directory. If changes are |
| 98 | found to commit, they will be merged to the merge branch and pushed. |
| 99 | In that case, the local repository will be left on the merge branch. |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 100 | |
| 101 | Args: |
| 102 | stable_branch: The local branch with commits we want to push. |
| 103 | tracking_branch: The tracking branch of the local branch. |
Chris Sosa | 62ad852 | 2011-03-08 17:46:17 -0800 | [diff] [blame] | 104 | dryrun: Use git push --dryrun to emulate a push. |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 105 | cwd: The directory to run commands in. |
Ningning Xia | 5200906 | 2016-05-09 14:33:51 -0700 | [diff] [blame] | 106 | staging_branch: The staging branch to push for a failed PFQ run |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 107 | |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 108 | Raises: |
Mike Frysinger | 5cd8c74 | 2013-10-11 14:43:01 -0400 | [diff] [blame] | 109 | OSError: Error occurred while pushing. |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 110 | """ |
David James | 4795963 | 2015-10-23 07:56:01 -0700 | [diff] [blame] | 111 | if not git.DoesCommitExistInRepo(cwd, stable_branch): |
| 112 | logging.debug('No branch created for %s. Exiting', cwd) |
| 113 | return |
| 114 | |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 115 | if not _DoWeHaveLocalCommits(stable_branch, tracking_branch, cwd): |
David James | 4795963 | 2015-10-23 07:56:01 -0700 | [diff] [blame] | 116 | logging.debug('No work found to push in %s. Exiting', cwd) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 117 | return |
| 118 | |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 119 | # For the commit queue, our local branch may contain commits that were |
| 120 | # just tested and pushed during the CommitQueueCompletion stage. Sync |
| 121 | # and rebase our local branch on top of the remote commits. |
Paul Hobbs | 72d8e39 | 2015-10-21 17:24:23 -0700 | [diff] [blame] | 122 | remote_ref = git.GetTrackingBranch(cwd, |
| 123 | branch=stable_branch, |
| 124 | for_push=True) |
Paul Hobbs | 1e46be8 | 2015-10-30 13:46:02 -0700 | [diff] [blame] | 125 | # SyncPushBranch rebases HEAD onto the updated remote. We need to checkout |
| 126 | # stable_branch here in order to update it. |
| 127 | git.RunGit(cwd, ['checkout', stable_branch]) |
Don Garrett | 9944959 | 2015-03-25 11:01:30 -0700 | [diff] [blame] | 128 | git.SyncPushBranch(cwd, remote_ref.remote, remote_ref.ref) |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 129 | |
| 130 | # Check whether any local changes remain after the sync. |
Don Garrett | 9944959 | 2015-03-25 11:01:30 -0700 | [diff] [blame] | 131 | if not _DoWeHaveLocalCommits(stable_branch, remote_ref.ref, cwd): |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 132 | logging.info('All changes already pushed for %s. Exiting', cwd) |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 133 | return |
| 134 | |
Matt Tennant | cb52205 | 2013-11-25 14:23:43 -0800 | [diff] [blame] | 135 | # Add a failsafe check here. Only CLs from the 'chrome-bot' user should |
| 136 | # be involved here. If any other CLs are found then complain. |
| 137 | # In dryruns extra CLs are normal, though, and can be ignored. |
| 138 | bad_cl_cmd = ['log', '--format=short', '--perl-regexp', |
| 139 | '--author', '^(?!chrome-bot)', '%s..%s' % ( |
Don Garrett | 9944959 | 2015-03-25 11:01:30 -0700 | [diff] [blame] | 140 | remote_ref.ref, stable_branch)] |
Matt Tennant | cb52205 | 2013-11-25 14:23:43 -0800 | [diff] [blame] | 141 | bad_cls = git.RunGit(cwd, bad_cl_cmd).output |
| 142 | if bad_cls.strip() and not dryrun: |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 143 | logging.error('The Uprev stage found changes from users other than ' |
| 144 | 'chrome-bot:\n\n%s', bad_cls) |
Matt Tennant | cb52205 | 2013-11-25 14:23:43 -0800 | [diff] [blame] | 145 | raise AssertionError('Unexpected CLs found during uprev stage.') |
| 146 | |
Lev Rumyantsev | 25352ba | 2016-09-07 15:53:58 -0700 | [diff] [blame] | 147 | if staging_branch is not None: |
| 148 | logging.info('PFQ FAILED. Pushing uprev change to staging branch %s', |
| 149 | staging_branch) |
| 150 | |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 151 | description = git.RunGit( |
| 152 | cwd, |
| 153 | ['log', '--format=format:%s%n%n%b', |
Don Garrett | 9944959 | 2015-03-25 11:01:30 -0700 | [diff] [blame] | 154 | '%s..%s' % (remote_ref.ref, stable_branch)]).output |
Gabe Black | 71e963e | 2014-10-28 20:19:59 -0700 | [diff] [blame] | 155 | description = '%s\n\n%s' % (GIT_COMMIT_SUBJECT, description) |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 156 | logging.info('For %s, using description %s', cwd, description) |
Paul Hobbs | f52ea8f | 2015-10-21 17:24:23 -0700 | [diff] [blame] | 157 | git.CreatePushBranch(constants.MERGE_BRANCH, cwd, |
| 158 | remote_push_branch=remote_ref) |
David James | 97d9587 | 2012-11-16 15:09:56 -0800 | [diff] [blame] | 159 | git.RunGit(cwd, ['merge', '--squash', stable_branch]) |
| 160 | git.RunGit(cwd, ['commit', '-m', description]) |
| 161 | git.RunGit(cwd, ['config', 'push.default', 'tracking']) |
Mike Nichols | a6818c5 | 2018-04-09 11:05:42 -0600 | [diff] [blame] | 162 | git.PushBranch(constants.MERGE_BRANCH, cwd, dryrun=dryrun, |
| 163 | staging_branch=staging_branch) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 164 | |
| 165 | |
| 166 | class GitBranch(object): |
| 167 | """Wrapper class for a git branch.""" |
| 168 | |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 169 | def __init__(self, branch_name, tracking_branch, cwd): |
David James | c7c4ff5 | 2013-09-18 17:57:13 -0700 | [diff] [blame] | 170 | """Sets up variables but does not create the branch. |
| 171 | |
Mike Frysinger | 5cd8c74 | 2013-10-11 14:43:01 -0400 | [diff] [blame] | 172 | Args: |
David James | c7c4ff5 | 2013-09-18 17:57:13 -0700 | [diff] [blame] | 173 | branch_name: The name of the branch. |
| 174 | tracking_branch: The associated tracking branch. |
| 175 | cwd: The git repository to work in. |
| 176 | """ |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 177 | self.branch_name = branch_name |
| 178 | self.tracking_branch = tracking_branch |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 179 | self.cwd = cwd |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 180 | |
| 181 | def CreateBranch(self): |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 182 | self.Checkout() |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 183 | |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 184 | def Checkout(self, branch=None): |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 185 | """Function used to check out to another GitBranch.""" |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 186 | if not branch: |
| 187 | branch = self.branch_name |
| 188 | if branch == self.tracking_branch or self.Exists(branch): |
Lann Martin | b26e129 | 2018-08-09 13:59:19 -0600 | [diff] [blame] | 189 | git.RunGit(self.cwd, ['checkout', '-f', branch], quiet=True) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 190 | else: |
Lann Martin | b26e129 | 2018-08-09 13:59:19 -0600 | [diff] [blame] | 191 | repo = repo_util.Repository.MustFind(self.cwd) |
| 192 | repo.StartBranch(branch, projects=['.'], cwd=self.cwd) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 193 | |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 194 | def Exists(self, branch=None): |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 195 | """Returns True if the branch exists.""" |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 196 | if not branch: |
| 197 | branch = self.branch_name |
David James | 67d7325 | 2013-09-19 17:33:12 -0700 | [diff] [blame] | 198 | branches = git.RunGit(self.cwd, ['branch']).output |
Mike Frysinger | 2ebe373 | 2012-05-08 17:04:12 -0400 | [diff] [blame] | 199 | return branch in branches.split() |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 200 | |
| 201 | |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 202 | def GetParser(): |
| 203 | """Creates the argparse parser.""" |
| 204 | parser = commandline.ArgumentParser() |
| 205 | parser.add_argument('--all', action='store_true', |
| 206 | help='Mark all packages as stable.') |
| 207 | parser.add_argument('-b', '--boards', default='', |
| 208 | help='Colon-separated list of boards.') |
| 209 | parser.add_argument('--drop_file', |
| 210 | help='File to list packages that were revved.') |
| 211 | parser.add_argument('--dryrun', action='store_true', |
| 212 | help='Passes dry-run to git push if pushing a change.') |
Bertrand SIMONNET | 6af5430 | 2015-08-05 10:12:49 -0700 | [diff] [blame] | 213 | parser.add_argument('--force', action='store_true', |
| 214 | help='Force the stabilization of blacklisted packages. ' |
| 215 | '(only compatible with -p)') |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 216 | parser.add_argument('-o', '--overlays', |
| 217 | help='Colon-separated list of overlays to modify.') |
Don Garrett | f9eff95 | 2018-08-10 16:50:04 -0700 | [diff] [blame] | 218 | parser.add_argument('--overlay-type', |
| 219 | help='Populates --overlays based on "public", "private"' |
| 220 | ', or "both".') |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 221 | parser.add_argument('-p', '--packages', |
| 222 | help='Colon separated list of packages to rev.') |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 223 | parser.add_argument('--buildroot', type='path', |
| 224 | help='Path to buildroot.') |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 225 | parser.add_argument('-r', '--srcroot', type='path', |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 226 | help='Path to root src. Deprecated in favor of ' |
| 227 | '--buildroot') |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 228 | parser.add_argument('--verbose', action='store_true', |
| 229 | help='Prints out debug info.') |
Ningning Xia | 5200906 | 2016-05-09 14:33:51 -0700 | [diff] [blame] | 230 | parser.add_argument('--staging_branch', |
| 231 | help='The staging branch to push changes') |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 232 | parser.add_argument('command', choices=COMMAND_DICTIONARY.keys(), |
| 233 | help='Command to run.') |
| 234 | return parser |
| 235 | |
| 236 | |
| 237 | def main(argv): |
| 238 | parser = GetParser() |
| 239 | options = parser.parse_args(argv) |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 240 | |
| 241 | # TODO: Remove this code in favor of a simple default on buildroot when |
| 242 | # srcroot is removed. |
| 243 | if options.srcroot and not options.buildroot: |
| 244 | # Convert /<repo>/src -> <repo> |
| 245 | options.buildroot = os.path.dirname(options.srcroot) |
| 246 | if not options.buildroot: |
| 247 | options.buildroot = constants.SOURCE_ROOT |
| 248 | options.srcroot = None |
| 249 | |
Bertrand SIMONNET | 6af5430 | 2015-08-05 10:12:49 -0700 | [diff] [blame] | 250 | options.Freeze() |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 251 | |
Bertrand SIMONNET | 6af5430 | 2015-08-05 10:12:49 -0700 | [diff] [blame] | 252 | if options.command == 'commit': |
| 253 | if not options.packages and not options.all: |
| 254 | parser.error('Please specify at least one package (--packages)') |
| 255 | if options.force and options.all: |
| 256 | parser.error('Cannot use --force with --all. You must specify a list of ' |
| 257 | 'packages you want to force uprev.') |
| 258 | |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 259 | if not os.path.isdir(options.buildroot): |
| 260 | parser.error('buildroot is not a valid path: %s' % options.buildroot) |
Mike Frysinger | e8dcbfd | 2015-03-08 21:45:52 -0400 | [diff] [blame] | 261 | |
Don Garrett | f9eff95 | 2018-08-10 16:50:04 -0700 | [diff] [blame] | 262 | if options.overlay_type and options.overlays: |
| 263 | parser.error('Cannot use --overlay-type with --overlays.') |
| 264 | |
Alex Deymo | 075c229 | 2014-09-04 18:31:50 -0700 | [diff] [blame] | 265 | portage_util.EBuild.VERBOSE = options.verbose |
Chris Sosa | 62ad852 | 2011-03-08 17:46:17 -0800 | [diff] [blame] | 266 | |
Chris Sosa | 62ad852 | 2011-03-08 17:46:17 -0800 | [diff] [blame] | 267 | package_list = None |
| 268 | if options.packages: |
| 269 | package_list = options.packages.split(':') |
| 270 | |
Ningning Xia | db88432 | 2018-01-26 16:27:06 -0800 | [diff] [blame] | 271 | overlays = [] |
Chris Sosa | 62ad852 | 2011-03-08 17:46:17 -0800 | [diff] [blame] | 272 | if options.overlays: |
Chris Sosa | 62ad852 | 2011-03-08 17:46:17 -0800 | [diff] [blame] | 273 | for path in options.overlays.split(':'): |
Ryan Cui | 05a31ba | 2011-05-31 17:47:37 -0700 | [diff] [blame] | 274 | if not os.path.isdir(path): |
Chris Sosa | c13bba5 | 2011-05-24 15:14:09 -0700 | [diff] [blame] | 275 | cros_build_lib.Die('Cannot find overlay: %s' % path) |
Ningning Xia | db88432 | 2018-01-26 16:27:06 -0800 | [diff] [blame] | 276 | overlays.append(os.path.realpath(path)) |
Don Garrett | f9eff95 | 2018-08-10 16:50:04 -0700 | [diff] [blame] | 277 | elif options.overlay_type: |
| 278 | overlays = portage_util.FindOverlays( |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 279 | options.overlay_type, buildroot=options.buildroot) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 280 | else: |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 281 | logging.warning('Missing --overlays argument') |
Ningning Xia | db88432 | 2018-01-26 16:27:06 -0800 | [diff] [blame] | 282 | overlays.extend([ |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 283 | '%s/src/private-overlays/chromeos-overlay' % options.buildroot, |
| 284 | '%s/src/third_party/chromiumos-overlay' % options.buildroot]) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 285 | |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 286 | manifest = git.ManifestCheckout.Cached(options.buildroot) |
Ryan Cui | 4656a3c | 2011-05-24 12:30:30 -0700 | [diff] [blame] | 287 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 288 | # Dict mapping from each overlay to its tracking branch. |
| 289 | overlay_tracking_branch = {} |
| 290 | # Dict mapping from each git repository (project) to a list of its overlays. |
| 291 | git_project_overlays = {} |
| 292 | |
| 293 | for overlay in overlays: |
| 294 | remote_ref = git.GetTrackingBranchViaManifest(overlay, manifest=manifest) |
| 295 | overlay_tracking_branch[overlay] = remote_ref.ref |
| 296 | git_project_overlays.setdefault(remote_ref.project_name, []).append(overlay) |
| 297 | |
| 298 | if options.command == 'push': |
| 299 | _WorkOnPush(options, overlay_tracking_branch, git_project_overlays) |
| 300 | elif options.command == 'commit': |
| 301 | _WorkOnCommit(options, overlays, overlay_tracking_branch, |
| 302 | git_project_overlays, manifest, package_list) |
| 303 | |
| 304 | |
| 305 | def _WorkOnPush(options, overlay_tracking_branch, git_project_overlays): |
| 306 | """Push uprevs of overlays belonging to differet git projects in parallel. |
| 307 | |
| 308 | Args: |
| 309 | options: The options object returned by the argument parser. |
| 310 | overlay_tracking_branch: A dict mapping from each overlay to its tracking |
| 311 | branch. |
| 312 | git_project_overlays: A dict mapping from each git repository to a list of |
| 313 | its overlays. |
| 314 | """ |
| 315 | inputs = [[options, overlays_per_project, overlay_tracking_branch] |
| 316 | for overlays_per_project in git_project_overlays.itervalues()] |
| 317 | parallel.RunTasksInProcessPool(_PushOverlays, inputs) |
| 318 | |
| 319 | |
| 320 | def _PushOverlays(options, overlays, overlay_tracking_branch): |
| 321 | """Push uprevs for overlays in sequence. |
| 322 | |
| 323 | Args: |
| 324 | options: The options object returned by the argument parser. |
| 325 | overlays: A list of overlays to push uprevs in sequence. |
| 326 | overlay_tracking_branch: A dict mapping from each overlay to its tracking |
| 327 | branch. |
| 328 | """ |
| 329 | for overlay in overlays: |
| 330 | if not os.path.isdir(overlay): |
| 331 | logging.warning('Skipping %s, which is not a directory.', overlay) |
| 332 | continue |
| 333 | |
| 334 | tracking_branch = overlay_tracking_branch[overlay] |
| 335 | PushChange(constants.STABLE_EBUILD_BRANCH, tracking_branch, options.dryrun, |
| 336 | cwd=overlay, staging_branch=options.staging_branch) |
| 337 | |
| 338 | |
| 339 | def _WorkOnCommit(options, overlays, overlay_tracking_branch, |
| 340 | git_project_overlays, manifest, package_list): |
| 341 | """Commit uprevs of overlays belonging to different git projects in parallel. |
| 342 | |
| 343 | Args: |
| 344 | options: The options object returned by the argument parser. |
| 345 | overlays: A list of overlays to work on. |
| 346 | overlay_tracking_branch: A dict mapping from each overlay to its tracking |
| 347 | branch. |
| 348 | git_project_overlays: A dict mapping from each git repository to a list of |
| 349 | its overlays. |
| 350 | manifest: The manifest of the given source root. |
| 351 | package_list: A list of packages passed from commandline to work on. |
| 352 | """ |
| 353 | overlay_ebuilds = _GetOverlayToEbuildsMap(options, overlays, package_list) |
David James | 84e953c | 2013-04-23 18:44:06 -0700 | [diff] [blame] | 354 | |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 355 | with parallel.Manager() as manager: |
| 356 | # Contains the array of packages we actually revved. |
| 357 | revved_packages = manager.list() |
| 358 | new_package_atoms = manager.list() |
David James | a8457b5 | 2011-05-28 00:03:20 -0700 | [diff] [blame] | 359 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 360 | inputs = [[options, manifest, overlays_per_project, overlay_tracking_branch, |
| 361 | overlay_ebuilds, revved_packages, new_package_atoms] |
| 362 | for overlays_per_project in git_project_overlays.itervalues()] |
| 363 | parallel.RunTasksInProcessPool(_CommitOverlays, inputs) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 364 | |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 365 | chroot_path = os.path.join(options.buildroot, constants.DEFAULT_CHROOT_DIR) |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 366 | if os.path.exists(chroot_path): |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 367 | CleanStalePackages(options.buildroot, options.boards.split(':'), |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 368 | new_package_atoms) |
| 369 | if options.drop_file: |
| 370 | osutils.WriteFile(options.drop_file, ' '.join(revved_packages)) |
Chris Sosa | dad0d32 | 2011-01-31 16:37:33 -0800 | [diff] [blame] | 371 | |
Brian Harring | 609dc4e | 2012-05-07 02:17:44 -0700 | [diff] [blame] | 372 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 373 | def _GetOverlayToEbuildsMap(options, overlays, package_list): |
| 374 | """Get ebuilds for overlays. |
David James | 15ed130 | 2013-04-25 09:21:19 -0700 | [diff] [blame] | 375 | |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 376 | Args: |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 377 | options: The options object returned by the argument parser. |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 378 | overlays: A list of overlays to work on. |
| 379 | package_list: A list of packages passed from commandline to work on. |
| 380 | |
| 381 | Returns: |
| 382 | A dict mapping each overlay to a list of ebuilds belonging to it. |
| 383 | """ |
| 384 | overlay_ebuilds = {} |
| 385 | inputs = [[overlay, options.all, package_list, options.force] |
| 386 | for overlay in overlays] |
| 387 | result = parallel.RunTasksInProcessPool( |
| 388 | portage_util.GetOverlayEBuilds, inputs) |
| 389 | for idx, ebuilds in enumerate(result): |
| 390 | overlay_ebuilds[overlays[idx]] = ebuilds |
| 391 | |
| 392 | return overlay_ebuilds |
| 393 | |
| 394 | |
| 395 | def _CommitOverlays(options, manifest, overlays, overlay_tracking_branch, |
| 396 | overlay_ebuilds, revved_packages, new_package_atoms): |
| 397 | """Commit uprevs for overlays in sequence. |
| 398 | |
| 399 | Args: |
| 400 | options: The options object returned by the argument parser. |
| 401 | manifest: The manifest of the given source root. |
| 402 | overlays: A list over overlays to commit. |
| 403 | overlay_tracking_branch: A dict mapping from each overlay to its tracking |
| 404 | branch. |
| 405 | overlay_ebuilds: A dict mapping overlays to their ebuilds. |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 406 | revved_packages: A shared list of revved packages. |
| 407 | new_package_atoms: A shared list of new package atoms. |
| 408 | """ |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 409 | for overlay in overlays: |
| 410 | if not os.path.isdir(overlay): |
| 411 | logging.warning('Skipping %s, which is not a directory.', overlay) |
| 412 | continue |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 413 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 414 | # Note we intentionally work from the non push tracking branch; |
| 415 | # everything built thus far has been against it (meaning, http mirrors), |
| 416 | # thus we should honor that. During the actual push, the code switches |
| 417 | # to the correct urls, and does an appropriate rebasing. |
| 418 | tracking_branch = overlay_tracking_branch[overlay] |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 419 | |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 420 | existing_commit = git.GetGitRepoRevision(overlay) |
Lann Martin | e0ef98c | 2018-06-11 13:12:24 -0600 | [diff] [blame] | 421 | |
| 422 | # Make sure we run in the top-level git directory in case we are |
| 423 | # adding/removing an overlay in existing_commit. |
| 424 | git_root = git.FindGitTopLevel(overlay) |
| 425 | if git_root is None: |
| 426 | cros_build_lib.Die('No git repo at overlay directory %s.', overlay) |
| 427 | |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 428 | work_branch = GitBranch(constants.STABLE_EBUILD_BRANCH, tracking_branch, |
Lann Martin | e0ef98c | 2018-06-11 13:12:24 -0600 | [diff] [blame] | 429 | cwd=git_root) |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 430 | work_branch.CreateBranch() |
| 431 | if not work_branch.Exists(): |
| 432 | cros_build_lib.Die('Unable to create stabilizing branch in %s' % |
| 433 | overlay) |
| 434 | |
| 435 | # In the case of uprevving overlays that have patches applied to them, |
| 436 | # include the patched changes in the stabilizing branch. |
Lann Martin | e0ef98c | 2018-06-11 13:12:24 -0600 | [diff] [blame] | 437 | git.RunGit(git_root, ['rebase', existing_commit]) |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 438 | |
Ningning Xia | db88432 | 2018-01-26 16:27:06 -0800 | [diff] [blame] | 439 | ebuilds = overlay_ebuilds.get(overlay, []) |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 440 | if ebuilds: |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 441 | with parallel.Manager() as manager: |
| 442 | # Contains the array of packages we actually revved. |
| 443 | messages = manager.list() |
| 444 | ebuild_paths_to_add = manager.list() |
| 445 | ebuild_paths_to_remove = manager.list() |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 446 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 447 | inputs = [[overlay, ebuild, manifest, options, ebuild_paths_to_add, |
| 448 | ebuild_paths_to_remove, messages, revved_packages, |
| 449 | new_package_atoms] for ebuild in ebuilds] |
| 450 | parallel.RunTasksInProcessPool(_WorkOnEbuild, inputs) |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 451 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 452 | if ebuild_paths_to_add: |
| 453 | logging.info('Adding new stable ebuild paths %s in overlay %s.', |
| 454 | ebuild_paths_to_add, overlay) |
| 455 | git.RunGit(overlay, ['add'] + list(ebuild_paths_to_add)) |
David James | 15ed130 | 2013-04-25 09:21:19 -0700 | [diff] [blame] | 456 | |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 457 | if ebuild_paths_to_remove: |
| 458 | logging.info('Removing old ebuild paths %s in overlay %s.', |
| 459 | ebuild_paths_to_remove, overlay) |
| 460 | git.RunGit(overlay, ['rm', '-f'] + list(ebuild_paths_to_remove)) |
| 461 | |
| 462 | if messages: |
| 463 | portage_util.EBuild.CommitChange('\n\n'.join(messages), overlay) |
David James | 15ed130 | 2013-04-25 09:21:19 -0700 | [diff] [blame] | 464 | |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 465 | |
| 466 | def _WorkOnEbuild(overlay, ebuild, manifest, options, ebuild_paths_to_add, |
| 467 | ebuild_paths_to_remove, messages, revved_packages, |
| 468 | new_package_atoms): |
| 469 | """Work on a single ebuild. |
| 470 | |
| 471 | Args: |
| 472 | overlay: The overlay where the ebuild belongs to. |
| 473 | ebuild: The ebuild to work on. |
| 474 | manifest: The manifest of the given source root. |
| 475 | options: The options object returned by the argument parser. |
| 476 | ebuild_paths_to_add: New stable ebuild paths to add to git. |
| 477 | ebuild_paths_to_remove: Old ebuild paths to remove from git. |
| 478 | messages: A share list of commit messages. |
| 479 | revved_packages: A shared list of revved packages. |
| 480 | new_package_atoms: A shared list of new package atoms. |
| 481 | """ |
| 482 | if options.verbose: |
| 483 | logging.info('Working on %s, info %s', ebuild.package, |
| 484 | ebuild.cros_workon_vars) |
| 485 | try: |
Don Garrett | 4fef8c3 | 2018-08-10 18:04:01 -0700 | [diff] [blame] | 486 | result = ebuild.RevWorkOnEBuild(os.path.join(options.buildroot, 'src'), |
| 487 | manifest) |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 488 | if result: |
| 489 | new_package, ebuild_path_to_add, ebuild_path_to_remove = result |
| 490 | |
| 491 | if ebuild_path_to_add: |
| 492 | ebuild_paths_to_add.append(ebuild_path_to_add) |
| 493 | if ebuild_path_to_remove: |
| 494 | ebuild_paths_to_remove.append(ebuild_path_to_remove) |
| 495 | |
| 496 | messages.append(_GIT_COMMIT_MESSAGE % ebuild.package) |
| 497 | revved_packages.append(ebuild.package) |
| 498 | new_package_atoms.append('=%s' % new_package) |
| 499 | except (OSError, IOError): |
| 500 | logging.warning( |
| 501 | 'Cannot rev %s\n' |
| 502 | 'Note you will have to go into %s ' |
Ningning Xia | 419e4eb | 2018-02-05 10:30:36 -0800 | [diff] [blame] | 503 | 'and reset the git repo yourself.', ebuild.package, overlay) |
Ningning Xia | 783efc0 | 2018-01-24 13:39:51 -0800 | [diff] [blame] | 504 | raise |