Skip to content

Clone your own voice on your own computer: speak any text in 60+ languages and dub videos into another language in your voice. Fully local. Early version.

14.0 KB
Raw Blame
"""Move the data directory: ``setup.py --move-to <new dir>``. Standard library only.

The app parses the output: ``[move] 37% 11.4/31.0 GB`` while copying, then ``[move] done`` (exit code 0) or
``[move] failed: <reason>`` (non-zero); anything else is ``[move] <text>``.
"""

from __future__ import annotations

import contextlib
import json
import os
import re
import shutil
import stat
import subprocess
from collections.abc import Callable, Iterator
from pathlib import Path
from urllib.parse import quote

MARKER = ".mimvox-move.json"
GB = 1e9
TEXT_LIMIT = 2_000_000


class MoveError(RuntimeError):
    """The message is shown to the user."""


def out(line: str) -> None:
    print(f"[move] {line}", flush=True)


def running_processes(old: Path, windows: bool) -> list[str]:
    needle = os.path.normcase(str(old)).rstrip("\\/")
    found: list[str] = []
    me = os.getpid()
    if windows:
        script = (
            "Get-CimInstance Win32_Process | ForEach-Object { "
            '"$($_.ProcessId)`t$($_.ExecutablePath)`t$($_.CommandLine)" }'
        )
        try:
            text = subprocess.run(
                ["powershell", "-NoProfile", "-NonInteractive", "-Command", script],
                capture_output=True,
                text=True,
                timeout=60,
                check=False,
                creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
            ).stdout
        except (OSError, subprocess.SubprocessError):
            raise MoveError("cannot list running processes to check that the engine is stopped") from None
        rows = [ln.split("\t", 2) for ln in text.splitlines() if ln.count("\t") >= 2]
        for pid, exe, cmd in rows:
            if not pid.strip().isdigit() or int(pid) == me:
                continue
            if (exe and os.path.normcase(exe).startswith(needle)) or "mimvox_engine" in cmd:
                found.append(f"pid {pid.strip()}: {(exe or cmd)[:100]}")
    else:
        for entry in Path("/proc").glob("[0-9]*") if Path("/proc").is_dir() else []:
            if int(entry.name) == me:
                continue
            try:
                exe = str((entry / "exe").readlink())
                cmd = (entry / "cmdline").read_bytes().replace(b"\0", b" ").decode("utf-8", "replace")
            except OSError:
                continue
            if exe.startswith(needle) or "mimvox_engine" in cmd:
                found.append(f"pid {entry.name}: {(exe or cmd)[:100]}")
        if not Path("/proc").is_dir():  # macOS
            try:
                text = subprocess.run(
                    ["ps", "-axo", "pid=,command="], capture_output=True, text=True, timeout=30, check=False
                ).stdout
            except (OSError, subprocess.SubprocessError):
                raise MoveError("cannot list running processes to check that the engine is stopped") from None
            for ln in text.splitlines():
                pid, _, cmd = ln.strip().partition(" ")
                if pid.isdigit() and int(pid) != me and (needle in cmd or "mimvox_engine" in cmd):
                    found.append(f"pid {pid}: {cmd[:100]}")
    return found


def existing_parent(path: Path) -> Path:
    while not path.exists() and path != path.parent:
        path = path.parent
    return path


# Left behind and deleted with the old folder: both are made again when needed, and copying uv's cache to another
# drive would undo its hard links.
NOT_MOVED = ("cache", "tmp")


def _prune_top(root: Path, dirpath: Path, dirs: list[str], files: list[str]) -> None:
    if dirpath == root:
        dirs[:] = [d for d in dirs if d not in NOT_MOVED]
        files[:] = [f for f in files if f not in NOT_MOVED or not (root / f).is_dir()]


def tree_size(root: Path) -> int:
    total = 0
    for dirpath, dirs, files in root.walk():
        _prune_top(root, dirpath, dirs, files)
        for name in files:
            with contextlib.suppress(OSError):
                total += (dirpath / name).lstat().st_size
    return total


def _inside(child: Path, parent: Path) -> bool:
    c, p = os.path.normcase(str(child)), os.path.normcase(str(parent))
    return c == p or c.startswith(p.rstrip("\\/") + os.sep)


def _walk_files(root: Path) -> Iterator[tuple[Path, bool | None]]:
    """``(rel, is_symlink)`` of every file and symlink; directories as ``(rel, None)``."""
    for dirpath, dirs, files in root.walk():
        _prune_top(root, dirpath, dirs, files)
        rel_dir = dirpath.relative_to(root)
        yield rel_dir, None
        for name in files:
            yield rel_dir / name, (dirpath / name).is_symlink()


