Skip to content

jettex.utils

Utility functions for platform detection, paths, and execution.

Module Overview

This module provides low-level utilities used by other modules.

from jettex import (
    is_tinytex_installed,
    get_tinytex_root,
    get_bin_dir,
    get_executable,
    ensure_tinytex_in_path
)

Functions

get_platform()

Get the current platform identifier.

Returns:

Name Type Description
str str

'windows', 'darwin', or 'linux'

Source code in jettex/utils.py
def get_platform() -> str:
    """Get the current platform identifier.

    Returns:
        str: 'windows', 'darwin', or 'linux'
    """
    system = platform.system().lower()
    if system == "darwin":
        return "darwin"
    elif system == "windows":
        return "windows"
    else:
        return "linux"

get_arch()

Get the current CPU architecture.

Returns:

Name Type Description
str str

'x86_64', 'aarch64', or 'i386'

Source code in jettex/utils.py
def get_arch() -> str:
    """Get the current CPU architecture.

    Returns:
        str: 'x86_64', 'aarch64', or 'i386'
    """
    machine = platform.machine().lower()
    if machine in ("x86_64", "amd64"):
        return "x86_64"
    elif machine in ("arm64", "aarch64"):
        return "aarch64"
    elif machine in ("i386", "i686"):
        return "i386"
    return machine

get_download_url(version=1, release_version='')

Get the download URL for TinyTeX.

Parameters:

Name Type Description Default
version int

TinyTeX version (0, 1, or 2)

1
release_version str

Specific release version (e.g., 'v2025.12'), or empty for latest

''

Returns:

Name Type Description
str str

Full download URL

Source code in jettex/utils.py
def get_download_url(version: int = 1, release_version: str = "") -> str:
    """Get the download URL for TinyTeX.

    Args:
        version: TinyTeX version (0, 1, or 2)
        release_version: Specific release version (e.g., 'v2025.12'), or empty for latest

    Returns:
        str: Full download URL
    """
    plat = get_platform()

    # Get release version
    if not release_version:
        release_version = get_latest_release_version()

    # Determine file extension by platform
    if plat == "windows":
        ext = "zip"
    elif plat == "darwin":
        ext = "tgz"
    else:
        ext = "tar.gz"

    # Build filename: TinyTeX-{0,1,or nothing}-{release_version}.{ext}
    if version == 0:
        filename = f"TinyTeX-0-{release_version}.{ext}"
    elif version == 1:
        filename = f"TinyTeX-1-{release_version}.{ext}"
    else:
        # Version 2 or default uses just "TinyTeX"
        filename = f"TinyTeX-{release_version}.{ext}"

    return f"{TINYTEX_RELEASES_BASE}/{release_version}/{filename}"

get_default_install_dir()

Get the default TinyTeX installation directory.

Returns:

Name Type Description
Path Path

Default installation directory

Source code in jettex/utils.py
def get_default_install_dir() -> Path:
    """Get the default TinyTeX installation directory.

    Returns:
        Path: Default installation directory
    """
    plat = get_platform()

    if plat == "windows":
        # Use APPDATA on Windows
        appdata = os.environ.get("APPDATA", os.path.expanduser("~"))
        return Path(appdata) / "TinyTeX"
    elif plat == "darwin":
        # Use ~/Library/TinyTeX on macOS
        return Path.home() / "Library" / "TinyTeX"
    else:
        # Use ~/.TinyTeX on Linux
        return Path.home() / ".TinyTeX"

get_tinytex_root()

Get the TinyTeX root directory if installed.

Checks environment variable TINYTEX_HOME first, then default location.

Returns:

Type Description
Optional[Path]

Path or None: TinyTeX root directory if found

Source code in jettex/utils.py
def get_tinytex_root() -> Optional[Path]:
    """Get the TinyTeX root directory if installed.

    Checks environment variable TINYTEX_HOME first, then default location.

    Returns:
        Path or None: TinyTeX root directory if found
    """
    # Check environment variable first
    env_home = os.environ.get("TINYTEX_HOME")
    if env_home:
        path = Path(env_home)
        if path.exists():
            return path

    # Check default location
    default = get_default_install_dir()
    if default.exists():
        return default

    return None

get_bin_dir()

Get the TinyTeX bin directory containing executables.

Returns:

Type Description
Optional[Path]

Path or None: bin directory if found

Source code in jettex/utils.py
def get_bin_dir() -> Optional[Path]:
    """Get the TinyTeX bin directory containing executables.

    Returns:
        Path or None: bin directory if found
    """
    root = get_tinytex_root()
    if root is None:
        return None

    plat = get_platform()
    arch = get_arch()

    # Construct platform-specific bin path
    if plat == "windows":
        bin_subdir = "win32"
    elif plat == "darwin":
        if arch == "aarch64":
            bin_subdir = "universal-darwin"
        else:
            bin_subdir = "universal-darwin"
    else:
        if arch == "aarch64":
            bin_subdir = "aarch64-linux"
        else:
            bin_subdir = "x86_64-linux"

    # Check various possible structures
    candidates = [
        root / "bin" / bin_subdir,
        root / ".TinyTeX" / "bin" / bin_subdir,
        root / "bin" / "windows",
        root / ".TinyTeX" / "bin" / "windows",
        root / "bin",
    ]

    for candidate in candidates:
        if candidate.exists():
            return candidate

    return None

