# Test detection functionsprint(f"is_windows(): {is_windows()}")print(f"is_macos(): {is_macos()}")print(f"is_linux(): {is_linux()}")print(f"is_apple_silicon(): {is_apple_silicon()}")# Exactly one of these should be Trueassertsum([is_windows(), is_macos(), is_linux()]) ==1
Get current platform string for manifest filtering.
Returns strings like ‘linux-x64’, ‘darwin-arm64’, ‘win-x64’.
# Test platform stringcurrent = get_current_platform()print(f"Current platform: {current}")# Should be one of the expected formatsvalid_prefixes = ["linux-", "darwin-", "win-"]assertany(current.startswith(p) for p in valid_prefixes)
Current platform: linux-x64
Path Utilities
Functions for cross-platform path handling, particularly for conda environments.
def terminate_process( process:Popen, # Process to terminate timeout:float=2.0, # Seconds to wait before force kill)->None:
Terminate a subprocess gracefully, with fallback to force kill.
On all platforms: 1. Calls process.terminate() (SIGTERM on Unix, TerminateProcess on Windows) 2. Waits for timeout seconds 3. If still running, calls process.kill() (SIGKILL on Unix, TerminateProcess on Windows)
def run_shell_command( cmd:str, # Shell command to execute check:bool=True, # Whether to raise on non-zero exit capture_output:bool=False, # Whether to capture stdout/stderr kwargs:VAR_KEYWORD)->CompletedProcess: # Additional kwargs passed to subprocess.run
Run a shell command cross-platform.
Unlike using shell=True with executable=‘/bin/bash’, this function uses the platform’s default shell: - Linux/macOS: /bin/sh (or $SHELL) - Windows: cmd.exe
# Test conda_env_exists (will return False for non-existent env)exists = conda_env_exists("this-env-should-not-exist-12345")print(f"Non-existent env exists: {exists}")assert exists ==False# Test with base env "miniforge3" (should exist if miniforge3 is installed)base_exists = conda_env_exists("miniforge3")print(f"Base env exists: {base_exists}")
Non-existent env exists: False
Base env exists: True
Conda/Micromamba Management
Functions for managing conda and micromamba runtimes, including downloading micromamba binaries and building commands with proper prefix handling for project-local mode.
def download_micromamba( dest_path:Path, # Destination path for the micromamba binary platform_str:Optional=None, # Platform string, uses current if None show_progress:bool=True, # Whether to print progress messages)->bool: # True if download succeeded
Download and extract micromamba binary to the specified path.
# Test micromamba URL functionscurrent_platform = get_current_platform()url = get_micromamba_download_url()print(f"Platform: {current_platform}")print(f"Download URL: {url}")# Verify all platforms have URLsfor plat in ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64", "win-x64"]:assert plat in MICROMAMBA_URLS, f"Missing URL for {plat}"print(f" {plat}: {MICROMAMBA_URLS[plat]}")