Skip to content

Commit c0f8532

Browse files
Magpie Embeddedpaulober
authored andcommitted
Improve installation process of python, venv and 7zip
1 parent d29af69 commit c0f8532

File tree

1 file changed

+94
-40
lines changed

1 file changed

+94
-40
lines changed

src/commands/getPaths.mts

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { rmSync } from "fs";
1+
import { existsSync, readdirSync, rmSync } from "fs";
22
import { CommandWithResult } from "./command.mjs";
33
import { commands, type Uri, window, workspace } from "vscode";
44
import { type ExecOptions, exec, spawnSync } from "child_process";
@@ -718,40 +718,56 @@ export class SetupZephyrCommand extends CommandWithResult<string | undefined> {
718718
window.showInformationMessage("wget installed.");
719719

720720
this._logger.info("Installing 7zip");
721-
const szipURL = new URL("https://7-zip.org/a/7z2409-x64.exe");
722-
const szipResult = await downloadFileGot(
723-
szipURL,
724-
joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "7zip-x64.exe")
725-
);
726-
727-
if (!szipResult) {
728-
window.showErrorMessage(
729-
"Could not download 7zip. Exiting Zephyr Setup"
721+
const szipTargetDirectory = joinPosix("C:\\Program Files\\7-Zip");
722+
if (
723+
existsSync(szipTargetDirectory) &&
724+
readdirSync(szipTargetDirectory).length !== 0
725+
) {
726+
this._logger.info("7-Zip is already installed.");
727+
} else {
728+
const szipURL = new URL("https://7-zip.org/a/7z2409-x64.exe");
729+
const szipResult = await downloadFileGot(
730+
szipURL,
731+
joinPosix(
732+
homedir().replaceAll("\\", "/"),
733+
".pico-sdk",
734+
"7zip-x64.exe"
735+
)
730736
);
731737

732-
return;
733-
}
738+
if (!szipResult) {
739+
window.showErrorMessage(
740+
"Could not download 7-Zip. Exiting Zephyr Setup"
741+
);
734742

735-
const szipCommand: string = "7zip-x64.exe";
736-
const szipInstallResult = await this._runCommand(szipCommand, {
737-
cwd: joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk"),
738-
});
743+
return;
744+
}
739745

740-
if (szipInstallResult !== 0) {
741-
window.showErrorMessage(
742-
"Could not install 7zip. Please ensure 7-Zip is installed." +
743-
"Exiting Zephyr Setup"
744-
);
746+
const szipCommand: string = "7zip-x64.exe";
747+
const szipInstallResult = await this._runCommand(szipCommand, {
748+
cwd: joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk"),
749+
});
745750

746-
return;
747-
}
751+
if (szipInstallResult !== 0) {
752+
window.showErrorMessage(
753+
"Could not install 7-Zip. Please ensure 7-Zip is installed." +
754+
"Exiting Zephyr Setup"
755+
);
748756

749-
// Clean up
750-
rmSync(
751-
joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "7zip-x64.exe")
752-
);
757+
return;
758+
}
759+
760+
// Clean up
761+
rmSync(
762+
joinPosix(
763+
homedir().replaceAll("\\", "/"),
764+
".pico-sdk",
765+
"7zip-x64.exe"
766+
)
767+
);
753768

754-
window.showInformationMessage("7zip installed.");
769+
window.showInformationMessage("7-Zip installed.");
770+
}
755771
}
756772

757773
this._logger.info("Installing OpenOCD");
@@ -846,38 +862,76 @@ manifest:
846862
Buffer.from(zephyrManifestContent)
847863
);
848864

849-
const command: string = [
865+
const createVenvCommandVenv: string = [
866+
`${process.env.ComSpec === "powershell.exe" ? "&" : ""}"${pythonExe}"`,
867+
"-m venv venv",
868+
].join(" ");
869+
const createVenvCommandVirtualenv: string = [
850870
`${process.env.ComSpec === "powershell.exe" ? "&" : ""}"${pythonExe}"`,
851-
"-m " + (process.platform === "win32" ? "virtualenv" : "venv") + " venv",
871+
"-m virtualenv venv",
852872
].join(" ");
853873

854874
// Create a Zephyr workspace, copy the west manifest in and initialise the workspace
855875
workspace.fs.createDirectory(Uri.file(zephyrWorkspaceDirectory));
856876

877+
// Generic result to get value from runCommand calls
878+
let result: number | null;
879+
857880
this._logger.info("Setting up virtual environment for Zephyr");
858-
let result = await this._runCommand(command, {
859-
cwd: zephyrWorkspaceDirectory,
860-
windowsHide: true,
861-
env: customEnv,
862-
});
881+
const venvPython: string = joinPosix(
882+
zephyrWorkspaceDirectory,
883+
"venv",
884+
process.platform === "win32" ? "Scripts" : "bin",
885+
process.platform === "win32" ? "python.exe" : "python"
886+
);
887+
if (existsSync(venvPython)) {
888+
this._logger.info("Existing Python venv found.");
889+
} else {
890+
result = await this._runCommand(createVenvCommandVenv, {
891+
cwd: zephyrWorkspaceDirectory,
892+
windowsHide: true,
893+
env: customEnv,
894+
});
895+
if (result !== 0) {
896+
this._logger.warn(
897+
"Could not create virtual environment with venv," +
898+
"trying with virtualenv..."
899+
);
863900

864-
this._logger.info(`${result}`);
901+
result = await this._runCommand(createVenvCommandVirtualenv, {
902+
cwd: zephyrWorkspaceDirectory,
903+
windowsHide: true,
904+
env: customEnv,
905+
});
906+
907+
if (result !== 0) {
908+
this._logger.error("Could not create virtual environment. Exiting.");
909+
window.showErrorMessage(
910+
"Could not install venv. Exiting Zephyr Setup"
911+
);
912+
913+
return;
914+
}
915+
}
916+
917+
this._logger.info("Venv created.");
918+
}
865919

866-
const venvPythonExe: string = joinPosix(
920+
const venvPythonCommand: string = joinPosix(
867921
process.env.ComSpec === "powershell.exe" ? "&" : "",
868922
zephyrWorkspaceDirectory,
869923
"venv",
870924
process.platform === "win32" ? "Scripts" : "bin",
871925
process.platform === "win32" ? "python.exe" : "python"
872926
);
873927

874-
const command2: string = [
875-
venvPythonExe,
928+
const installWestCommand: string = [
929+
venvPythonCommand,
876930
"-m pip install west pyelftools",
877931
].join(" ");
878932

879933
this._logger.info("Installing Python dependencies for Zephyr");
880-
result = await this._runCommand(command2, {
934+
result = await this._runCommand(installWestCommand, {
881935
cwd: zephyrWorkspaceDirectory,
882936
windowsHide: true,
883937
env: customEnv,

0 commit comments

Comments
 (0)