Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

10.4 KB
Raw Blame
#!/usr/bin/env python3
"""Build the ``dodaios`` sdist and wheel, and the ``personal-jarvis`` pointer to it.

    python scripts/ci/build_distributions.py --outdir dist

The project is ``dodaios`` on PyPI. Its earlier name, ``personal-jarvis``, is
published at every release as a thin compatibility package and no longer as a
second copy of the code: it contains one small launcher module and depends on
``dodaios==<the same version>``, so ``pip install -U personal-jarvis`` and a
stored ``uvx --from personal-jarvis jarvis ...`` keep getting the current
release. Its extras map one to one (``personal-jarvis[full]`` installs
``dodaios[full]``), and it declares every console command ``dodaios`` declares,
because ``uv tool install personal-jarvis`` only links the commands of the
package it was asked for. Each command starts the same entry point the
``dodaios`` command of that name starts.

The launcher exists for one case. Releases up to 4.1.2 published
``personal-jarvis`` as a full copy that owns the same files as ``dodaios``.
Upgrading such an install with plain pip installs ``dodaios`` first and then
uninstalls the old copy, which deletes the files ``dodaios`` just wrote (uv
uninstalls first and is not affected). The launcher then prints the one
command that repairs it instead of a bare ``ModuleNotFoundError``.

Output: ``<outdir>/<name>/`` per distribution, one wheel and one sdist each,
so the publish jobs can upload them separately.
"""

from __future__ import annotations

import argparse
import json
import re
import shutil
import subprocess
import sys
import tempfile
import tomllib
from collections.abc import Callable, Mapping, Sequence
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
CURRENT = "dodaios"
LEGACY = "personal-jarvis"
#: The only module the compatibility package ships.
COMPAT_MODULE = "personal_jarvis_compat"

Runner = Callable[[Sequence[str], Path], None]


def _normalized(name: str) -> str:
    """The file-name spelling of a distribution name (PEP 427 / PEP 625)."""
    return re.sub(r"[-_.]+", "_", name).lower()


def _toml(value: object) -> str:
    """A TOML value for a string or a list of strings (JSON spells both validly)."""
    return json.dumps(value)


def compat_pyproject(project: Mapping, *, current: str = CURRENT, legacy: str = LEGACY) -> str:
    """``pyproject.toml`` of the compatibility package for ``project`` (dodaios's table)."""
    if project["name"] != current:
        raise ValueError(f"pyproject names {project['name']!r}, expected {current!r}")
    version = project["version"]
    scripts = project.get("scripts", {})
    if not scripts:
        raise ValueError("the project declares no console commands to keep")
    classifiers = [
        item for item in project.get("classifiers", []) if not item.startswith("Development Status")
    ]
    description = f"Personal Jarvis is now Dodaios: this package installs {current}"
    lines = [
        "[build-system]",
        'requires = ["setuptools>=84.1.0"]',
        'build-backend = "setuptools.build_meta"',
        "",
        "[project]",
        f"name = {_toml(legacy)}",
        f"version = {_toml(version)}",
        f"description = {_toml(description)}",
        'readme = "README.md"',
        f"requires-python = {_toml(project['requires-python'])}",
        f"license = {_toml(project['license'])}",
        'license-files = ["LICENSE"]',
    ]
    authors = ", ".join(
        "{ " + ", ".join(f"{key} = {_toml(value)}" for key, value in author.items()) + " }"
        for author in project.get("authors", [])
    )
    if authors:
        lines.append(f"authors = [{authors}]")
    lines += [
        f"classifiers = {_toml(classifiers)}",
        f"dependencies = {_toml([f'{current}=={version}'])}",
        "",
        "[project.optional-dependencies]",
    ]
    lines += [
        f"{_toml(extra)} = {_toml([f'{current}[{extra}]=={version}'])}"
        for extra in project.get("optional-dependencies", {})
    ]
    lines += ["", "[project.urls]"]
    lines += [f"{_toml(key)} = {_toml(url)}" for key, url in project.get("urls", {}).items()]
    lines += ["", "[project.scripts]"]
    lines += [f"{_toml(name)} = {_toml(f'{COMPAT_MODULE}:main')}" for name in scripts]
    lines += ["", "[tool.setuptools]", f"py-modules = {_toml([COMPAT_MODULE])}", ""]
    return "\n".join(lines)


