-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_etcd_client_all.py
More file actions
55 lines (43 loc) · 1.55 KB
/
fix_etcd_client_all.py
File metadata and controls
55 lines (43 loc) · 1.55 KB
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
#!/usr/bin/env python3
"""
批量替换测试文件中的 clientv3.New() 调用为 NewEtcdClient()
这样可以抑制 lease keep-alive 警告日志
"""
import re
import os
from pathlib import Path
def replace_in_file(filepath):
"""替换单个文件中的 clientv3.New() 调用"""
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
original = content
# 模式:匹配 clientv3.New(clientv3.Config{...})
# 提取 Endpoints 和 DialTimeout
pattern = r'clientv3\.New\(clientv3\.Config\{\s*Endpoints:\s*(\[[^\]]+\]),\s*DialTimeout:\s*([^,\}]+),?\s*\}\)'
def replacer(match):
endpoints = match.group(1).strip()
timeout = match.group(2).strip().rstrip(',')
return f'NewEtcdClient({endpoints}, {timeout})'
content = re.sub(pattern, replacer, content, flags=re.MULTILINE | re.DOTALL)
if content != original:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return True
return False
def main():
test_dir = Path('test')
test_files = list(test_dir.glob('*_test.go'))
modified_count = 0
modified_files = []
for filepath in test_files:
if replace_in_file(filepath):
modified_count += 1
modified_files.append(filepath.name)
print(f'✓ 修改: {filepath.name}')
print(f'\n总共修改了 {modified_count} 个文件')
if modified_files:
print('\n修改的文件列表:')
for name in modified_files:
print(f' - {name}')
if __name__ == '__main__':
main()