-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathmake_sync.py
More file actions
140 lines (114 loc) · 4.47 KB
/
make_sync.py
File metadata and controls
140 lines (114 loc) · 4.47 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
import os
import re
import shutil
from pathlib import Path
import unasync
# Files to preserve in redis_om/ directory (not overwritten by sync)
PRESERVED_FILES = {"README.md"}
ADDITIONAL_REPLACEMENTS = {
"aredis_om": "redis_om",
"async_redis": "sync_redis",
":tests.": ":tests_sync.",
"pytest_asyncio": "pytest",
"py_test_mark_asyncio": "py_test_mark_sync",
"AsyncMock": "Mock",
# RedisVL uses SearchIndex for sync, not SyncSearchIndex
"AsyncSearchIndex": "SearchIndex",
# Fix supports_hash_field_expiration to check sync Redis class
"redis_lib.asyncio.Redis": "redis_lib.Redis",
}
def clean_generated_dir(dirpath: Path, preserved: set):
"""Remove generated files from a directory while preserving specified files."""
if not dirpath.exists():
return
for item in dirpath.iterdir():
if item.name in preserved:
continue
if item.is_dir():
shutil.rmtree(item)
else:
item.unlink()
def main():
base_dir = Path(__file__).absolute().parent
# Clean generated directories before regenerating (preserve README.md)
redis_om_dir = base_dir / "redis_om"
tests_sync_dir = base_dir / "tests_sync"
clean_generated_dir(redis_om_dir, PRESERVED_FILES)
clean_generated_dir(tests_sync_dir, set())
rules = [
unasync.Rule(
fromdir="/aredis_om/",
todir="/redis_om/",
additional_replacements=ADDITIONAL_REPLACEMENTS,
),
unasync.Rule(
fromdir="/tests/",
todir="/tests_sync/",
additional_replacements=ADDITIONAL_REPLACEMENTS,
),
]
# Files to exclude from sync generation (benchmarks require special async handling)
excluded_files = {"test_benchmarks.py"}
filepaths = []
for root, _, filenames in os.walk(base_dir):
for filename in filenames:
if filename in excluded_files:
continue
if filename.rpartition(".")[-1] in (
"py",
"pyi",
):
filepaths.append(os.path.join(root, filename))
unasync.unasync_files(filepaths, rules)
# Post-process CLI files to remove run_async() wrappers
cli_files = [
"redis_om/model/cli/migrate_data.py",
"redis_om/model/cli/migrate.py"
]
for cli_file in cli_files:
file_path = Path(__file__).absolute().parent / cli_file
if file_path.exists():
with open(file_path, 'r') as f:
content = f.read()
# Remove run_async() call wrappers (not the function definition)
# Only match run_async() calls that are not function definitions
def remove_run_async_call(match):
inner_content = match.group(1)
return inner_content
# Pattern to match run_async() function calls (not definitions)
# Look for = or return statements followed by run_async(...)
lines = content.split('\n')
new_lines = []
for line in lines:
# Skip function definitions
if 'def run_async(' in line:
new_lines.append(line)
continue
# Replace run_async() calls
if 'run_async(' in line and ('=' in line or 'return ' in line or line.strip().startswith('run_async(')):
# Simple pattern for function calls
line = re.sub(r'run_async\(([^)]+(?:\([^)]*\)[^)]*)*)\)', r'\1', line)
new_lines.append(line)
content = '\n'.join(new_lines)
with open(file_path, 'w') as f:
f.write(content)
# Post-process model.py to fix async imports for sync version
model_file = Path(__file__).absolute().parent / "redis_om/model/model.py"
if model_file.exists():
with open(model_file, 'r') as f:
content = f.read()
# Fix supports_hash_field_expiration to check sync Redis class
# The unasync replacement doesn't work for dotted attribute access
content = content.replace(
'redis_lib.asyncio.Redis',
'redis_lib.Redis'
)
# Fix Pipeline import: redis.asyncio.client -> redis.client
content = content.replace(
'from redis.asyncio.client import Pipeline',
'from redis.client import Pipeline'
)
with open(model_file, 'w') as f:
f.write(content)
if __name__ == "__main__":
main()