-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-clang-stable.py
executable file
·130 lines (101 loc) · 4.62 KB
/
update-clang-stable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
#
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=not-callable, relative-import
""" Update clang-stable """
import argparse
import logging
from pathlib import Path
import shutil
import subprocess
from typing import List, Optional
import paths
import utils
class ArgParser(argparse.ArgumentParser):
def __init__(self):
super().__init__(description=__doc__)
self.add_argument(
'version', metavar='VERSION',
help='Version of binutil prebuilt updating to (e.g. r123456a).')
self.add_argument(
'-b', '--bug', type=int,
help='Bug to reference in commit message.')
self.add_argument(
'--use-current-branch', action='store_true',
help='Do not repo start a new branch for the update.')
class ClangStableBuilder:
def __init__(self, prebuilt_dir: Path, version: str):
self.version = version
self.clang_dir = prebuilt_dir / ('clang-' + version)
self.stable_dir = prebuilt_dir / 'clang-stable'
def build(self):
shutil.rmtree(self.stable_dir)
self.stable_dir.mkdir()
(self.stable_dir / 'bin').mkdir()
(self.stable_dir / 'lib').mkdir()
(self.stable_dir / 'lib' / 'x86_64-unknown-linux-gnu').mkdir()
(self.stable_dir / 'share').mkdir()
self.copy_file(self.clang_dir / 'bin' / 'clang-format', self.stable_dir / 'bin')
self.copy_file(self.clang_dir / 'bin' / 'git-clang-format', self.stable_dir / 'bin')
self.copy_files((self.clang_dir / 'lib').glob('libclang.*'), self.stable_dir / 'lib')
self.copy_dir(self.clang_dir / 'lib' / 'python3', self.stable_dir / 'lib')
self.copy_files((self.clang_dir / 'lib' / 'x86_64-unknown-linux-gnu').glob('libc++*'),
self.stable_dir / 'lib' / 'x86_64-unknown-linux-gnu')
self.copy_dir(self.clang_dir / 'share' / 'clang', self.stable_dir / 'share')
(self.stable_dir / 'README.md').write_text(
f'All contents in clang-stable are copies of clang-{self.version}.')
def copy_file(self, src_file: Path, dst_dir: Path):
shutil.copy(src_file, dst_dir, follow_symlinks=False)
def copy_files(self, src_files: List[Path], dst_dir: Path):
for src_file in src_files:
self.copy_file(src_file, dst_dir)
def copy_dir(self, src_dir: Path, dst_parent_dir: Path):
shutil.copytree(src_dir, dst_parent_dir / src_dir.name,
symlinks=True, ignore_dangling_symlinks=True)
def test(self):
utils.check_call([self.stable_dir / 'bin' / 'clang-format', '-h'],
stdout=subprocess.DEVNULL)
utils.check_call([self.stable_dir / 'bin' / 'git-clang-format', '-h'],
stdout=subprocess.DEVNULL)
def update_clang_stable(prebuilt_dir: Path, version: str, use_cbr: bool):
if not use_cbr:
utils.unchecked_call(['repo', 'abandon', 'update-clang-stable-' + version, prebuilt_dir],
stderr=subprocess.DEVNULL)
utils.check_call(['repo', 'start', 'update-clang-stable-' + version, prebuilt_dir])
builder = ClangStableBuilder(prebuilt_dir, version)
builder.build()
builder.test()
def do_commit(prebuilt_dir: Path, version: str, bug_id: Optional[str]):
utils.check_call(['git', 'add', 'clang-stable'], cwd=prebuilt_dir)
message_lines = []
message_lines.append(f'Update clang-stable to {version}.')
message_lines.append('')
message_lines.append('Test: N/A')
if bug_id is not None:
message_lines.append(f'Bug: http://b/{bug_id}')
message = '\n'.join(message_lines)
utils.check_call(['git', 'commit', '-m', message], cwd=prebuilt_dir)
def main():
logging.basicConfig(level=logging.DEBUG)
args = ArgParser().parse_args()
bug_id = args.bug
use_cbr = args.use_current_branch
version = args.version
prebuilt_dir = paths.PREBUILTS_DIR / 'clang' / 'host' / 'linux-x86'
update_clang_stable(prebuilt_dir, version, use_cbr)
do_commit(prebuilt_dir, version, bug_id)
if __name__ == '__main__':
main()