blob: 5875c685fbc8336e09990ea8b224556b3f969373 [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()
101 commit_msg = ('Update WebRTC code version (%02d-%02d-%02dT%02d:%02d:%02d).'
102 '\n\nBugs: None')
103 commit_msg = commit_msg % (d.year, d.month, d.day, d.hour, d.minute,
104 d.second)
105 subprocess.check_call(['git', 'add', '--update', '.'])
106 subprocess.check_call(['git', 'commit', '-m', commit_msg])
107
108
109def _UploadCL(commit_queue_mode):
110 """Upload the committed changes as a changelist to Gerrit.
111
112 commit_queue_mode:
113 - 2: Submit to commit queue.
114 - 1: Run trybots but do not submit to CQ.
115 - 0: Skip CQ, upload only.
116 """
117 cmd = ['git', 'cl', 'upload', '--force', '--bypass-hooks',
118 '--cc=""', '--bypass-watchlist']
119 if commit_queue_mode >= 2:
120 logging.info('Sending the CL to the CQ...')
121 cmd.extend(['--use-commit-queue'])
122 elif commit_queue_mode >= 1:
123 logging.info('Starting CQ dry run...')
124 cmd.extend(['--cq-dry-run'])
125 subprocess.check_call(cmd)
126
127
128def main():
129 logging.basicConfig(level=logging.INFO)
130 p = argparse.ArgumentParser()
131 p.add_argument('--clean',
132 action='store_true',
133 default=False,
134 help='Removes any previous local update branch.')
135 opts = p.parse_args()
136
137 if opts.clean:
138 _RemovePreviousUpdateBranch()
139
140 version_filename = os.path.join(CHECKOUT_SRC_DIR, 'call', 'version.cc')
141 _CreateUpdateBranch()
142 _UpdateWebRTCVersion(version_filename)
143 if _IsTreeClean():
144 logging.info("No WebRTC version change detected, skipping CL.")
145 else:
146 _LocalCommit()
147 logging.info('Uploading CL...')
148 _UploadCL(1)
149 return 0
150
151
152if __name__ == '__main__':
153 sys.exit(main())