-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_sync.py
79 lines (64 loc) · 2.45 KB
/
build_sync.py
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
"""Convery async modules to sync equivalents using tokenisation."""
import subprocess
import sys
from pathlib import Path
import unasync
OUTPUT_FILES = ["pg_nearest_city/nearest_city.py", "tests/test_nearest_city.py"]
def build_sync():
"""Transform async code to sync versions with proper import handling."""
source_files = [
"pg_nearest_city/async_nearest_city.py",
"tests/test_async_nearest_city.py",
]
common_replacements = {
# Class and type replacements
"AsyncNearestCity": "NearestCity",
"async_nearest_city": "nearest_city",
"AsyncConnection": "Connection",
"AsyncCursor": "Cursor",
# Test-specific patterns (not working, but not required for now)
"pytest_asyncio": "pytest",
# "@pytest_asyncio": "@pytest",
# "@pytest_asyncio.fixture(loop_scope=\"function\")": "None",
# "@pytest.mark.asyncio(loop_scope=\"function\")": "None",
# "@pytest.mark.asyncio": "",
}
try:
unasync.unasync_files(
source_files,
rules=[
unasync.Rule(
"async_nearest_city.py",
"nearest_city.py",
additional_replacements=common_replacements,
),
unasync.Rule(
"test_async_nearest_city.py",
"test_nearest_city.py",
additional_replacements=common_replacements,
),
],
)
print("Transformation completed!")
# Verify with special focus on import statements
for output_file in OUTPUT_FILES:
if Path(output_file).exists():
print(f"\nSuccessfully created: {output_file}")
else:
print(f"Warning: Expected output file not found: {output_file}")
# Check if the output files were modified
result = subprocess.run(
["git", "diff", "--quiet", "--"] + OUTPUT_FILES, check=False
)
if result.returncode == 1:
print("Files were modified by unasync.")
sys.exit(0) # Allow pre-commit to continue
sys.exit(0) # No changes, allow pre-commit to continue
except Exception as e:
print(f"Error during transformation: {type(e).__name__}: {str(e)}")
import traceback
traceback.print_exc()
# Force failure of pre-commit hook
sys.exit(1)
if __name__ == "__main__":
build_sync()