def copy_tree(src: Path, dst: Path, old: Path, total: int, progress: Callable[[int], None]) -> None:
    """Files are written as ``name.mimvox-part`` and renamed, so a half copy never looks complete."""
    dst.mkdir(parents=True, exist_ok=True)
    done = 0
    for rel, link in _walk_files(src):
        s, d = src / rel, dst / rel
        if link is None:
            d.mkdir(parents=True, exist_ok=True)
            continue
        if link:
            target = str(s.readlink())
            if os.path.normcase(target).startswith(os.path.normcase(str(src))):
                target = str(dst) + target[len(str(src)) :]
            if d.exists(follow_symlinks=False):
                d.unlink()
            d.symlink_to(target, target_is_directory=s.is_dir())
            continue
        size = s.stat().st_size
        if d.is_file() and d.stat().st_size == size:
            done += size
            progress(done)
            continue
        tmp = d.with_name(d.name + ".mimvox-part")
        with s.open("rb") as fin, tmp.open("wb") as fout:
            while True:
                block = fin.read(8 * 1024 * 1024)
                if not block:
                    break
                fout.write(block)
                done += len(block)
                progress(done)
        shutil.copystat(s, tmp)
        tmp.replace(d)
    progress(total)


def verify_tree(src: Path, dst: Path) -> None:
    want = {
        rel.as_posix(): (None if link else (src / rel).stat().st_size)
        for rel, link in _walk_files(src)
        if link is not None
    }
    have = {
        rel.as_posix(): (None if link else (dst / rel).stat().st_size)
        for rel, link in _walk_files(dst)
        if link is not None
    }
    have.pop(MARKER, None)
    missing = [k for k in want if k not in have]
    wrong = [k for k, v in want.items() if k in have and have[k] != v]
    stray = [k for k in have if k not in want and not k.endswith(".mimvox-part")]
    parts = [k for k in have if k.endswith(".mimvox-part")]
    if missing or wrong or parts:
        raise MoveError(
            f"the copy is not complete ({len(missing)} missing, {len(wrong)} with a different size, "
            f"{len(parts)} unfinished); e.g. {(missing + wrong + parts)[0]}. Run the move again to resume."
        )
    del stray  # files only the target has (for example the marker) are fine


def remove_tree(path: Path) -> None:
    def onexc(func: Callable[[str], object], p: str, _exc: BaseException) -> None:  # read-only files (git objects)
        try:
            Path(p).chmod(stat.S_IWRITE | stat.S_IREAD)
            func(p)
        except OSError:
            pass

    shutil.rmtree(path, onexc=onexc)


def _forms(old: Path, new: Path) -> list[tuple[bytes, bytes]]:
    o, n = str(old), str(new)
    pairs = [
        (o.replace("\\", "\\\\"), n.replace("\\", "\\\\")),
        (o, n),
        (old.as_posix(), new.as_posix()),
        (quote(old.as_posix(), safe=":/"), quote(new.as_posix(), safe=":/")),
    ]
    seen: set[str] = set()
    result = []
    for a, b in pairs:
        if a not in seen:
            seen.add(a)
            result.append((a.encode("utf-8"), b.encode("utf-8")))
    return result


class PathFixer:
    def __init__(self, old: Path, new: Path, windows: bool) -> None:
        self.pairs = _forms(old, new)
        flags = re.IGNORECASE if windows else 0
        self.rx = [(re.compile(re.escape(a), flags), b) for a, b in self.pairs]
        self.fixed: list[str] = []

    def replace(self, data: bytes) -> bytes:
        for rx, b in self.rx:
            data = rx.sub(lambda _m, b=b: b, data)
        return data

    def has_old(self, data: bytes) -> bool:
        return any(rx.search(data) for rx, _ in self.rx)

    def text_file(self, path: Path) -> None:
        try:
            if path.stat().st_size > TEXT_LIMIT:
                return
            data = path.read_bytes()
        except OSError:
            return
        if b"\0" in data or not self.has_old(data):
            return
        try:
            data.decode("utf-8")
        except UnicodeDecodeError:
            return
        path.write_bytes(self.replace(data))
        self.fixed.append(str(path))


def _link_target(path: Path) -> str | None:
    """A symlink's target, or a Windows junction's (uv links each Python's minor version to its patch release)."""
    try:
        junction = path.lstat().st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT
    except (OSError, AttributeError):  # st_reparse_tag: Windows only
        junction = False
    if not (path.is_symlink() or junction):
        return None
    try:
        return str(path.readlink()).removeprefix("\\\\?\\")
    except OSError:
        return None


