2021-11-10 00:59:10 +01:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2022-05-01 16:03:05 +02:00
|
|
|
from typing import Iterable, Optional, Union
|
2021-11-10 00:59:10 +01:00
|
|
|
|
|
|
|
import unasync
|
|
|
|
|
2021-11-10 20:31:02 +01:00
|
|
|
ADDITIONAL_REPLACEMENTS = {
|
|
|
|
"aredis_om": "redis_om",
|
|
|
|
"aioredis": "redis",
|
|
|
|
":tests.": ":tests_sync.",
|
2022-05-01 16:03:05 +02:00
|
|
|
"pytest_asyncio": "pytest",
|
2021-11-10 20:31:02 +01:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:03:05 +02:00
|
|
|
STRINGS_TO_REMOVE_FROM_SYNC_TESTS = {
|
|
|
|
"@pytest.mark.asyncio",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def remove_strings_from_files(
|
|
|
|
filepaths: Iterable[Union[bytes, str, os.PathLike]],
|
|
|
|
strings_to_remove: Iterable[str],
|
|
|
|
):
|
|
|
|
for filepath in filepaths:
|
|
|
|
tmp_filepath = f"{filepath}.tmp"
|
|
|
|
with open(filepath, "r") as read_file, open(tmp_filepath, "w") as write_file:
|
|
|
|
for line in read_file:
|
|
|
|
if line.strip() in strings_to_remove:
|
|
|
|
continue
|
|
|
|
print(line, end="", file=write_file)
|
|
|
|
os.replace(tmp_filepath, filepath)
|
|
|
|
|
|
|
|
|
|
|
|
def get_source_filepaths(directory: Optional[Union[bytes, str, os.PathLike]] = None):
|
|
|
|
walk_path = (
|
|
|
|
Path(__file__).absolute().parent
|
|
|
|
if directory is None
|
|
|
|
else os.path.join(Path(__file__).absolute().parent, directory)
|
|
|
|
)
|
|
|
|
|
|
|
|
filepaths = []
|
|
|
|
for root, _, filenames in os.walk(walk_path):
|
|
|
|
for filename in filenames:
|
|
|
|
if filename.rpartition(".")[-1] in (
|
|
|
|
"py",
|
|
|
|
"pyi",
|
|
|
|
):
|
|
|
|
filepaths.append(os.path.join(root, filename))
|
|
|
|
return filepaths
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
rules = [
|
|
|
|
unasync.Rule(
|
|
|
|
fromdir="/aredis_om/",
|
|
|
|
todir="/redis_om/",
|
2021-11-10 20:31:02 +01:00
|
|
|
additional_replacements=ADDITIONAL_REPLACEMENTS,
|
|
|
|
),
|
|
|
|
unasync.Rule(
|
|
|
|
fromdir="/tests/",
|
|
|
|
todir="/tests_sync/",
|
|
|
|
additional_replacements=ADDITIONAL_REPLACEMENTS,
|
2021-11-10 00:59:10 +01:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2022-05-01 16:03:05 +02:00
|
|
|
unasync.unasync_files(get_source_filepaths(), rules)
|
|
|
|
remove_strings_from_files(
|
|
|
|
get_source_filepaths("tests_sync"), STRINGS_TO_REMOVE_FROM_SYNC_TESTS
|
|
|
|
)
|
2021-11-10 00:59:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|