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
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
Requirements
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
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
Requirements
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
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
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
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
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
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
Requirements
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
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
Requirements
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
Called by main
Calls _detect_backend, _download, _expected_sha256, _extract_to, _install_root, _sha256_of, _verify_existing
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
Requirements
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