71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
"""
|
|
Utility functions for detecting and selecting available KiCad API approaches.
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import shutil
|
|
from typing import Tuple, Optional, Literal
|
|
|
|
from kicad_mcp.config import system
|
|
|
|
def check_for_cli_api() -> bool:
|
|
"""Check if KiCad CLI API is available.
|
|
|
|
Returns:
|
|
True if KiCad CLI is available, False otherwise
|
|
"""
|
|
try:
|
|
# Check if kicad-cli is in PATH
|
|
if system == "Windows":
|
|
# On Windows, check for kicad-cli.exe
|
|
kicad_cli = shutil.which("kicad-cli.exe")
|
|
else:
|
|
# On Unix-like systems
|
|
kicad_cli = shutil.which("kicad-cli")
|
|
|
|
if kicad_cli:
|
|
# Verify it's a working kicad-cli
|
|
if system == "Windows":
|
|
cmd = [kicad_cli, "--version"]
|
|
else:
|
|
cmd = [kicad_cli, "--version"]
|
|
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
if result.returncode == 0:
|
|
print(f"Found working kicad-cli: {kicad_cli}")
|
|
return True
|
|
|
|
# Check common installation locations if not found in PATH
|
|
if system == "Windows":
|
|
# Common Windows installation paths
|
|
potential_paths = [
|
|
r"C:\Program Files\KiCad\bin\kicad-cli.exe",
|
|
r"C:\Program Files (x86)\KiCad\bin\kicad-cli.exe"
|
|
]
|
|
elif system == "Darwin": # macOS
|
|
# Common macOS installation paths
|
|
potential_paths = [
|
|
"/Applications/KiCad/KiCad.app/Contents/MacOS/kicad-cli",
|
|
"/Applications/KiCad/kicad-cli"
|
|
]
|
|
else: # Linux
|
|
# Common Linux installation paths
|
|
potential_paths = [
|
|
"/usr/bin/kicad-cli",
|
|
"/usr/local/bin/kicad-cli",
|
|
"/opt/kicad/bin/kicad-cli"
|
|
]
|
|
|
|
# Check each potential path
|
|
for path in potential_paths:
|
|
if os.path.exists(path) and os.access(path, os.X_OK):
|
|
print(f"Found kicad-cli at common location: {path}")
|
|
return True
|
|
|
|
print("KiCad CLI API is not available")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"Error checking for KiCad CLI API: {str(e)}")
|
|
return False
|