Spaces:
Running

File size: 9,319 Bytes
25834e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Move the application into the ``app/`` package and rewrite imports.



Idempotent: a second run changes nothing. Branches cut before the move can

rebase onto main and re-run this to carry their own new files and imports

across, then run the tests.



    python scripts/move_to_package.py

"""

from __future__ import annotations

import re
import shutil
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
PKG = ROOT / "app"

PACKAGES = (
    "routes",
    "middleware",
    "dependencies",
    "services",
    "repositories",
    "schemas",
    "infrastructure",
    "shared",
    "workers",
)
FILE_MODULES = ("config", "errors", "main")
OLD_MODULES = PACKAGES + FILE_MODULES

MOVES = [
    ("main.py", "app/main.py"),
    ("app.py", "app/factory.py"),
    ("config.py", "app/config.py"),
    ("errors.py", "app/errors.py"),
    ("config/apps.yaml", "app/data/apps.yaml"),
    *((name, f"app/{name}") for name in PACKAGES),
    ("data", "app/data"),
    ("static", "app/static"),
    ("templates", "app/templates"),
]

SCAN_DIRS = ("app", "tests", "scripts")
SELF = Path(__file__).resolve()

_MOD = "|".join(OLD_MODULES)
FROM_RE = re.compile(rf"^(\s*)from ({_MOD})((?:\.\w+)*) import\b", re.M)
IMPORT_DOTTED_RE = re.compile(rf"^(\s*)import ({_MOD})((?:\.\w+)+)(\s+as\s+\w+)", re.M)
IMPORT_BARE_RE = re.compile(rf"^(\s*)import ({_MOD})[ \t]*$", re.M)
IMPORT_UNALIASED_RE = re.compile(rf"^\s*import ({_MOD})\.\w+[\w.]*[ \t]*$", re.M)
FROM_APP_RE = re.compile(r"^(\s*)from app import (\(?)([^\n]*)", re.M)
IMPORT_APP_RE = re.compile(r"^\s*import app[ \t]*$", re.M)
STRING_RE = re.compile(r"""(["'])((?:\w+)(?:\.\w+)+(?::\w+)?)\1""")


def git(*args: str) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True
    )


def is_tracked(path: Path) -> bool:
    out = git("ls-files", "--", str(path.relative_to(ROOT))).stdout
    return bool(out.strip())


def move_file(src: Path, dst: Path, moved: list[str], problems: list[str]) -> None:
    if dst.exists():
        problems.append(
            f"both exist, left alone: {src.relative_to(ROOT)} and {dst.relative_to(ROOT)}"
        )
        return
    dst.parent.mkdir(parents=True, exist_ok=True)
    if is_tracked(src):
        git("mv", str(src.relative_to(ROOT)), str(dst.relative_to(ROOT)))
    else:
        src.rename(dst)
    moved.append(f"{src.relative_to(ROOT)} -> {dst.relative_to(ROOT)}")


def remove_empty_dirs(path: Path) -> None:
    for sub in sorted(path.rglob("*"), key=lambda p: len(p.parts), reverse=True):
        if sub.is_dir() and sub.name == "__pycache__":
            shutil.rmtree(sub)
        elif sub.is_dir() and not any(sub.iterdir()):
            sub.rmdir()
    if path.is_dir() and not any(path.iterdir()):
        path.rmdir()


def move_paths() -> tuple[list[str], list[str]]:
    moved: list[str] = []
    problems: list[str] = []
    PKG.mkdir(exist_ok=True)
    init = PKG / "__init__.py"
    if not init.exists():
        init.touch()
        git("add", "app/__init__.py")
        moved.append("created app/__init__.py")

    for old, new in MOVES:
        src, dst = ROOT / old, ROOT / new
        if not src.exists():
            continue
        if src.is_file():
            move_file(src, dst, moved, problems)
            continue
        if not dst.exists():
            dst.parent.mkdir(parents=True, exist_ok=True)
            git("mv", old, new)
            moved.append(f"{old}/ -> {new}/")
            continue
        for path in sorted(src.rglob("*")):
            if path.is_file() and "__pycache__" not in path.parts:
                move_file(path, dst / path.relative_to(src), moved, problems)
        remove_empty_dirs(src)

    if (ROOT / "config").is_dir():
        remove_empty_dirs(ROOT / "config")
    return moved, problems


def defined_names(module_file: Path) -> set[str]:
    if not module_file.is_file():
        return set()
    text = module_file.read_text()
    names = set(re.findall(r"^(?:async\s+)?(?:def|class)\s+(\w+)", text, re.M))
    names |= set(re.findall(r"^(\w+)\s*[:=]", text, re.M))
    for imported in re.findall(
        r"^\s*(?:from\s+\S+\s+)?import\s+\(?([^)\n]*)", text, re.M
    ):
        for part in imported.split(","):
            bits = part.split()
            if bits:
                names.add(bits[-1])
    for block in re.findall(r"^\s*from\s+\S+\s+import\s+\(([^)]*)\)", text, re.M):
        for part in block.split(","):
            bits = part.split()
            if bits:
                names.add(bits[-1])
    return names


