Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 61 additions & 17 deletions crates/pcb-kicad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ use std::path::Path;
use std::process::Command;
use tempfile::NamedTempFile;

mod path_helpers {
use dirs;
pub(crate) fn home_dir_to_string() -> String {
dirs::home_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string()
}
}

#[cfg(target_os = "macos")]
mod paths {
use crate::path_helpers;

pub(crate) fn python_interpreter() -> String {
std::env::var("KICAD_PYTHON_INTERPRETER").unwrap_or_else(|_|
"/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/Current/bin/python3".to_string()).replace("~", dirs::home_dir().unwrap_or_default().to_str().unwrap_or_default())
Expand Down Expand Up @@ -38,19 +51,28 @@ mod paths {
})
.replace(
"~",
dirs::home_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default(),
path_helpers::home_dir_to_string().as_str(),
)
}
}

#[cfg(target_os = "windows")]
mod paths {
use std::path::Path;
use crate::path_helpers;

pub(crate) fn python_interpreter() -> String {
std::env::var("KICAD_PYTHON_INTERPRETER")
.unwrap_or_else(|_| r"C:\Program Files\KiCad\9.0\bin\python.exe".to_string())
let paths = vec![
std::env::var("KICAD_PYTHON_INTERPRETER")
.unwrap_or_else(|_| "python.exe".to_string()),
Comment on lines +66 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this default to using the system python.exe if KICAD_PYTHON_INTERPRETER isn't defined?

Copy link
Author

@jglanz jglanz Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual interpreter will/should run from the PATH, but the site-packages functions will not recognize the directory structure.

Personally, I'd create a fallback fn to scan the path (equivalent ofwhich) would facilitate what you are suggesting in a more complete way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated the PR to resolve a compilation issue the windows fix caused the other build targets

r"C:\Program Files\KiCad\9.0\bin\python.exe".to_string(),
r"~\AppData\Local\Programs\KiCad\9.0\bin\python.exe".to_string()
.replace("~", path_helpers::home_dir_to_string().as_str())
];
paths.iter().find(|p| Path::new(p).exists())
.cloned()
.unwrap()

}

pub(crate) fn python_site_packages() -> String {
Expand All @@ -60,22 +82,31 @@ mod paths {
})
.replace(
"~",
dirs::home_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default(),
path_helpers::home_dir_to_string().as_str(),
)
}

pub(crate) fn venv_site_packages() -> String {
dirs::home_dir()
.unwrap_or_default()
.join(".diode")
.join("venv")
let venv_path = Path::new(python_interpreter().as_str())
.parent()
.unwrap_or_else(|| Path::new(""))
.join("Lib")
.join("site-packages")
.to_string_lossy()
.to_string()
.join("site-packages");

if venv_path.exists() {
venv_path
.to_string_lossy()
.to_string()
} else {
dirs::home_dir()
.unwrap_or_default()
.join(".diode")
.join("venv")
.join("Lib")
.join("site-packages")
.to_string_lossy()
.to_string()
}
}

pub(crate) fn kicad_cli() -> String {
Expand All @@ -86,6 +117,8 @@ mod paths {

#[cfg(target_os = "linux")]
mod paths {
use crate::path_helpers;

pub(crate) fn python_interpreter() -> String {
std::env::var("KICAD_PYTHON_INTERPRETER").unwrap_or_else(|_| "/usr/bin/python3".to_string())
}
Expand Down Expand Up @@ -155,6 +188,17 @@ fn check_kicad_python() -> Result<()> {
));
}

let python_venv_site_packages = paths::venv_site_packages();
let python_site_packages = paths::python_site_packages();
if !Path::new(&python_venv_site_packages).exists() && !Path::new(&python_site_packages).exists() {
return Err(anyhow!(
"KiCad Python site packages were not found at ({}) or ({}): \n\
Please ensure at least one of these locations is valid OR specify a non-standard location by \n\
setting the KICAD_PYTHON_SITE_PACKAGES environment variable.",
python_venv_site_packages,
python_site_packages
));
}
// Try to run python --version to verify it's executable
match Command::new(&python_path).arg("--version").output() {
Ok(output) if output.status.success() => Ok(()),
Expand Down
Loading