Skip to content

Install

erpc.install

eRPC binary installation helpers.

Inspired by py-geth <https://github.com/ethereum/py-geth>_'s install module. Handles cross-platform binary download from GitHub releases with optional SHA256 checksum verification.

GITHUB_RELEASES_URL module-attribute

GITHUB_RELEASES_URL = 'https://github.com/erpc/erpc/releases/download'

Base URL for eRPC GitHub release artifacts.

PLATFORM_MAP module-attribute

PLATFORM_MAP: dict[tuple[str, str], str] = {('Linux', 'x86_64'): 'erpc_linux_amd64', ('Linux', 'aarch64'): 'erpc_linux_arm64', ('Darwin', 'x86_64'): 'erpc_darwin_amd64', ('Darwin', 'arm64'): 'erpc_darwin_arm64'}

Mapping of (system, machine) to release artifact names.

get_platform_binary_name

get_platform_binary_name() -> str

Get the eRPC binary artifact name for the current platform.

Returns:

Type Description
str

Artifact filename for the current OS and architecture.

Raises:

Type Description
ERPCError

If the current platform is not supported.

Examples:

>>> get_platform_binary_name()  # On Linux x86_64
'erpc_linux_amd64'
Source code in erpc/install.py
def get_platform_binary_name() -> str:
    """Get the eRPC binary artifact name for the current platform.

    Returns:
        Artifact filename for the current OS and architecture.

    Raises:
        ERPCError: If the current platform is not supported.

    Examples:
        >>> get_platform_binary_name()  # On Linux x86_64
        'erpc_linux_amd64'

    """
    key = (platform.system(), platform.machine())
    if key not in PLATFORM_MAP:
        raise ERPCError(
            f"Unsupported platform: {platform.system()} {platform.machine()}. "
            f"Supported: {', '.join(f'{s}/{m}' for s, m in PLATFORM_MAP)}"
        )
    return PLATFORM_MAP[key]

verify_checksum

verify_checksum(path: Path, expected_sha256: str) -> None

Verify the SHA256 checksum of a file.

Parameters:

Name Type Description Default
path Path

Path to the file to verify.

required
expected_sha256 str

Expected lowercase hex-encoded SHA256 digest.

required

Raises:

Type Description
ERPCError

If the checksum does not match.

Examples:

>>> verify_checksum(Path("/usr/local/bin/erpc"), "abc123...")
Source code in erpc/install.py
def verify_checksum(path: Path, expected_sha256: str) -> None:
    """Verify the SHA256 checksum of a file.

    Args:
        path: Path to the file to verify.
        expected_sha256: Expected lowercase hex-encoded SHA256 digest.

    Raises:
        ERPCError: If the checksum does not match.

    Examples:
        >>> verify_checksum(Path("/usr/local/bin/erpc"), "abc123...")

    """
    sha256 = hashlib.sha256()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(8192), b""):
            sha256.update(chunk)
    actual = sha256.hexdigest()
    if actual != expected_sha256.lower():
        raise ERPCError(f"Checksum mismatch for {path}: expected {expected_sha256}, got {actual}")

install_erpc

install_erpc(version: str | None = None, install_dir: str = '/usr/local/bin', binary_name: str = 'erpc', checksum: str | None = None) -> Path

Download and install eRPC binary from GitHub releases.

Parameters:

Name Type Description Default
version str | None

Release version tag (e.g., "0.0.62"). Defaults to :data:erpc.ERPC_VERSION — the pinned compatible version.

None
install_dir str

Directory to install the binary. Created if it doesn't exist.

'/usr/local/bin'
binary_name str

Name for the installed binary.

'erpc'
checksum str | None

Optional SHA256 hex digest for verification.

None

Returns:

Type Description
Path

Path to the installed binary.

Raises:

Type Description
ERPCError

If the platform is unsupported or checksum verification fails.

Examples:

>>> install_erpc()  # installs pinned version
PosixPath('/usr/local/bin/erpc')
>>> install_erpc("0.0.62", checksum="abc123...")
PosixPath('/usr/local/bin/erpc')
Source code in erpc/install.py
def install_erpc(
    version: str | None = None,
    install_dir: str = "/usr/local/bin",
    binary_name: str = "erpc",
    checksum: str | None = None,
) -> Path:
    """Download and install eRPC binary from GitHub releases.

    Args:
        version: Release version tag (e.g., ``"0.0.62"``). Defaults to
            :data:`erpc.ERPC_VERSION` — the pinned compatible version.
        install_dir: Directory to install the binary. Created if it doesn't exist.
        binary_name: Name for the installed binary.
        checksum: Optional SHA256 hex digest for verification.

    Returns:
        Path to the installed binary.

    Raises:
        ERPCError: If the platform is unsupported or checksum verification fails.

    Examples:
        >>> install_erpc()  # installs pinned version
        PosixPath('/usr/local/bin/erpc')

        >>> install_erpc("0.0.62", checksum="abc123...")
        PosixPath('/usr/local/bin/erpc')

    """
    if version is None:
        from erpc import ERPC_VERSION

        version = ERPC_VERSION
    artifact = get_platform_binary_name()
    url = f"{GITHUB_RELEASES_URL}/{version}/{artifact}"
    dest_dir = Path(install_dir)
    dest_dir.mkdir(parents=True, exist_ok=True)
    dest = dest_dir / binary_name

    logger.info("Downloading eRPC %s from %s", version, url)
    urllib.request.urlretrieve(url, str(dest))

    if checksum is not None:
        try:
            verify_checksum(dest, checksum)
        except ERPCError:
            dest.unlink(missing_ok=True)
            raise

    # Make executable
    dest.chmod(dest.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)

    logger.info("Installed eRPC %s to %s", version, dest)
    return dest