Skip to content

Commit b156338

Browse files
committed
fix(ZephyrSetup): Add pip and virtualenv installation for embedded python on Windows
Signed-off-by: paulober <[email protected]>
1 parent d3978e7 commit b156338

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/utils/setupZephyr.mts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import VersionBundlesLoader, { type VersionBundle } from "./versionBundles.mjs";
2626
import {
2727
CURRENT_DTC_VERSION,
2828
CURRENT_GPERF_VERSION,
29+
GET_PIP_URL,
2930
LICENSE_URL_7ZIP,
3031
OPENOCD_VERSION,
3132
SDK_REPOSITORY_URL,
@@ -355,6 +356,84 @@ async function checkPicotool(latestVb: VersionBundle): Promise<boolean> {
355356
);
356357
}
357358

359+
async function preparePython(python3Path: string): Promise<boolean> {
360+
if (!python3Path.includes(".pico-sdk")) {
361+
return true;
362+
}
363+
364+
// download get-pip.py and run it
365+
const getPipUrl = new URL(GET_PIP_URL);
366+
const getPipTarget = join(dirname(python3Path), "get-pip.py");
367+
368+
const downloadResult = await downloadFileGot(getPipUrl, getPipTarget);
369+
if (!downloadResult) {
370+
Logger.error(
371+
LoggerSource.zephyrSetup,
372+
"Failed to download get-pip.py for Python setup."
373+
);
374+
375+
return false;
376+
}
377+
378+
const pipInstallCommand: string = `"${python3Path}" "${getPipTarget}"`;
379+
380+
const result = await _runCommand(pipInstallCommand, {
381+
windowsHide: true,
382+
});
383+
if (result !== 0) {
384+
Logger.error(
385+
LoggerSource.zephyrSetup,
386+
"Failed to install pip for Python setup."
387+
);
388+
389+
return false;
390+
}
391+
392+
// add pip to python path by modifying the ._pth file
393+
const dirContents = await workspace.fs.readDirectory(
394+
Uri.file(dirname(python3Path))
395+
);
396+
397+
const pthFile = dirContents.find(item => item[0].endsWith("._pth"));
398+
if (pthFile === undefined) {
399+
Logger.error(
400+
LoggerSource.zephyrSetup,
401+
"Failed to find ._pth file for Python setup."
402+
);
403+
404+
return false;
405+
}
406+
407+
const pthFileUri = Uri.file(join(dirname(python3Path), pthFile[0]));
408+
let pthFileContents = await workspace.fs.readFile(pthFileUri);
409+
let pthFileString = pthFileContents.toString();
410+
pthFileString = pthFileString.replace(
411+
"#import site",
412+
"Lib\\site-packages\nScripts\n.\n\n#import site"
413+
);
414+
pthFileContents = Buffer.from(pthFileString, "utf-8");
415+
await workspace.fs.writeFile(pthFileUri, pthFileContents);
416+
417+
const pipPathCommand: string =
418+
`"${python3Path}" -m pip install --upgrade ` +
419+
"pip setuptools wheel virtualenv";
420+
421+
const pipResult = await _runCommand(pipPathCommand, {
422+
windowsHide: true,
423+
});
424+
425+
if (pipResult !== 0) {
426+
Logger.error(
427+
LoggerSource.zephyrSetup,
428+
"Failed to upgrade pip, setuptools, wheel and virtualenv."
429+
);
430+
431+
return false;
432+
}
433+
434+
return true;
435+
}
436+
358437
async function checkSdk(
359438
extensionUri: Uri,
360439
latestVb: [string, VersionBundle],
@@ -926,6 +1005,18 @@ export async function setupZephyr(
9261005

9271006
return false;
9281007
}
1008+
if (!(await preparePython(python3Path))) {
1009+
progress.report({
1010+
message: "Failed",
1011+
increment: 100,
1012+
});
1013+
void window.showErrorMessage(
1014+
"Failed to prepare Python for Zephyr setup. " +
1015+
"Cannot continue Zephyr setup."
1016+
);
1017+
1018+
return false;
1019+
}
9291020

9301021
// required for svd files
9311022
const sdk = await checkSdk(data.extUri, latestVb, python3Path);

src/utils/sharedConstants.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const WINDOWS_X86_PYTHON_DOWNLOAD_URL =
33
export const WINDOWS_ARM64_PYTHON_DOWNLOAD_URL =
44
"https://www.python.org/ftp/python/3.13.7/python-3.13.7-embed-arm64.zip";
55
export const CURRENT_PYTHON_VERSION = "3.13.7";
6+
export const GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py";
67

78
export const CURRENT_DATA_VERSION = "0.18.0";
89
export const OPENOCD_VERSION = "0.12.0+dev";

0 commit comments

Comments
 (0)