blob: 3c2be3fe7524f21943706551e3e4bdf842c8411d [file] [log] [blame]
Mirko Bonadeib9857482020-12-14 15:28:43 +01001#!/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
12import argparse
13import datetime
14import logging
15import os
16import re
17import subprocess
18import sys
19
20
21def 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
29UPDATE_BRANCH_NAME = 'webrtc_version_update'
30CHECKOUT_SRC_DIR = FindSrcDirPath()
31
32
33def _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 Bonadeia776f512021-03-15 20:41:34 +010045def _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 Bonadeib9857482020-12-14 15:28:43 +010053def _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
74def _CreateUpdateBranch():
75 logging.info('Creating update branch: %s', UPDATE_BRANCH_NAME)
76 subprocess.check_call(['git', 'checkout', '-b', UPDATE_BRANCH_NAME])
77
78
79def _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
99def _IsTreeClean():
100 stdout = subprocess.check_output(['git', 'status', '--porcelain'])
101 if len(stdout) == 0:
102 return True
103 return False
104
105
106def _LocalCommit():
107 logging.info('Committing changes locally.')
108 d = datetime.datetime.utcnow()
Mirko Bonadeia6c236f2020-12-14 21:45:37 +0100109
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 Bonadeib9857482020-12-14 15:28:43 +0100114 commit_msg = ('Update WebRTC code version (%02d-%02d-%02dT%02d:%02d:%02d).'
Mirko Bonadeib08b23e2020-12-15 13:15:01 +0100115 '\n\nTBR=%s\nBug: None')
Mirko Bonadeib9857482020-12-14 15:28:43 +0100116 commit_msg = commit_msg % (d.year, d.month, d.day, d.hour, d.minute,
Mirko Bonadeia6c236f2020-12-14 21:45:37 +0100117 d.second, tbr_authors)
Mirko Bonadeib9857482020-12-14 15:28:43 +0100118 subprocess.check_call(['git', 'add', '--update', '.'])
119 subprocess.check_call(['git', 'commit', '-m', commit_msg])
120
121
122def _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
141def 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 Bonadeia776f512021-03-15 20:41:34 +0100153 if _GetLastAuthor() == 'webrtc-version-updater':
154 logging.info('Last commit is a version change, skipping CL.')
155 return 0
156
Mirko Bonadeib9857482020-12-14 15:28:43 +0100157 version_filename = os.path.join(CHECKOUT_SRC_DIR, 'call', 'version.cc')
158 _CreateUpdateBranch()
159 _UpdateWebRTCVersion(version_filename)
160 if _IsTreeClean():
Mirko Bonadeia776f512021-03-15 20:41:34 +0100161 logging.info('No WebRTC version change detected, skipping CL.')
Mirko Bonadeib9857482020-12-14 15:28:43 +0100162 else:
163 _LocalCommit()
164 logging.info('Uploading CL...')
Mirko Bonadeia6c236f2020-12-14 21:45:37 +0100165 _UploadCL(2)
Mirko Bonadeib9857482020-12-14 15:28:43 +0100166 return 0
167
168
169if __name__ == '__main__':
170 sys.exit(main())