blob: d5a72b8710cd9d19a8fb72db5755cef4d7e42e70 [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
45def _GetBranches():
46 """Returns a tuple (active, branches).
47
48 'active' is a string with name of the currently active branch, while
49 'branches' is the list of all branches.
50 """
51 lines = subprocess.check_output(['git', 'branch']).splitlines()
52 branches = []
53 active = ''
54 for line in lines:
55 if '*' in line:
56 # The assumption is that the first char will always be the '*'.
57 active = line[1:].strip()
58 branches.append(active)
59 else:
60 branch = line.strip()
61 if branch:
62 branches.append(branch)
63 return active, branches
64
65
66def _CreateUpdateBranch():
67 logging.info('Creating update branch: %s', UPDATE_BRANCH_NAME)
68 subprocess.check_call(['git', 'checkout', '-b', UPDATE_BRANCH_NAME])
69
70
71def _UpdateWebRTCVersion(filename):
72 with open(filename) as f:
73 content = f.read()
74 d = datetime.datetime.utcnow()
75 # pylint: disable=line-too-long
76 new_content = re.sub(
77 r'WebRTC source stamp [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}',
78 r'WebRTC source stamp %02d-%02d-%02dT%02d:%02d:%02d' % (d.year,
79 d.month,
80 d.day,
81 d.hour,
82 d.minute,
83 d.second),
84 content,
85 flags=re.MULTILINE)
86 # pylint: enable=line-too-long
87 with open(filename, 'w') as f:
88 f.write(new_content)
89
90
91def _IsTreeClean():
92 stdout = subprocess.check_output(['git', 'status', '--porcelain'])
93 if len(stdout) == 0:
94 return True
95 return False
96
97
98def _LocalCommit():
99 logging.info('Committing changes locally.')
100 d = datetime.datetime.utcnow()
Mirko Bonadeia6c236f2020-12-14 21:45:37 +0100101
102 git_author = subprocess.check_output(['git', 'config',
103 'user.email']).strip()
104 tbr_authors = git_author + ',' + 'mbonadei@webrtc.org'
105 tbr = 'TBR=%s' % tbr_authors
Mirko Bonadeib9857482020-12-14 15:28:43 +0100106 commit_msg = ('Update WebRTC code version (%02d-%02d-%02dT%02d:%02d:%02d).'
Mirko Bonadeib08b23e2020-12-15 13:15:01 +0100107 '\n\nTBR=%s\nBug: None')
Mirko Bonadeib9857482020-12-14 15:28:43 +0100108 commit_msg = commit_msg % (d.year, d.month, d.day, d.hour, d.minute,
Mirko Bonadeia6c236f2020-12-14 21:45:37 +0100109 d.second, tbr_authors)
Mirko Bonadeib9857482020-12-14 15:28:43 +0100110 subprocess.check_call(['git', 'add', '--update', '.'])
111 subprocess.check_call(['git', 'commit', '-m', commit_msg])
112
113
114def _UploadCL(commit_queue_mode):
115 """Upload the committed changes as a changelist to Gerrit.
116
117 commit_queue_mode:
118 - 2: Submit to commit queue.
119 - 1: Run trybots but do not submit to CQ.
120 - 0: Skip CQ, upload only.
121 """
122 cmd = ['git', 'cl', 'upload', '--force', '--bypass-hooks',
123 '--cc=""', '--bypass-watchlist']
124 if commit_queue_mode >= 2:
125 logging.info('Sending the CL to the CQ...')
126 cmd.extend(['--use-commit-queue'])
127 elif commit_queue_mode >= 1:
128 logging.info('Starting CQ dry run...')
129 cmd.extend(['--cq-dry-run'])
130 subprocess.check_call(cmd)
131
132
133def main():
134 logging.basicConfig(level=logging.INFO)
135 p = argparse.ArgumentParser()
136 p.add_argument('--clean',
137 action='store_true',
138 default=False,
139 help='Removes any previous local update branch.')
140 opts = p.parse_args()
141
142 if opts.clean:
143 _RemovePreviousUpdateBranch()
144
145 version_filename = os.path.join(CHECKOUT_SRC_DIR, 'call', 'version.cc')
146 _CreateUpdateBranch()
147 _UpdateWebRTCVersion(version_filename)
148 if _IsTreeClean():
149 logging.info("No WebRTC version change detected, skipping CL.")
150 else:
151 _LocalCommit()
152 logging.info('Uploading CL...')
Mirko Bonadeia6c236f2020-12-14 21:45:37 +0100153 _UploadCL(2)
Mirko Bonadeib9857482020-12-14 15:28:43 +0100154 return 0
155
156
157if __name__ == '__main__':
158 sys.exit(main())