install_engine.py

python/src/entropic/install_engine.py

``entropic install-engine`` — fetch the matching native tarball.

Owners: @tvanfossen

_detect_backend

str entropic.install_engine._detect_backend()

Return "cuda" if nvidia-smi succeeds, else "cpu".

python/src/entropic/install_engine.py:42

Show on the map →

Called by install

Calls none

Source · 14 lines
def _detect_backend() -> str:
    """Return ``"cuda"`` if nvidia-smi succeeds, else ``"cpu"``."""
    try:
        result = subprocess.run(
            ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
            capture_output=True,
            timeout=5,
            check=False,
        )
    except (FileNotFoundError, subprocess.TimeoutExpired):
        return "cpu"
    return "cuda" if result.returncode == 0 and result.stdout.strip() else "cpu"

Activity · 5 steps
lines 44–44trylines 45–50result = subprocess.run( ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], capture_output=True, timeout=5, check=False, )lines 51–51except (FileNotFoundError, subprocess.TimeoutExpired)lines 52–52return "cpu"lines 53–53return "cuda" if result.returncode == 0 and result.stdout.strip() else "cpu"

Requirements

REQ-PROD-003

Flows

_download

None entropic.install_engine._download(str url, Path dest)

Stream url to dest; raise URLError or HTTPError on failure.

python/src/entropic/install_engine.py:81

Source · 7 lines
def _download(url: str, dest: Path) -> None:
    """Stream ``url`` to ``dest``; raise URLError or HTTPError on failure."""
    print(f"  downloading {url}", file=sys.stderr)
    with urllib.request.urlopen(url) as response, dest.open("wb") as out:
        shutil.copyfileobj(response, out)

Activity · 3 steps
lines 83–83print(f" downloading {url}", file=sys.stderr)lines 84–84with urllib.request.urlopen(url) as response, dest.open("wb") as outlines 85–85shutil.copyfileobj(response, out)

Flows

_expected_sha256

str entropic.install_engine._expected_sha256(str version, str backend, Path scratch)

Fetch the .sha256 sidecar and return the bare hash.

python/src/entropic/install_engine.py:92

Source · 9 lines
def _expected_sha256(version: str, backend: str, scratch: Path) -> str:
    """Fetch the .sha256 sidecar and return the bare hash."""
    sha_url = RELEASE_URL_TEMPLATE.format(version=version, backend=backend) + ".sha256"
    sha_file = scratch / "tarball.sha256"
    _download(sha_url, sha_file)
    # `sha256sum` output format: "<hash>  <filename>". Take the first token.
    return sha_file.read_text().split()[0].strip()

Activity · 4 steps
lines 94–94sha_url = RELEASE_URL_TEMPLATE.format(version=version, backend=backend) + ".sha256"lines 95–95sha_file = scratch / "tarball.sha256"lines 96–96_download(sha_url, sha_file)lines 98–98return sha_file.read_text().split()[0].strip()

Requirements

REQ-PROD-003

Flows

_extract_to

None entropic.install_engine._extract_to(Path tarball, Path root)

Wipe root/entropic then extract tarball into root.

python/src/entropic/install_engine.py:116

Show on the map →

Called by install

Calls none

Source · 10 lines
def _extract_to(tarball: Path, root: Path) -> None:
    """Wipe ``root/entropic`` then extract tarball into ``root``."""
    target = root / "entropic"
    if target.exists():
        shutil.rmtree(target)
    root.mkdir(parents=True, exist_ok=True)
    with tarfile.open(tarball, "r:gz") as tf:
        tf.extractall(root, filter="data")

Activity · 6 steps
lines 118–118target = root / "entropic"lines 119–119◇ if target.exists()lines 120–120shutil.rmtree(target)lines 121–121root.mkdir(parents=True, exist_ok=True)lines 122–122with tarfile.open(tarball, "r:gz") as tflines 123–123tf.extractall(root, filter="data")

Flows

_install_root

Path entropic.install_engine._install_root()

Return the resolved install root ($ENTROPIC_HOME or ~/.entropic).

python/src/entropic/install_engine.py:59

Show on the map →

Called by install

Calls none

Source · 6 lines
def _install_root() -> Path:
    """Return the resolved install root (``$ENTROPIC_HOME`` or ``~/.entropic``)."""
    home = os.environ.get("ENTROPIC_HOME")
    return Path(home) if home else Path.home() / ".entropic"

Activity · 2 steps
lines 61–61home = os.environ.get("ENTROPIC_HOME")lines 62–62return Path(home) if home else Path.home() / ".entropic"

Flows

_sha256_of

str entropic.install_engine._sha256_of(Path path)

Return the lowercase hex SHA256 of path.

python/src/entropic/install_engine.py:69

Show on the map →

Called by install

Calls none

