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