-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_sync.py
More file actions
74 lines (61 loc) · 2.03 KB
/
make_sync.py
File metadata and controls
74 lines (61 loc) · 2.03 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
import os
from pathlib import Path
import unasync
ADDITIONAL_REPLACEMENTS = {
"aredis_om": "redis_om",
"async_redis": "sync_redis",
"redis.asyncio as aioredis": "redis as aioredis",
":tests.": ":tests_sync.",
"pytest_asyncio": "pytest",
"py_test_mark_asyncio": "py_test_mark_sync",
"pytest.mark.asyncio(f)": "f",
"pytest.mark.asyncio": "py_test_mark_sync",
".aclose()": ".close()",
}
POST_SYNC_FIXES = {
"tests_sync/test_cluster_operations.py": {
"import redis.asyncio as aioredis": "import redis as aioredis",
"conn.aclose()": "conn.close()",
# In the generated sync mirror these call sites already contain eager
# return values, not coroutines, so the async gather wrapper must be
# removed.
"asyncio.gather(*tasks)": "tasks",
}
}
def apply_post_sync_fixes(repo_root: Path):
for relative_path, replacements in POST_SYNC_FIXES.items():
file_path = repo_root / relative_path
if not file_path.exists():
continue
content = file_path.read_text()
updated = content
for old, new in replacements.items():
updated = updated.replace(old, new)
if updated != content:
file_path.write_text(updated)
def main():
repo_root = Path(__file__).absolute().parent
rules = [
unasync.Rule(
fromdir="/aredis_om/",
todir="/redis_om/",
additional_replacements=ADDITIONAL_REPLACEMENTS,
),
unasync.Rule(
fromdir="/tests/",
todir="/tests_sync/",
additional_replacements=ADDITIONAL_REPLACEMENTS,
),
]
filepaths = []
for root, _, filenames in os.walk(repo_root):
for filename in filenames:
if filename.rpartition(".")[-1] in (
"py",
"pyi",
):
filepaths.append(os.path.join(root, filename))
unasync.unasync_files(filepaths, rules)
apply_post_sync_fixes(repo_root)
if __name__ == "__main__":
main()