def fix_link(path: Path, old: Path, new: Path, fixer: PathFixer) -> None:
    """A link into the old folder dangles once that was renamed (a copy already re-pointed it)."""
    target = _link_target(path)
    if target is None or not _inside(Path(target), old):
        return
    moved = str(new) + target[len(str(old)) :]
    if path.is_symlink():
        path.unlink()
        path.symlink_to(moved, target_is_directory=Path(moved).is_dir())
    else:
        import _winapi

        path.rmdir()  # the junction, not its target
        _winapi.CreateJunction(moved, str(path))
    fixer.fixed.append(str(path))


def fix_venv_paths(root: Path, old: Path, windows: bool) -> PathFixer:
    fixer = PathFixer(old, root, windows)
    # uv's Pythons (setup.py UV_PYTHON_DIR): absolute links to the patch release each venv's home goes through.
    for entry in sorted(root.glob("runtime/python/*")):
        fix_link(entry, old, root, fixer)
    for venv in sorted(root.glob("runtime/*/.venv")) + sorted(root.glob("runtime/*/venv")):
        fixer.text_file(venv / "pyvenv.cfg")
        for sub in ("Scripts", "bin"):
            for f in sorted((venv / sub).glob("*")) if (venv / sub).is_dir() else []:
                fix_link(f, old, root, fixer)
                if f.is_file() and not f.is_symlink():
                    fixer.text_file(f)
        sites = list(venv.glob("Lib/site-packages")) + list(venv.glob("lib/python*/site-packages"))
        for site in sites:
            for pattern in ("*.pth", "*.egg-link", "__editable__*.py", "_editable_impl*.py"):
                for f in sorted(site.glob(pattern)):
                    fixer.text_file(f)
            for f in sorted(site.glob("*.dist-info/direct_url.json")):
                fixer.text_file(f)
    return fixer


def prepare(old: Path | None, new: Path, windows: bool, force: bool) -> tuple[Path, bool, bool]:
    """Returns ``(old, resume, same_volume)``."""
    marker = new / MARKER
    resume = marker.is_file()
    if resume:
        try:
            old = Path(json.loads(marker.read_text(encoding="utf-8"))["from"])
        except (OSError, ValueError, KeyError):
            raise MoveError(f"{marker} is unreadable; remove the unfinished folder {new} and start again") from None
        out(f"Resuming an interrupted move from {old}")
    if old is None:
        raise MoveError("no current data directory found; pass --data-dir <current dir>")
    if _inside(new, old) or _inside(old, new):
        raise MoveError("the new folder must not be inside the old one (or the other way round)")
    if not resume:
        if not (old / "runtime").is_dir() and not (old / "runtime.toml").is_file():
            raise MoveError(f"nothing to move: no runtime in {old}")
        if new.exists() and any(new.iterdir()):
            raise MoveError(f"{new} is not empty")
    busy = running_processes(old, windows)
    if busy:
        raise MoveError("the engine or the runtime is still running; close the app first (" + "; ".join(busy[:3]) + ")")
    same = False
    if old.exists():
        try:
            same = old.stat().st_dev == existing_parent(new).stat().st_dev
        except OSError:
            same = False
    if os.environ.get("MIMVOX_MOVE_FORCE_COPY"):  # testing: take the copy path on one volume
        same = False
    return old, resume, same


def move_files(old: Path, new: Path, resume: bool, same_volume: bool, force: bool) -> None:
    if not old.exists():
        if not (new / MARKER).is_file():
            raise MoveError(f"{old} does not exist")
        return  # renamed earlier; only the fix-up is left
    marker_data = json.dumps({"from": str(old), "to": str(new)})
    if same_volume and not resume:
        (old / MARKER).write_text(marker_data, encoding="utf-8")
        new.parent.mkdir(parents=True, exist_ok=True)
        if new.exists():
            new.rmdir()
        try:
            old.rename(new)
            out(f"Renamed {old} to {new} (same volume, nothing copied)")
            out("100% done")
            return
        except OSError as exc:
            (old / MARKER).unlink(missing_ok=True)
            out(f"Rename not possible ({exc}); copying instead")
    total = tree_size(old)
    free = shutil.disk_usage(existing_parent(new)).free
    already = tree_size(new) if new.exists() else 0
    need = max(0, total - already) + int(1 * GB)
    if free < need and not force:
        raise MoveError(f"not enough free space on the target: {free / GB:.1f} GB free, {need / GB:.1f} GB needed")
    new.mkdir(parents=True, exist_ok=True)
    (new / MARKER).write_text(marker_data, encoding="utf-8")
    last = [-1]

    def progress(done: int) -> None:
        pct = int(done * 100 / total) if total else 100
        if pct != last[0]:
            last[0] = pct
            out(f"{pct}% {done / GB:.1f}/{total / GB:.1f} GB")

    out(f"Copying {total / GB:.1f} GB from {old} to {new}")
    copy_tree(old, new, old, total, progress)
    out("Verifying the copy")
    verify_tree(old, new)
    out("Copy verified")