Skip to content

livery.toolroom.store._trampoline

livery.toolroom.store._trampoline

Native Windows PE32+ executable trampoline for tool launchers.

On Windows, a .cmd launcher routes every invocation through cmd.exe, which interprets ^ as an escape character, expands %VAR%, and re-parses quotes before forwarding %*. Replacing .cmd with a tiny PE32+ console executable for native binary targets lets Windows start the target directly through CreateProcessW with the caller's raw command-line tail byte-for-byte unchanged.

Batch targets (.cmd and .bat) stay behind .cmd wrappers because CreateProcessW does not execute batch scripts directly without cmd.exe.

Each trampoline binary carries a small trailer appended after the PE image::

[PE32+ image]
[UTF-8 target path][NUL][UTF-8 quoted command-line prefix]
[uint32_le payload_byte_length]
[4-byte magic: b"LSHM"]

Functions:

Name Description
is_script_target

Whether target is a Windows batch script that still needs a .cmd shim.

format_command_prefix

Format target and leading args as a Windows CreateProcessW prefix.

write_trampoline

Write a native Windows .exe trampoline at dest forwarding to target.

read_trampoline

Read (target, command_prefix) from a trampoline at path, or None.

Attributes:

Name Type Description
TRAILER_MAGIC bytes
SCRIPT_SUFFIXES tuple[str, ...]

TRAILER_MAGIC module-attribute

TRAILER_MAGIC: bytes = b'LSHM'

SCRIPT_SUFFIXES module-attribute

SCRIPT_SUFFIXES: tuple[str, ...] = ('.cmd', '.bat')

is_script_target

is_script_target(target: Path) -> bool

Whether target is a Windows batch script that still needs a .cmd shim.

Source code in packages/toolroom-store/src/livery/toolroom/store/_trampoline.py
def is_script_target(target: Path) -> bool:
    """Whether *target* is a Windows batch script that still needs a ``.cmd`` shim."""
    return target.suffix.lower() in SCRIPT_SUFFIXES

format_command_prefix

format_command_prefix(target: Path, args: Sequence[str] = ()) -> str

Format target and leading args as a Windows CreateProcessW prefix.

Source code in packages/toolroom-store/src/livery/toolroom/store/_trampoline.py
def format_command_prefix(target: Path, args: Sequence[str] = ()) -> str:
    """Format *target* and leading *args* as a Windows ``CreateProcessW`` prefix."""
    head = f'"{target}"'
    if not args:
        return head
    return f"{head} {subprocess.list2cmdline(args)}"

write_trampoline

write_trampoline(dest: Path, target: Path, args: Sequence[str] = ()) -> Path

Write a native Windows .exe trampoline at dest forwarding to target.

Any existing symlink or file at dest is unlinked before writing so the write never follows an existing link. Returns dest.

Source code in packages/toolroom-store/src/livery/toolroom/store/_trampoline.py
def write_trampoline(dest: Path, target: Path, args: Sequence[str] = ()) -> Path:
    """Write a native Windows ``.exe`` trampoline at *dest* forwarding to *target*.

    Any existing symlink or file at *dest* is unlinked before writing so the
    write never follows an existing link. Returns *dest*.
    """
    dest.parent.mkdir(parents=True, exist_ok=True)
    if dest.is_symlink() or dest.exists():
        dest.unlink()
    prefix = format_command_prefix(target, args)
    payload = str(target).encode("utf-8") + b"\x00" + prefix.encode("utf-8")
    footer = struct.pack("<I", len(payload)) + TRAILER_MAGIC
    dest.write_bytes(_stub_bytes() + payload + footer)
    return dest

read_trampoline

read_trampoline(path: Path) -> tuple[str, str] | None

Read (target, command_prefix) from a trampoline at path, or None.

Source code in packages/toolroom-store/src/livery/toolroom/store/_trampoline.py
def read_trampoline(path: Path) -> tuple[str, str] | None:
    """Read ``(target, command_prefix)`` from a trampoline at *path*, or ``None``."""
    data = path.read_bytes()
    if len(data) < 12 or data[-4:] != TRAILER_MAGIC:
        return None
    (payload_len,) = struct.unpack_from("<I", data, len(data) - 8)
    if payload_len == 0 or payload_len > len(data) - 8:
        return None
    payload = data[len(data) - 8 - payload_len : len(data) - 8]
    sep = payload.find(b"\x00")
    if sep <= 0 or sep + 1 >= len(payload):
        return None
    try:
        target = payload[:sep].decode("utf-8")
        prefix = payload[sep + 1 :].decode("utf-8")
    except UnicodeDecodeError:
        return None
    return target, prefix