chrome_release_branch.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import os
  8. import re
  9. import subprocess
  10. import sys
  11. from infra import git
  12. from infra import go
  13. _TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
  14. _REPO_ROOT = os.path.realpath(os.path.join(_TOOLS_DIR, os.pardir))
  15. _INFRA_BOTS = os.path.join(_REPO_ROOT, 'infra', 'bots')
  16. sys.path.insert(0, _INFRA_BOTS)
  17. import git_utils
  18. REFS_HEADS_PREFIX = 'refs/heads/'
  19. CHROME_REF_PREFIX = REFS_HEADS_PREFIX + 'chrome/m'
  20. SK_MILESTONE_H = os.path.join('include', 'core', 'SkMilestone.h')
  21. SK_MILESTONE_TMPL = r'#define SK_MILESTONE %s'
  22. SK_MILESTONE_RE = SK_MILESTONE_TMPL % r'(\d+)'
  23. SKIA_REPO = 'https://skia.googlesource.com/skia.git'
  24. SUPPORTED_CHROME_BRANCHES = 2 # Per infra policy; see skbug.com/8940
  25. UPDATE_MILESTONE_COMMIT_MSG = '''Update Skia milestone to %d'''
  26. def get_current_milestone():
  27. '''Read SkMilestone.h and parse out the current milestone.'''
  28. sk_milestone = os.path.join(_REPO_ROOT, SK_MILESTONE_H)
  29. with open(sk_milestone, 'r') as f:
  30. contents = f.read()
  31. for line in contents.splitlines():
  32. m = re.match(SK_MILESTONE_RE, line)
  33. if m:
  34. return int(m.groups()[0])
  35. print >> sys.stderr, (
  36. 'Failed to parse %s; has the format changed?' % SK_MILESTONE_H)
  37. sys.exit(1)
  38. def create_new_branch(new_branch, branch_at):
  39. '''Create a temporary checkout of the repo, create the new branch and push.'''
  40. b = new_branch[len(REFS_HEADS_PREFIX):]
  41. with git_utils.NewGitCheckout(SKIA_REPO, local=_REPO_ROOT):
  42. git.git('checkout', '-b', b)
  43. git.git('reset', '--hard', branch_at)
  44. git.git('push', '--set-upstream', 'origin', b)
  45. def update_milestone(m):
  46. '''Update SkMilestone.h to match the given milestone number.'''
  47. with git_utils.NewGitCheckout(SKIA_REPO, local=_REPO_ROOT):
  48. with git_utils.GitBranch(
  49. 'update_milestone', UPDATE_MILESTONE_COMMIT_MSG % m):
  50. with open(SK_MILESTONE_H, 'r+') as f:
  51. contents = re.sub(
  52. SK_MILESTONE_RE, SK_MILESTONE_TMPL % str(m), f.read(), flags=re.M)
  53. f.seek(0)
  54. f.write(contents)
  55. f.truncate()
  56. git.git('diff')
  57. def update_infra_config(old_branch, new_branch):
  58. '''Create a CL to add infra support for the new branch and remove the old.'''
  59. owner = git.git('config', 'user.email').rstrip()
  60. if not owner:
  61. print >> sys.stderr, ('No configured git user; please run '
  62. '"git config user.email <your email>".')
  63. sys.exit(1)
  64. go.get(go.INFRA_GO+'/go/supported_branches/cmd/new-branch')
  65. subprocess.check_call(['new-branch',
  66. '--branch', new_branch[len(REFS_HEADS_PREFIX):],
  67. '--delete', old_branch[len(REFS_HEADS_PREFIX):],
  68. '--owner', owner,
  69. '--exclude-trybots=chromium.*',
  70. '--exclude-trybots=.*Android_Framework.*'])
  71. def main():
  72. if len(sys.argv) != 2 or '--help' in sys.argv or '-h' in sys.argv:
  73. print >> sys.stderr, 'Usage: %s <commit hash for branch>' % sys.argv[0]
  74. sys.exit(1)
  75. go.check()
  76. branch_at = sys.argv[1]
  77. m = get_current_milestone()
  78. new_branch = '%s%d' % (CHROME_REF_PREFIX, m)
  79. old_branch = '%s%d' % (CHROME_REF_PREFIX, m-SUPPORTED_CHROME_BRANCHES)
  80. print 'Creating branch %s and removing support (eg. CQ) for %s' % (
  81. new_branch, old_branch)
  82. create_new_branch(new_branch, branch_at)
  83. update_milestone(m+1)
  84. update_infra_config(old_branch, new_branch)
  85. if __name__ == '__main__':
  86. main()