Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2019 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 9 | import os |
| 10 | import re |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | from infra import git |
| 15 | from infra import go |
| 16 | |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 17 | _TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 18 | _REPO_ROOT = os.path.realpath(os.path.join(_TOOLS_DIR, os.pardir)) |
| 19 | _INFRA_BOTS = os.path.join(_REPO_ROOT, 'infra', 'bots') |
| 20 | sys.path.insert(0, _INFRA_BOTS) |
| 21 | import git_utils |
| 22 | |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 23 | |
| 24 | REFS_HEADS_PREFIX = 'refs/heads/' |
| 25 | CHROME_REF_PREFIX = REFS_HEADS_PREFIX + 'chrome/m' |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 26 | SK_MILESTONE_H = os.path.join('include', 'core', 'SkMilestone.h') |
| 27 | SK_MILESTONE_TMPL = r'#define SK_MILESTONE %s' |
| 28 | SK_MILESTONE_RE = SK_MILESTONE_TMPL % r'(\d+)' |
| 29 | SKIA_REPO = 'https://skia.googlesource.com/skia.git' |
Eric Boren | 3101370 | 2020-05-13 13:48:55 -0400 | [diff] [blame] | 30 | SUPPORTED_CHROME_BRANCHES = 3 |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 31 | UPDATE_MILESTONE_COMMIT_MSG = '''Update Skia milestone to %d''' |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 32 | |
| 33 | |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 34 | def get_current_milestone(): |
| 35 | '''Read SkMilestone.h and parse out the current milestone.''' |
| 36 | sk_milestone = os.path.join(_REPO_ROOT, SK_MILESTONE_H) |
| 37 | with open(sk_milestone, 'r') as f: |
| 38 | contents = f.read() |
| 39 | for line in contents.splitlines(): |
| 40 | m = re.match(SK_MILESTONE_RE, line) |
| 41 | if m: |
| 42 | return int(m.groups()[0]) |
| 43 | print >> sys.stderr, ( |
| 44 | 'Failed to parse %s; has the format changed?' % SK_MILESTONE_H) |
| 45 | sys.exit(1) |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 46 | |
| 47 | |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 48 | def create_new_branch(new_branch, branch_at): |
| 49 | '''Create a temporary checkout of the repo, create the new branch and push.''' |
| 50 | b = new_branch[len(REFS_HEADS_PREFIX):] |
| 51 | with git_utils.NewGitCheckout(SKIA_REPO, local=_REPO_ROOT): |
| 52 | git.git('checkout', '-b', b) |
| 53 | git.git('reset', '--hard', branch_at) |
| 54 | git.git('push', '--set-upstream', 'origin', b) |
| 55 | |
| 56 | |
| 57 | def update_milestone(m): |
| 58 | '''Update SkMilestone.h to match the given milestone number.''' |
| 59 | with git_utils.NewGitCheckout(SKIA_REPO, local=_REPO_ROOT): |
| 60 | with git_utils.GitBranch( |
| 61 | 'update_milestone', UPDATE_MILESTONE_COMMIT_MSG % m): |
| 62 | with open(SK_MILESTONE_H, 'r+') as f: |
| 63 | contents = re.sub( |
| 64 | SK_MILESTONE_RE, SK_MILESTONE_TMPL % str(m), f.read(), flags=re.M) |
| 65 | f.seek(0) |
| 66 | f.write(contents) |
| 67 | f.truncate() |
| 68 | git.git('diff') |
| 69 | |
| 70 | |
| 71 | def update_infra_config(old_branch, new_branch): |
| 72 | '''Create a CL to add infra support for the new branch and remove the old.''' |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 73 | owner = git.git('config', 'user.email').rstrip() |
| 74 | if not owner: |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 75 | print >> sys.stderr, ('No configured git user; please run ' |
| 76 | '"git config user.email <your email>".') |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 77 | sys.exit(1) |
Eric Boren | 46e2d8d | 2019-12-05 14:29:38 -0500 | [diff] [blame] | 78 | go.mod_download() |
| 79 | go.install(go.INFRA_GO+'/go/supported_branches/cmd/new-branch') |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 80 | subprocess.check_call(['new-branch', |
Eric Boren | 6fed6d3 | 2019-06-04 12:36:43 -0400 | [diff] [blame] | 81 | '--branch', new_branch[len(REFS_HEADS_PREFIX):], |
| 82 | '--delete', old_branch[len(REFS_HEADS_PREFIX):], |
Eric Boren | 06a7257 | 2019-04-18 08:54:33 -0400 | [diff] [blame] | 83 | '--owner', owner, |
| 84 | '--exclude-trybots=chromium.*', |
Eric Boren | 7e9dc42 | 2019-12-04 07:18:57 -0500 | [diff] [blame] | 85 | '--exclude-trybots=.*Android_Framework.*', |
Ravi Mistry | 262796e | 2020-02-28 12:29:53 -0500 | [diff] [blame] | 86 | '--exclude-trybots=.*G3_Framework.*', |
Eric Boren | fafb477 | 2021-01-08 09:06:54 -0500 | [diff] [blame] | 87 | '--exclude-trybots=.*CanvasKit.*', |
| 88 | '--exclude-trybots=.*PathKit.*', |
Eric Boren | 7e9dc42 | 2019-12-04 07:18:57 -0500 | [diff] [blame] | 89 | '--submit']) |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 90 | |
| 91 | |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 92 | def main(): |
Eric Boren | 6fed6d3 | 2019-06-04 12:36:43 -0400 | [diff] [blame] | 93 | if len(sys.argv) != 2 or '--help' in sys.argv or '-h' in sys.argv: |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 94 | print >> sys.stderr, 'Usage: %s <commit hash for branch>' % sys.argv[0] |
| 95 | sys.exit(1) |
Eric Boren | 06a7257 | 2019-04-18 08:54:33 -0400 | [diff] [blame] | 96 | go.check() |
Eric Boren | f19ef9b | 2019-04-17 12:32:07 -0400 | [diff] [blame] | 97 | branch_at = sys.argv[1] |
| 98 | m = get_current_milestone() |
| 99 | new_branch = '%s%d' % (CHROME_REF_PREFIX, m) |
| 100 | old_branch = '%s%d' % (CHROME_REF_PREFIX, m-SUPPORTED_CHROME_BRANCHES) |
| 101 | print 'Creating branch %s and removing support (eg. CQ) for %s' % ( |
| 102 | new_branch, old_branch) |
| 103 | create_new_branch(new_branch, branch_at) |
| 104 | update_milestone(m+1) |
| 105 | update_infra_config(old_branch, new_branch) |
| 106 | |
| 107 | |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 108 | if __name__ == '__main__': |
| 109 | main() |