forked from DreamLab-AI/origin-logseq-AR
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmigrate_responses_final.py
More file actions
executable file
·201 lines (154 loc) · 7.57 KB
/
migrate_responses_final.py
File metadata and controls
executable file
·201 lines (154 loc) · 7.57 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
"""
Final Comprehensive HTTP Response Migration
Handles all remaining patterns including multi-line JSON and complex error objects
"""
import re
import sys
from pathlib import Path
class FinalMigrator:
def __init__(self):
self.stats = {
'files_modified': 0,
'responses_migrated': 0,
'manual_review': []
}
def migrate_multiline_errors(self, content: str) -> tuple[str, int]:
"""Migrate multi-line error responses"""
migrations = 0
# Pattern: HttpResponse::InternalServerError().json(serde_json::json!({...}))
# Spanning multiple lines
pattern = r'HttpResponse::InternalServerError\(\)\.json\((?:serde_)?json!\(\s*\{\s*"error"\s*:\s*(?:"([^"]+)"|format!\("([^"]+)"(?:,\s*([^)]+))?\))\s*(?:,\s*"message"\s*:\s*[^}]+)?\s*\}\s*\)\)'
def replace_internal_error(match):
nonlocal migrations
migrations += 1
error_msg = match.group(1) or match.group(2)
if match.group(3): # has format args
return f'error_json!("{error_msg}", {match.group(3)})'
return f'error_json!("{error_msg}")'
content = re.sub(pattern, replace_internal_error, content, flags=re.MULTILINE | re.DOTALL)
# Pattern: HttpResponse::BadRequest().json(...)
pattern = r'HttpResponse::BadRequest\(\)\.json\((?:serde_)?json!\(\s*\{\s*"error"\s*:\s*(?:"([^"]+)"|format!\("([^"]+)"(?:,\s*([^)]+))?\))\s*\}\s*\)\)'
def replace_bad_request(match):
nonlocal migrations
migrations += 1
error_msg = match.group(1) or match.group(2)
if match.group(3):
return f'bad_request!("{error_msg}", {match.group(3)})'
return f'bad_request!("{error_msg}")'
content = re.sub(pattern, replace_bad_request, content, flags=re.MULTILINE | re.DOTALL)
# Pattern: HttpResponse::NotFound().json(...)
pattern = r'HttpResponse::NotFound\(\)\.json\((?:serde_)?json!\(\s*\{\s*"error"\s*:\s*(?:"([^"]+)"|format!\("([^"]+)"(?:,\s*([^)]+))?\))\s*\}\s*\)\)'
def replace_not_found(match):
nonlocal migrations
migrations += 1
error_msg = match.group(1) or match.group(2)
if match.group(3):
return f'not_found!("{error_msg}", {match.group(3)})'
return f'not_found!("{error_msg}")'
content = re.sub(pattern, replace_not_found, content, flags=re.MULTILINE | re.DOTALL)
# Pattern: HttpResponse::ServiceUnavailable().json(...)
pattern = r'HttpResponse::ServiceUnavailable\(\)\.json\((?:serde_)?json!\(\s*\{\s*"error"\s*:\s*"([^"]+)"\s*\}\s*\)\)'
def replace_service_unavail(match):
nonlocal migrations
migrations += 1
return f'service_unavailable!("{match.group(1)}")'
content = re.sub(pattern, replace_service_unavail, content, flags=re.MULTILINE | re.DOTALL)
# Pattern: HttpResponse::TooManyRequests().json(...)
pattern = r'HttpResponse::TooManyRequests\(\)\.json\((?:serde_)?json!\(\s*\{\s*"error"\s*:\s*"([^"]+)"\s*\}\s*\)\)'
def replace_too_many(match):
nonlocal migrations
migrations += 1
return f'too_many_requests!("{match.group(1)}")'
content = re.sub(pattern, replace_too_many, content, flags=re.MULTILINE | re.DOTALL)
# Pattern: HttpResponse::PayloadTooLarge().json(...)
pattern = r'HttpResponse::PayloadTooLarge\(\)\.json\((?:serde_)?json!\(\s*\{\s*"error"\s*:\s*"([^"]+)"\s*\}\s*\)\)'
def replace_payload_large(match):
nonlocal migrations
migrations += 1
return f'payload_too_large!("{match.group(1)}")'
content = re.sub(pattern, replace_payload_large, content, flags=re.MULTILINE | re.DOTALL)
return content, migrations
def ensure_imports(self, content: str, file_path: Path) -> str:
"""Ensure response macro imports are present"""
macros = ['ok_json', 'error_json', 'bad_request', 'not_found',
'too_many_requests', 'service_unavailable', 'payload_too_large']
has_imports = any(f'{macro}!' in content for macro in macros)
if not has_imports:
return content
# Check if imports already exist
for macro in macros:
if f'use crate::{{{macro}' in content or f'use crate::{macro}' in content:
return content
# Add imports after the last use statement
use_pattern = r'(use\s+[^;]+;)'
matches = list(re.finditer(use_pattern, content))
if matches:
last_use = matches[-1]
insert_pos = last_use.end()
import_statement = '''
use crate::{
ok_json, created_json, error_json, bad_request, not_found,
unauthorized, forbidden, conflict, no_content, accepted,
too_many_requests, service_unavailable, payload_too_large
};
'''
content = content[:insert_pos] + import_statement + content[insert_pos:]
print(f" + Added macro imports")
return content
def migrate_file(self, file_path: Path) -> bool:
"""Migrate a single file"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Skip if no HttpResponse patterns
if 'HttpResponse::' not in content or 'use actix_web' not in content:
return False
original_content = content
content, migrations = self.migrate_multiline_errors(content)
if migrations > 0:
content = self.ensure_imports(content, file_path)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
self.stats['files_modified'] += 1
self.stats['responses_migrated'] += migrations
print(f" ✓ Migrated {migrations} responses")
return True
return False
except Exception as e:
print(f" ✗ Error: {str(e)}", file=sys.stderr)
return False
def migrate_directory(self, directory: Path):
"""Migrate all handler files"""
rs_files = sorted(directory.rglob('*.rs'))
print(f"\nFinal Migration Pass - {len(rs_files)} files")
print("=" * 60)
for file_path in rs_files:
if '/tests/' in str(file_path):
continue
# Only process files with HttpResponse patterns
try:
with open(file_path, 'r') as f:
if 'HttpResponse::' in f.read():
print(f"\n{file_path.relative_to(directory.parent)}")
self.migrate_file(file_path)
except:
continue
def print_summary(self):
"""Print migration summary"""
print("\n" + "=" * 60)
print("FINAL MIGRATION SUMMARY")
print("=" * 60)
print(f"Files modified: {self.stats['files_modified']}")
print(f"Responses migrated: {self.stats['responses_migrated']}")
def main():
project_root = Path(__file__).parent.parent
handlers_dir = project_root / 'src' / 'handlers'
if not handlers_dir.exists():
print(f"ERROR: Handlers directory not found", file=sys.stderr)
sys.exit(1)
migrator = FinalMigrator()
migrator.migrate_directory(handlers_dir)
migrator.print_summary()
if __name__ == '__main__':
main()