-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_clientv3.py
More file actions
51 lines (38 loc) · 1.3 KB
/
replace_clientv3.py
File metadata and controls
51 lines (38 loc) · 1.3 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
#!/usr/bin/env python3
"""逐文件替换 clientv3.New() 调用"""
import re
from pathlib import Path
def process_file(filepath):
with open(filepath, 'r') 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 replace(match):
endpoints = match.group(1)
timeout = match.group(2).strip()
return f'NewEtcdClient({endpoints}, {timeout})'
content = re.sub(pattern, replace, content, flags=re.DOTALL)
if content != original:
with open(filepath, 'w') as f:
f.write(content)
return True
return False
test_dir = Path('test')
modified = []
for test_file in sorted(test_dir.glob('*_test.go')):
if process_file(test_file):
modified.append(test_file.name)
print(f'✓ {test_file.name}')
print(f'\n修改了 {len(modified)} 个文件')
# 验证
remaining = 0
for test_file in test_dir.glob('*_test.go'):
with open(test_file) as f:
count = f.read().count('clientv3.New(clientv3.Config{')
remaining += count
print(f'剩余 clientv3.New() 调用: {remaining}')