-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_etcd_client.py
More file actions
65 lines (50 loc) · 1.87 KB
/
replace_etcd_client.py
File metadata and controls
65 lines (50 loc) · 1.87 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
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
"""
Replace clientv3.New(clientv3.Config{...}) with NewEtcdClient(...)
to suppress lease keep-alive warnings in tests.
"""
import re
import sys
from pathlib import Path
def replace_clientv3_new(content):
"""Replace clientv3.New patterns with NewEtcdClient."""
# Pattern to match clientv3.New(clientv3.Config{...}) across multiple lines
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})'
return re.sub(pattern, replacer, content, flags=re.DOTALL)
def process_file(filepath):
"""Process a single file."""
try:
with open(filepath, 'r', encoding='utf-8') as f:
original = f.read()
modified = replace_clientv3_new(original)
if modified != original:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(modified)
return True
return False
except Exception as e:
print(f"Error processing {filepath}: {e}", file=sys.stderr)
return False
def main():
"""Main function."""
test_dir = Path('test')
test_files = list(test_dir.glob('*_test.go'))
print(f"Processing {len(test_files)} test files...")
modified_count = 0
for filepath in test_files:
if process_file(filepath):
print(f" Modified: {filepath}")
modified_count += 1
print(f"\nTotal files modified: {modified_count}")
# Count NewEtcdClient usage
total_new_usage = 0
for filepath in test_files:
with open(filepath, 'r') as f:
total_new_usage += f.read().count('NewEtcdClient')
print(f"Total NewEtcdClient calls: {total_new_usage}")
if __name__ == '__main__':
main()