forked from luxfi/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_fix.py
More file actions
103 lines (85 loc) · 2.69 KB
/
final_fix.py
File metadata and controls
103 lines (85 loc) · 2.69 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
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
#!/usr/bin/env python3
"""
Final comprehensive fix for all metrics issues
"""
import os
import re
import subprocess
def fix_file(filepath):
"""Fix a single file"""
with open(filepath, 'r') as f:
content = f.read()
original = content
# Fix NewAPIInterceptor calls
content = re.sub(
r'metric\.NewAPIInterceptor\((\w+)\)',
r'metric.NewAPIInterceptor(metrics.NewWithRegistry("", \1))',
content
)
# Fix NewAverager calls with Registry
content = re.sub(
r'metric\.NewAverager\([^,]+,\s*[^,]+,\s*(reg|registerer|registry)\)',
r'metric.NewAverager(\1, \2, metrics.NewWithRegistry("", \3))',
content
)
# Fix duplicate imports
lines = content.split('\n')
new_lines = []
import_seen = {}
in_imports = False
for line in lines:
if 'import (' in line:
in_imports = True
import_seen = {}
elif in_imports and line.strip() == ')':
in_imports = False
elif in_imports:
# Track imports
if '"github.com/luxfi/metric"' in line:
if 'metrics' not in import_seen:
import_seen['metrics'] = True
if '"github.com/luxfi/node/api/metrics"' in import_seen:
line = '\tluxmetrics "github.com/luxfi/metric"'
else:
continue # Skip duplicate
elif '"github.com/luxfi/node/api/metrics"' in line:
import_seen['"github.com/luxfi/node/api/metrics"'] = True
new_lines.append(line)
content = '\n'.join(new_lines)
# Fix metrics.NewRegistry() calls
content = re.sub(
r'metrics\.NewRegistry\(\)',
r'metrics.NewPrometheusRegistry()',
content
)
# Fix undefined luxmetrics
content = re.sub(
r'undefined: luxmetrics',
r'luxmetrics',
content
)
# Write back if changed
if content != original:
with open(filepath, 'w') as f:
f.write(content)
return True
return False
def main():
# Find all Go files
go_files = []
for root, dirs, files in os.walk('.'):
if 'vendor' in root:
continue
for file in files:
if file.endswith('.go'):
go_files.append(os.path.join(root, file))
# Fix all files
for filepath in go_files:
if fix_file(filepath):
print(f"Fixed {filepath}")
# Run goimports to fix import formatting
print("\nRunning goimports...")
subprocess.run(['goimports', '-w', '.'], capture_output=True)
print("\nDone!")
if __name__ == '__main__':
main()