Skip to content

Commit

Permalink
update example creation to use idfSetup python if exist
Browse files Browse the repository at this point in the history
  • Loading branch information
brianignacio5 committed Jan 17, 2025
1 parent 8986954 commit a5f5a77
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
11 changes: 10 additions & 1 deletion src/examples/ExamplesPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getExamplesList, IExampleCategory } from "./Example";
import { ComponentManagerUIPanel } from "../component-manager/panel";
import { OutputChannel } from "../logger/outputChannel";
import { IdfSetup } from "../views/setup/types";
import { getSystemPythonFromSettings } from "../pythonManager";

export class ExamplesPlanel {
public static currentPanel: ExamplesPlanel | undefined;
Expand Down Expand Up @@ -224,7 +225,15 @@ export class ExamplesPlanel {
const isWin = process.platform === "win32" ? "Win" : "";
settingsJson["idf.espIdfPath" + isWin] = idfSetup.idfPath;
settingsJson["idf.toolsPath" + isWin] = idfSetup.toolsPath;
settingsJson["idf.pythonInstallPath"] = idfSetup.sysPythonPath;
if (idfSetup.python) {
settingsJson["idf.pythonInstallPath"] = await getSystemPythonFromSettings(
idfSetup.python,
idfSetup.idfPath,
idfSetup.toolsPath
);
} else {
settingsJson["idf.pythonInstallPath"] = idfSetup.sysPythonPath;
}
await writeJSON(settingsJsonPath, settingsJson, {
spaces: vscode.workspace.getConfiguration().get("editor.tabSize") || 2,
});
Expand Down
9 changes: 8 additions & 1 deletion src/newProject/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { readParameter } from "../idfConfiguration";
import { readJSON } from "fs-extra";
import { Uri } from "vscode";
import { IdfSetup } from "../views/setup/types";
import { getSystemPythonFromSettings } from "../pythonManager";

export async function setCurrentSettingsInTemplate(
settingsJsonPath: string,
Expand All @@ -35,7 +36,13 @@ export async function setCurrentSettingsInTemplate(
if (idfSetup.idfPath) {
settingsJson["idf.espIdfPath" + isWin] = idfSetup.idfPath;
}
if (idfSetup.sysPythonPath) {
if (idfSetup.python) {
settingsJson["idf.pythonInstallPath"] = await getSystemPythonFromSettings(
idfSetup.python,
idfSetup.idfPath,
idfSetup.toolsPath
);
} else {
settingsJson["idf.pythonInstallPath"] = idfSetup.sysPythonPath;
}
if (adfPathDir) {
Expand Down
39 changes: 22 additions & 17 deletions src/pythonManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,17 @@ export async function getPythonPath(workspaceFolder: Uri) {
return sysPythonBinPath;
}

export async function getSystemPython(workspaceFolder: Uri) {
let pythonBinPath = readParameter(
"idf.pythonBinPath",
workspaceFolder
) as string;
export async function getSystemPythonFromSettings(
pythonBinPath: string,
espIdfPath: string,
espIdfToolsPath: string
) {
const pythonBinPathExists = await pathExists(pythonBinPath);
if (pythonBinPathExists) {
const pythonCode = `import sys; print('{}'.format(sys.base_prefix))`;
const args = ["-c", pythonCode];
const workingDir =
workspaceFolder && workspaceFolder.fsPath
? workspaceFolder.fsPath
: __dirname;
const pythonVersion = (
await utils.execChildProcess(pythonBinPath, args, workingDir)
await utils.execChildProcess(pythonBinPath, args, __dirname)
).replace(/(\n|\r|\r\n)/gm, "");
const pyDir =
process.platform === "win32" ? ["python.exe"] : ["bin", "python3"];
Expand All @@ -371,18 +367,13 @@ export async function getSystemPython(workspaceFolder: Uri) {
const sysPythonBinPathList = await getUnixPythonList(__dirname);
return sysPythonBinPathList.length ? sysPythonBinPathList[0] : "python3";
} else {
const idfPathDir = readParameter("idf.espIdfPath", workspaceFolder);
const idfToolsDir = readParameter(
"idf.toolsPath",
workspaceFolder
) as string;
const idfVersion = await utils.getEspIdfFromCMake(idfPathDir);
const idfVersion = await utils.getEspIdfFromCMake(espIdfPath);
const pythonVersionToUse =
idfVersion >= "5.0"
? ESP.URL.IDF_EMBED_PYTHON.VERSION
: ESP.URL.OLD_IDF_EMBED_PYTHON.VERSION;
const idfPyDestPath = join(
idfToolsDir,
espIdfToolsPath,
"tools",
"idf-python",
pythonVersionToUse,
Expand All @@ -393,6 +384,20 @@ export async function getSystemPython(workspaceFolder: Uri) {
}
}

export async function getSystemPython(workspaceFolder: Uri) {
let pythonBinPath = readParameter(
"idf.pythonBinPath",
workspaceFolder
) as string;
const idfPathDir = readParameter("idf.espIdfPath", workspaceFolder);
const idfToolsDir = readParameter("idf.toolsPath", workspaceFolder) as string;
return await getSystemPythonFromSettings(
pythonBinPath,
idfPathDir,
idfToolsDir
);
}

export async function getPythonEnvPath(
espIdfDir: string,
idfToolsDir: string,
Expand Down

0 comments on commit a5f5a77

Please sign in to comment.