def package_children() -> set[str]:
    return {p.stem for p in PKG.iterdir() if p.suffix == ".py" or p.is_dir()} - {
        "__init__",
        "__pycache__",
    }


def rewrite_string(

    match: re.Match[str], file_names: dict[str, set[str]], children: set[str]

) -> str:
    quote, value = match.group(1), match.group(2)
    dotted, _, attr = value.partition(":")
    head, second = dotted.split(".")[:2]
    if head in PACKAGES:
        target = PKG / head / second
        if not (target.is_dir() or target.with_suffix(".py").is_file()):
            return match.group(0)
        new = f"app.{dotted}"
    elif head in FILE_MODULES:
        if second not in file_names[head]:
            return match.group(0)
        new = f"app.{dotted}"
    elif head == "app":
        if second in children or second not in file_names["factory"]:
            return match.group(0)
        new = "app.factory." + dotted.split(".", 1)[1]
    else:
        return match.group(0)
    return f"{quote}{new}{':' + attr if attr else ''}{quote}"


def rewrite_from_app(

    match: re.Match[str], children: set[str], problems: list[str], rel: str

) -> str:
    indent, paren, rest = match.groups()
    first = re.split(r"[\s,()#]+", rest.strip())[0] if rest.strip() else ""
    if not first:
        problems.append(f"{rel}: multi-line 'from app import (' needs a manual look")
        return match.group(0)
    if first in children:
        return match.group(0)
    return f"{indent}from app.factory import {paren}{rest}"


def rewrite_source(

    text: str,

    rel: str,

    file_names: dict[str, set[str]],

    children: set[str],

    problems: list[str],

) -> str:
    text = FROM_APP_RE.sub(lambda m: rewrite_from_app(m, children, problems, rel), text)
    text = FROM_RE.sub(
        lambda m: f"{m.group(1)}from app.{m.group(2)}{m.group(3)} import", text
    )
    text = IMPORT_DOTTED_RE.sub(
        lambda m: f"{m.group(1)}import app.{m.group(2)}{m.group(3)}{m.group(4)}", text
    )
    text = IMPORT_BARE_RE.sub(
        lambda m: f"{m.group(1)}from app import {m.group(2)}", text
    )
    text = STRING_RE.sub(lambda m: rewrite_string(m, file_names, children), text)
    for m in IMPORT_UNALIASED_RE.finditer(text):
        problems.append(f"{rel}: unaliased '{m.group(0).strip()}' needs a manual look")
    if IMPORT_APP_RE.search(text):
        problems.append(f"{rel}: bare 'import app' needs a manual look")
    return text


def rewrite_imports() -> tuple[list[str], list[str]]:
    problems: list[str] = []
    changed: list[str] = []
    file_names = {name: defined_names(PKG / f"{name}.py") for name in FILE_MODULES}
    file_names["factory"] = defined_names(PKG / "factory.py")
    children = package_children()
    for top in SCAN_DIRS:
        for path in sorted((ROOT / top).rglob("*.py")):
            if path.resolve() == SELF or "__pycache__" in path.parts:
                continue
            rel = str(path.relative_to(ROOT))
            old = path.read_text()
            new = rewrite_source(old, rel, file_names, children, problems)
            if new != old:
                path.write_text(new)
                changed.append(rel)
    return changed, problems


def run_ruff(files: list[str]) -> str:
    if not files:
        return "ruff: nothing to fix"
    venv_ruff = Path(sys.executable).with_name("ruff")
    ruff = str(venv_ruff) if venv_ruff.is_file() else shutil.which("ruff") or ""
    if not Path(ruff).is_file():
        return "ruff not found: run `ruff check --select I --fix` and `ruff format`"
    subprocess.run(
        [ruff, "check", "--select", "I", "--fix", "--quiet", *files],
        cwd=ROOT,
        check=False,
    )
    subprocess.run([ruff, "format", "--quiet", *files], cwd=ROOT, check=False)
    return f"ruff: sorted imports and formatted {len(files)} file(s)"


def main() -> int:
    moved, move_problems = move_paths()
    changed, import_problems = rewrite_imports()
    print(f"moved {len(moved)} path(s)")
    for line in moved:
        print(f"  {line}")
    print(f"rewrote imports or module strings in {len(changed)} file(s)")
    for line in changed:
        print(f"  {line}")
    print(run_ruff(changed))
    problems = move_problems + import_problems
    for line in problems:
        print(f"CHECK: {line}")
    if not moved and not changed:
        print("nothing to do")
    return 1 if problems else 0


if __name__ == "__main__":
    sys.exit(main())