Skip to content

Commit

Permalink
Better python utilization in stub switcher
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Oct 17, 2024
1 parent ce1d3eb commit 232a1af
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,8 @@ export default class Activator {
version.includes(" - ")
? displayStringToStubPort(versionParts[0])
: versionParts[0],
this.settings!
this.settings!,
this.pythonPath
);

if (result) {
Expand Down
85 changes: 59 additions & 26 deletions src/stubs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -286,51 +286,84 @@ export function displayStringToStubPort(displayString: string): string {
export async function installStubsByVersion(
version: string,
port: string,
settings: Settings
settings: Settings,
pythonPath?: string
): Promise<boolean> {
const pip3: string | null = await which("pip3", { nothrow: true });
// check if pip is available
const pip: string | null = await which("pip", { nothrow: true });

let command = "";
// if not available check for python prefixed installations
if (pip3 === null && pip === null) {
const python3: string | null = await which("python3", { nothrow: true });
// windows py launcher
const py: string | null = await which("py", { nothrow: true });
const isWin = process.platform === "win32";

// if (py ?? python) -m pip returns sth containing "No module named" -> not installed
if (pythonPath) {
command = `"${pythonPath}" -m pip`;

const pyCmd = python3 ?? py;
if (pyCmd !== null) {
const result = execSync(pyCmd + " -m pip");
try {
const result = execSync(`${isWin ? "&" : ""}${command}`, {
windowsHide: true,
shell: process.platform === "win32" ? "powershell" : undefined,
});
if (result.toString("utf-8").toLowerCase().includes("no module named")) {
void window.showWarningMessage(
"The selected python interpreter does not have pip installed."
);
command = "";
}
} catch (error) {
console.log(error);
void window.showErrorMessage(
"python3 or py (with pip) is required (in PATH) to install" +
" stubs different from the included ones."
);
command = "";
}
}

if (command.length <= 0) {
const pip3: string | null = await which("pip3", { nothrow: true });
// check if pip is available
const pip: string | null = await which("pip", { nothrow: true });

// if not available check for python prefixed installations
if (pip3 === null && pip === null) {
const python3: string | null = await which("python3", { nothrow: true });
// windows py launcher
const py: string | null = await which("py", { nothrow: true });

// if (py ?? python) -m pip returns sth containing "No module named" -> not installed

const pyCmd = python3 ?? py;
if (pyCmd !== null) {
const result = execSync(`${isWin ? "&" : ""}"${pyCmd}" -m pip`, {
windowsHide: true,
shell: process.platform === "win32" ? "powershell" : undefined,
});
if (
result.toString("utf-8").toLowerCase().includes("no module named")
) {
void window.showErrorMessage(
`pip module is required (in ${pyCmd}) to install` +
" stubs different from the included ones."
);

return false;
}
command = `"${pyCmd}" -m pip`;
} else {
void window.showErrorMessage(
`pip module is required (in ${pyCmd}) to install` +
"python3 or py is required (in PATH) to install" +
" stubs different from the included ones."
);

return false;
}
command = `"${pyCmd}" -m pip`;
} else {
void window.showErrorMessage(
"python3 or py is required (in PATH) to install" +
" stubs different from the included ones."
);

return false;
assert(pip3 !== null || pip !== null);
command = `"${(pip3 ?? pip)!}"`;
}
} else {
assert(pip3 !== null || pip !== null);
command = `"${(pip3 ?? pip)!}"`;
}

const folderName = `${port}==${version}`;
const target = getStubsPathForVersionPosix(folderName);
mkdirpSync(target);

const isWin = process.platform === "win32";
// install stubs with pip vscode user directory
const result = execSync(
`${isWin ? "&" : ""}${command} install ${port}==${version} ` +
Expand Down

0 comments on commit 232a1af

Please sign in to comment.