|
#!/usr/bin/env python3
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SKIP_PREFIXES = (
|
|
ROOT / "devplacepy" / "database",
|
|
ROOT / "devplacepy_services" / "database",
|
|
ROOT / "devplacepy" / "db_client.py",
|
|
ROOT / "scripts",
|
|
ROOT / "splitup.md",
|
|
)
|
|
|
|
REPLACEMENTS = [
|
|
(re.compile(r"\bfrom devplacepy\.database import\b"), "from devplacepy.db_client import"),
|
|
(re.compile(r"\bfrom devplacepy\.database\.([a-z_]+) import\b"), r"from devplacepy.db_client import"),
|
|
(re.compile(r"\bimport devplacepy\.database\b"), "import devplacepy.db_client"),
|
|
]
|
|
|
|
|
|
def should_skip(path: Path) -> bool:
|
|
for prefix in SKIP_PREFIXES:
|
|
if path == prefix or str(path).startswith(str(prefix) + "/"):
|
|
return True
|
|
return False
|
|
|
|
|
|
def migrate_file(path: Path) -> bool:
|
|
text = path.read_text(encoding="utf-8")
|
|
original = text
|
|
for pattern, repl in REPLACEMENTS:
|
|
text = pattern.sub(repl, text)
|
|
if text != original:
|
|
path.write_text(text, encoding="utf-8")
|
|
return True
|
|
return False
|
|
|
|
|
|
def main() -> int:
|
|
changed = []
|
|
for pattern in ("devplacepy/**/*.py", "tests/**/*.py", "devplacepy_services/**/*.py"):
|
|
for path in ROOT.glob(pattern):
|
|
if not path.is_file() or should_skip(path):
|
|
continue
|
|
if migrate_file(path):
|
|
changed.append(path.relative_to(ROOT))
|
|
if changed:
|
|
print(f"migrated {len(changed)} files")
|
|
for item in sorted(changed)[:40]:
|
|
print(f" {item}")
|
|
if len(changed) > 40:
|
|
print(f" ... and {len(changed) - 40} more")
|
|
else:
|
|
print("no files changed")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main()) |