def compat_module(project: Mapping, *, current: str = CURRENT) -> str:
    """Source of the launcher every command of the compatibility package runs."""
    version = project["version"]
    targets = dict(project["scripts"])
    default = targets.get(current, next(iter(targets.values())))
    return f'''"""Personal Jarvis is now Dodaios: start the {current} command this one stands for.

Generated by scripts/ci/build_distributions.py for the compatibility package.
"""

from __future__ import annotations

import sys
from importlib import import_module

VERSION = {version!r}
TARGETS = {targets!r}
DEFAULT_TARGET = {default!r}
REPAIR = (
    "Dodaios (formerly Personal Jarvis) is installed incompletely: upgrading the "
    "earlier personal-jarvis package with pip removed files the {current} package needs.\\n"
    "Repair it with:\\n"
    '  "{{python}}" -m pip install --force-reinstall --no-deps {current}=={{version}}\\n'
)


def command_name(argv0: str) -> str:
    """The command this process was started as, without a launcher suffix."""
    # Either separator: a Windows launcher path also reads right elsewhere.
    name = argv0.replace("\\\\", "/").rsplit("/", 1)[-1].lower()
    for suffix in ("-script.pyw", "-script.py", ".exe", ".py"):
        if name.endswith(suffix):
            return name[: -len(suffix)]
    return name


def main() -> object:
    target = TARGETS.get(command_name(sys.argv[0] if sys.argv else ""), DEFAULT_TARGET)
    module_name, _, attribute = target.partition(":")
    try:
        module = import_module(module_name)
    except ModuleNotFoundError as exc:
        missing = exc.name or ""
        if missing != {current!r} and not missing.startswith({current + "."!r}):
            raise
        sys.stderr.write(REPAIR.format(python=sys.executable, version=VERSION))
        return 1
    return getattr(module, attribute)()
'''


def compat_readme(project: Mapping, *, current: str = CURRENT, legacy: str = LEGACY) -> str:
    version = project["version"]
    homepage = project.get("urls", {}).get("Homepage", "")
    return f"""# {legacy} is now {current}

Personal Jarvis was renamed to **Dodaios**. This package no longer contains the
app: it installs [`{current}`](https://pypi.org/project/{current}/) at the same
version ({version}) and keeps the earlier commands working
(`{"`, `".join(project["scripts"])}`).

New installs: `pip install {current}` or `uv tool install {current}`.

To move an existing install to the new name:

```
pip uninstall -y {legacy}
pip install --force-reinstall {current}
```

If `pip install -U {legacy}` left an install that no longer starts (a command
prints a repair line, or Python reports `No module named '{current}'`), the
upgrade removed files of the old full package that `{current}` needs. Repair it:

```
pip install --force-reinstall --no-deps {current}=={version}
```

Project: {homepage}
"""


def write_compat_project(directory: Path, project: Mapping, license_file: Path) -> None:
    """Write the compatibility package's whole source tree into ``directory``."""
    directory.mkdir(parents=True, exist_ok=True)
    (directory / "pyproject.toml").write_text(compat_pyproject(project), encoding="utf-8")
    (directory / f"{COMPAT_MODULE}.py").write_text(compat_module(project), encoding="utf-8")
    (directory / "README.md").write_text(compat_readme(project), encoding="utf-8")
    shutil.copyfile(license_file, directory / "LICENSE")


def _run(command: Sequence[str], cwd: Path) -> None:
    subprocess.run(list(command), cwd=cwd, check=True)


def _clean_build_state(root: Path) -> None:
    """Leftovers of one build must not leak into the next one's sdist or wheel.

    Only what setuptools writes: a PyInstaller freeze keeps its own folder
    under ``build/``.
    """
    stale = [root / "build" / "lib", *(root / "build").glob("bdist.*")]
    stale += [root / f"{_normalized(name)}.egg-info" for name in (CURRENT, LEGACY)]
    for path in stale:
        shutil.rmtree(path, ignore_errors=True)


def expected_artifacts(name: str, version: str) -> set[str]:
    stem = f"{_normalized(name)}-{version}"
    return {f"{stem}.tar.gz", f"{stem}-py3-none-any.whl"}


def build_all(
    outdir: Path,
    *,
    root: Path = ROOT,
    build_command: Sequence[str] = (sys.executable, "-m", "build", "--no-isolation"),
    runner: Runner = _run,
) -> dict[str, Path]:
    """Build both distributions; return ``{name: its output directory}``."""
    project = tomllib.loads((root / "pyproject.toml").read_text(encoding="utf-8"))["project"]
    version = project["version"]
    compat_pyproject(project)  # refuses a tree that is not dodaios before anything runs
    outputs = {CURRENT: outdir / CURRENT, LEGACY: outdir / LEGACY}
    for directory in outputs.values():
        if directory.exists() and any(directory.iterdir()):
            raise ValueError(f"output directory is not empty: {directory}")
    try:
        _clean_build_state(root)
        runner([*build_command, "--outdir", str(outputs[CURRENT])], root)
    finally:
        _clean_build_state(root)
    with tempfile.TemporaryDirectory(prefix="compat-") as scratch:
        compat_root = Path(scratch) / LEGACY
        write_compat_project(compat_root, project, root / "LICENSE")
        runner([*build_command, "--outdir", str(outputs[LEGACY])], compat_root)
    for name, directory in outputs.items():
        found = {path.name for path in directory.iterdir()} if directory.is_dir() else set()
        expected = expected_artifacts(name, version)
        if found != expected:
            raise ValueError(
                f"{name}: built {sorted(found)}, expected {sorted(expected)}"
            )
    return outputs


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("--outdir", type=Path, required=True)
    args = parser.parse_args(argv)
    try:
        outputs = build_all(args.outdir.resolve())
    except (OSError, ValueError, subprocess.CalledProcessError) as exc:
        print(f"::error::building the distributions failed: {exc}")
        return 1
    for name, directory in outputs.items():
        print(f"[build] {name}: {', '.join(sorted(p.name for p in directory.iterdir()))}")
    return 0


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