import shutil
import subprocess
import sys
from pathlib import Path
from typing import Optional
def find_dissolve_binary() -> Optional[str]:
binary_path = shutil.which("dissolve")
if binary_path:
try:
result = subprocess.run(
[binary_path, "--version"], capture_output=True, text=True, timeout=5
)
if result.returncode == 0 and "dissolve" in result.stdout.lower():
return binary_path
except (
subprocess.TimeoutExpired,
subprocess.CalledProcessError,
FileNotFoundError,
):
pass
home = Path.home()
cargo_bin = home / ".cargo" / "bin" / "dissolve"
if cargo_bin.exists():
return str(cargo_bin)
current_dir = Path(__file__).parent.parent
local_binary = current_dir / "target" / "release" / "dissolve"
if local_binary.exists():
return str(local_binary)
return None
def print_installation_instructions() -> None:
print("The dissolve Rust binary is not installed or not found in PATH.")
print()
print("To install the high-performance Rust version:")
print(" cargo install dissolve-python")
print()
print("If you don't have Rust installed:")
print(" curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh")
print(" source ~/.cargo/env")
print(" cargo install dissolve-python")
print()
print("Alternative: You can also use the Python-only version (slower):")
print(" python -m dissolve [arguments]")
def main() -> None:
binary_path = find_dissolve_binary()
if binary_path:
try:
result = subprocess.run([binary_path, *sys.argv[1:]])
sys.exit(result.returncode)
except KeyboardInterrupt:
sys.exit(130) except Exception as e:
print(f"Error running dissolve binary: {e}", file=sys.stderr)
sys.exit(1)
else:
if len(sys.argv) > 1:
print("Error: dissolve Rust binary not found.", file=sys.stderr)
print()
print_installation_instructions()
sys.exit(1)
if __name__ == "__main__":
main()