Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. |
| 3 | # |
| 4 | # Use of this source code is governed by a BSD-style license |
| 5 | # that can be found in the LICENSE file in the root of the source |
| 6 | # tree. An additional intellectual property rights grant can be found |
| 7 | # in the file PATENTS. All contributing project authors may |
| 8 | # be found in the AUTHORS file in the root of the source tree. |
| 9 | |
| 10 | """Script to auto-update the WebRTC source version in call/version.cc""" |
| 11 | |
| 12 | import argparse |
| 13 | import datetime |
| 14 | import logging |
| 15 | import os |
| 16 | import re |
| 17 | import subprocess |
| 18 | import sys |
| 19 | |
| 20 | |
| 21 | def FindSrcDirPath(): |
| 22 | """Returns the abs path to the src/ dir of the project.""" |
| 23 | src_dir = os.path.dirname(os.path.abspath(__file__)) |
| 24 | while os.path.basename(src_dir) != 'src': |
| 25 | src_dir = os.path.normpath(os.path.join(src_dir, os.pardir)) |
| 26 | return src_dir |
| 27 | |
| 28 | |
| 29 | UPDATE_BRANCH_NAME = 'webrtc_version_update' |
| 30 | CHECKOUT_SRC_DIR = FindSrcDirPath() |
| 31 | |
| 32 | |
| 33 | def _RemovePreviousUpdateBranch(): |
| 34 | active_branch, branches = _GetBranches() |
| 35 | if active_branch == UPDATE_BRANCH_NAME: |
| 36 | active_branch = 'master' |
| 37 | if UPDATE_BRANCH_NAME in branches: |
| 38 | logging.info('Removing previous update branch (%s)', |
| 39 | UPDATE_BRANCH_NAME) |
| 40 | subprocess.check_call(['git', 'checkout', active_branch]) |
| 41 | subprocess.check_call(['git', 'branch', '-D', UPDATE_BRANCH_NAME]) |
| 42 | logging.info('No branch to remove') |
| 43 | |
| 44 | |
Mirko Bonadei | a776f51 | 2021-03-15 20:41:34 +0100 | [diff] [blame] | 45 | def _GetLastAuthor(): |
| 46 | """Returns a string with the author of the last commit.""" |
| 47 | author = subprocess.check_output(['git', 'log', |
| 48 | '-1', |
| 49 | '--pretty=format:"%an"']).splitlines() |
| 50 | return author |
| 51 | |
| 52 | |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 53 | def _GetBranches(): |
| 54 | """Returns a tuple (active, branches). |
| 55 | |
| 56 | 'active' is a string with name of the currently active branch, while |
| 57 | 'branches' is the list of all branches. |
| 58 | """ |
| 59 | lines = subprocess.check_output(['git', 'branch']).splitlines() |
| 60 | branches = [] |
| 61 | active = '' |
| 62 | for line in lines: |
| 63 | if '*' in line: |
| 64 | # The assumption is that the first char will always be the '*'. |
| 65 | active = line[1:].strip() |
| 66 | branches.append(active) |
| 67 | else: |
| 68 | branch = line.strip() |
| 69 | if branch: |
| 70 | branches.append(branch) |
| 71 | return active, branches |
| 72 | |
| 73 | |
| 74 | def _CreateUpdateBranch(): |
| 75 | logging.info('Creating update branch: %s', UPDATE_BRANCH_NAME) |
| 76 | subprocess.check_call(['git', 'checkout', '-b', UPDATE_BRANCH_NAME]) |
| 77 | |
| 78 | |
| 79 | def _UpdateWebRTCVersion(filename): |
| 80 | with open(filename) as f: |
| 81 | content = f.read() |
| 82 | d = datetime.datetime.utcnow() |
| 83 | # pylint: disable=line-too-long |
| 84 | new_content = re.sub( |
| 85 | r'WebRTC source stamp [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}', |
| 86 | r'WebRTC source stamp %02d-%02d-%02dT%02d:%02d:%02d' % (d.year, |
| 87 | d.month, |
| 88 | d.day, |
| 89 | d.hour, |
| 90 | d.minute, |
| 91 | d.second), |
| 92 | content, |
| 93 | flags=re.MULTILINE) |
| 94 | # pylint: enable=line-too-long |
| 95 | with open(filename, 'w') as f: |
| 96 | f.write(new_content) |
| 97 | |
| 98 | |
| 99 | def _IsTreeClean(): |
| 100 | stdout = subprocess.check_output(['git', 'status', '--porcelain']) |
| 101 | if len(stdout) == 0: |
| 102 | return True |
| 103 | return False |
| 104 | |
| 105 | |
| 106 | def _LocalCommit(): |
| 107 | logging.info('Committing changes locally.') |
| 108 | d = datetime.datetime.utcnow() |
Mirko Bonadei | a6c236f | 2020-12-14 21:45:37 +0100 | [diff] [blame] | 109 | |
| 110 | git_author = subprocess.check_output(['git', 'config', |
| 111 | 'user.email']).strip() |
| 112 | tbr_authors = git_author + ',' + 'mbonadei@webrtc.org' |
| 113 | tbr = 'TBR=%s' % tbr_authors |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 114 | commit_msg = ('Update WebRTC code version (%02d-%02d-%02dT%02d:%02d:%02d).' |
Mirko Bonadei | b08b23e | 2020-12-15 13:15:01 +0100 | [diff] [blame] | 115 | '\n\nTBR=%s\nBug: None') |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 116 | commit_msg = commit_msg % (d.year, d.month, d.day, d.hour, d.minute, |
Mirko Bonadei | a6c236f | 2020-12-14 21:45:37 +0100 | [diff] [blame] | 117 | d.second, tbr_authors) |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 118 | subprocess.check_call(['git', 'add', '--update', '.']) |
| 119 | subprocess.check_call(['git', 'commit', '-m', commit_msg]) |
| 120 | |
| 121 | |
| 122 | def _UploadCL(commit_queue_mode): |
| 123 | """Upload the committed changes as a changelist to Gerrit. |
| 124 | |
| 125 | commit_queue_mode: |
| 126 | - 2: Submit to commit queue. |
| 127 | - 1: Run trybots but do not submit to CQ. |
| 128 | - 0: Skip CQ, upload only. |
| 129 | """ |
| 130 | cmd = ['git', 'cl', 'upload', '--force', '--bypass-hooks', |
| 131 | '--cc=""', '--bypass-watchlist'] |
| 132 | if commit_queue_mode >= 2: |
| 133 | logging.info('Sending the CL to the CQ...') |
| 134 | cmd.extend(['--use-commit-queue']) |
| 135 | elif commit_queue_mode >= 1: |
| 136 | logging.info('Starting CQ dry run...') |
| 137 | cmd.extend(['--cq-dry-run']) |
| 138 | subprocess.check_call(cmd) |
| 139 | |
| 140 | |
| 141 | def main(): |
| 142 | logging.basicConfig(level=logging.INFO) |
| 143 | p = argparse.ArgumentParser() |
| 144 | p.add_argument('--clean', |
| 145 | action='store_true', |
| 146 | default=False, |
| 147 | help='Removes any previous local update branch.') |
| 148 | opts = p.parse_args() |
| 149 | |
| 150 | if opts.clean: |
| 151 | _RemovePreviousUpdateBranch() |
| 152 | |
Mirko Bonadei | a776f51 | 2021-03-15 20:41:34 +0100 | [diff] [blame] | 153 | if _GetLastAuthor() == 'webrtc-version-updater': |
| 154 | logging.info('Last commit is a version change, skipping CL.') |
| 155 | return 0 |
| 156 | |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 157 | version_filename = os.path.join(CHECKOUT_SRC_DIR, 'call', 'version.cc') |
| 158 | _CreateUpdateBranch() |
| 159 | _UpdateWebRTCVersion(version_filename) |
| 160 | if _IsTreeClean(): |
Mirko Bonadei | a776f51 | 2021-03-15 20:41:34 +0100 | [diff] [blame] | 161 | logging.info('No WebRTC version change detected, skipping CL.') |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 162 | else: |
| 163 | _LocalCommit() |
| 164 | logging.info('Uploading CL...') |
Mirko Bonadei | a6c236f | 2020-12-14 21:45:37 +0100 | [diff] [blame] | 165 | _UploadCL(2) |
Mirko Bonadei | b985748 | 2020-12-14 15:28:43 +0100 | [diff] [blame] | 166 | return 0 |
| 167 | |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | sys.exit(main()) |