Source · 9 lines
def _sha256_of(path: Path) -> str:
    """Return the lowercase hex SHA256 of ``path``."""
    h = hashlib.sha256()
    with path.open("rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 16), b""):
            h.update(chunk)
    return h.hexdigest()

Activity · 5 steps
lines 71–71h = hashlib.sha256()lines 72–72with path.open("rb") as fhlines 73–73◇ for chunk in iter(lambda: fh.read(1 << 16), b"")lines 74–74h.update(chunk)lines 75–75return h.hexdigest()

Requirements

REQ-PROD-003

Flows

_verify_existing

bool entropic.install_engine._verify_existing(Path root, str expected_hash)

If a stamp file matches expected_hash, install is current.

python/src/entropic/install_engine.py:105

Show on the map →

Called by install

Calls none

Source · 8 lines
def _verify_existing(root: Path, expected_hash: str) -> bool:
    """If a stamp file matches expected_hash, install is current."""
    stamp = root / ".entropic-engine-sha256"
    if not stamp.is_file():
        return False
    return stamp.read_text().strip() == expected_hash

Activity · 4 steps
lines 107–107stamp = root / ".entropic-engine-sha256"lines 108–108◇ if not stamp.is_file()lines 109–109return Falselines 110–110return stamp.read_text().strip() == expected_hash

Requirements

REQ-PROD-003

Flows

install

int entropic.install_engine.install(str|None version=None, str|None backend=None)

Install the engine tarball; return process-style exit code.

python/src/entropic/install_engine.py:132

Source · 35 lines
def install(version: str | None = None, backend: str | None = None) -> int:
    """Install the engine tarball; return process-style exit code."""
    version = version or __version__
    backend = backend or _detect_backend()
    root = _install_root()
    print(
        f"entropic install-engine: version={version} backend={backend} " f"root={root}",
        file=sys.stderr,
    )

    with tempfile.TemporaryDirectory() as tmp_str:
        tmp = Path(tmp_str)
        expected = _expected_sha256(version, backend, tmp)

        if _verify_existing(root, expected):
            print("  already installed (sha256 matches); skipping", file=sys.stderr)
            return 0

        url = RELEASE_URL_TEMPLATE.format(version=version, backend=backend)
        tarball = tmp / "entropic.tar.gz"
        _download(url, tarball)
        actual = _sha256_of(tarball)
        if actual != expected:
            print(
                f"error: sha256 mismatch (expected {expected}, got {actual})",
                file=sys.stderr,
            )
            return 2

        _extract_to(tarball, root)
        (root / ".entropic-engine-sha256").write_text(expected + "\n")
        print(f"  installed to {root / 'entropic'}", file=sys.stderr)
    return 0

Activity · 21 steps
lines 134–134version = version or __version__lines 135–135backend = backend or _detect_backend()lines 136–136root = _install_root()lines 137–140print( f"entropic install-engine: version={version} backend={backend} " f"root={root}", file=sys.stderr, )lines 142–142with tempfile.TemporaryDirectory() as tmp_strlines 143–143tmp = Path(tmp_str)lines 144–144expected = _expected_sha256(version, backend, tmp)lines 146–146◇ if _verify_existing(root, expected)lines 147–147print(" already installed (sha256 matches); skipping", file=sys.stderr)lines 148–148return 0lines 150–150url = RELEASE_URL_TEMPLATE.format(version=version, backend=backend)lines 151–151tarball = tmp / "entropic.tar.gz"lines 152–152_download(url, tarball)lines 153–153actual = _sha256_of(tarball)lines 154–154◇ if actual != expectedlines 155–158print( f"error: sha256 mismatch (expected {expected}, got {actual})", file=sys.stderr, )lines 159–159return 2lines 161–161_extract_to(tarball, root)lines 162–162(root / ".entropic-engine-sha256").write_text(expected + "\n")lines 163–163print(f" installed to {root / 'entropic'}", file=sys.stderr)lines 164–164return 0

Requirements

REQ-PROD-003

Flows

main

int entropic.install_engine.main(list[str]|None argv=None)

entropic install-engine entry point.

python/src/entropic/install_engine.py:171

Source · 15 lines
def main(argv: list[str] | None = None) -> int:
    """``entropic install-engine`` entry point. Optional --version / --backend."""
    args = list(sys.argv[1:] if argv is None else argv)
    version: str | None = None
    backend: str | None = None
    while args:
        flag = args.pop(0)
        if flag == "--version" and args:
            version = args.pop(0)
        elif flag == "--backend" and args:
            backend = args.pop(0)
        else:
            print(f"error: unknown argument {flag!r}", file=sys.stderr)
            return 64
    return install(version=version, backend=backend)

1 more lines, not shown

Activity · 13 steps
lines 173–173args = list(sys.argv[1:] if argv is None else argv)lines 174–174version: str | None = Nonelines 175–175backend: str | None = Nonelines 176–176◇ while argslines 177–177flag = args.pop(0)lines 178–178◇ if flag == "--version" and argslines 179–179version = args.pop(0)lines 180–180◇ elif flag == "--backend" and argslines 181–181backend = args.pop(0)lines 182–182elselines 183–183print(f"error: unknown argument {flag!r}", file=sys.stderr)lines 184–184return 64lines 185–185return install(version=version, backend=backend)

Requirements

REQ-PROD-001

Flows