get_executable(name)

Get the path to a TinyTeX executable.

Parameters:

Name Type Description Default
name str

Executable name (e.g., 'pdflatex', 'tlmgr')

required

Returns:

Type Description
Optional[Path]

Path or None: Full path to executable if found

Source code in jettex/utils.py
def get_executable(name: str) -> Optional[Path]:
    """Get the path to a TinyTeX executable.

    Args:
        name: Executable name (e.g., 'pdflatex', 'tlmgr')

    Returns:
        Path or None: Full path to executable if found
    """
    bin_dir = get_bin_dir()
    if bin_dir is None:
        return None

    plat = get_platform()

    if plat == "windows":
        # TeX Live on Windows ships tools as .exe or .bat (tlmgr is often .bat).
        for suffix in (".exe", ".bat", ".cmd", ".ps1"):
            exe = bin_dir / f"{name}{suffix}"
            if exe.exists():
                return exe
    else:
        exe = bin_dir / name
        if exe.exists():
            return exe

    return None

is_tinytex_installed()

Check if TinyTeX is installed.

Returns:

Name Type Description
bool bool

True if TinyTeX is installed and usable

Source code in jettex/utils.py
def is_tinytex_installed() -> bool:
    """Check if TinyTeX is installed.

    Returns:
        bool: True if TinyTeX is installed and usable
    """
    bin_dir = get_bin_dir()
    if bin_dir is None:
        return False

    # Check for tlmgr which should always exist
    tlmgr = get_executable("tlmgr")
    return tlmgr is not None and tlmgr.exists()

add_to_path(directory)

Add a directory to the current process PATH.

Parameters:

Name Type Description Default
directory Path

Directory to add to PATH

required
Source code in jettex/utils.py
def add_to_path(directory: Path) -> None:
    """Add a directory to the current process PATH.

    Args:
        directory: Directory to add to PATH
    """
    current_path = os.environ.get("PATH", "")
    dir_str = str(directory)

    if dir_str not in current_path:
        os.environ["PATH"] = f"{dir_str}{os.pathsep}{current_path}"

ensure_tinytex_in_path()

Ensure TinyTeX bin directory is in PATH.

Returns:

Name Type Description
bool bool

True if TinyTeX is available in PATH

Source code in jettex/utils.py
def ensure_tinytex_in_path() -> bool:
    """Ensure TinyTeX bin directory is in PATH.

    Returns:
        bool: True if TinyTeX is available in PATH
    """
    bin_dir = get_bin_dir()
    if bin_dir is None:
        return False

    add_to_path(bin_dir)
    return True

run_command(cmd, cwd=None, capture_output=True, check=False, timeout=None)

Run a command and return the result.

Parameters:

Name Type Description Default
cmd List[str]

Command and arguments as list

required
cwd Optional[Path]

Working directory

None
capture_output bool

Capture stdout/stderr

True
check bool

Raise exception on non-zero exit

False
timeout Optional[int]

Command timeout in seconds

None

Returns:

Name Type Description
CompletedProcess CompletedProcess

Result of the command

Source code in jettex/utils.py
def run_command(
    cmd: List[str],
    cwd: Optional[Path] = None,
    capture_output: bool = True,
    check: bool = False,
    timeout: Optional[int] = None,
) -> subprocess.CompletedProcess:
    """Run a command and return the result.

    Args:
        cmd: Command and arguments as list
        cwd: Working directory
        capture_output: Capture stdout/stderr
        check: Raise exception on non-zero exit
        timeout: Command timeout in seconds

    Returns:
        CompletedProcess: Result of the command
    """
    kwargs = {
        "cwd": cwd,
        "capture_output": capture_output,
        "text": True,
    }

    if timeout:
        kwargs["timeout"] = timeout

    result = subprocess.run(cmd, **kwargs)

    if check and result.returncode != 0:
        raise subprocess.CalledProcessError(
            result.returncode, cmd, result.stdout, result.stderr
        )

    return result

Examples

Platform Detection

from jettex.utils import get_platform, get_arch

platform = get_platform()  # 'darwin', 'linux', or 'windows'
arch = get_arch()          # 'x86_64', 'aarch64', etc.

print(f"Running on {platform} ({arch})")

Finding Executables

from jettex import get_executable, get_bin_dir

# Get bin directory
bin_dir = get_bin_dir()
print(f"Binaries at: {bin_dir}")

# Find specific executable
pdflatex = get_executable("pdflatex")
if pdflatex:
    print(f"pdflatex: {pdflatex}")

PATH Management

from jettex import ensure_tinytex_in_path

# Add TinyTeX to current process PATH
if ensure_tinytex_in_path():
    print("TinyTeX is now in PATH")

Check Installation

from jettex import is_tinytex_installed, get_tinytex_root

if is_tinytex_installed():
    root = get_tinytex_root()
    print(f"TinyTeX installed at: {root}")
else:
    print("TinyTeX not installed")

Platform-Specific Paths

Platform Default Install Dir Bin Subdir
Windows %APPDATA%\TinyTeX bin\win32
macOS ~/Library/TinyTeX bin/universal-darwin
Linux (x86_64) ~/.TinyTeX bin/x86_64-linux
Linux (arm64) ~/.TinyTeX bin/aarch64-linux