Add getValidPythonCommand function and improve dependency check

- Implement getValidPythonCommand in commandsBase.ts to detect Python 3.
- Update dependencyCheck in statusBarViewBase.ts to use getValidPythonCommand.
- Modify install.py to use sys.executable instead of hardcoded 'python3'.
This commit is contained in:
bobo.yang 2023-06-13 08:32:22 +08:00
parent d349ea42a5
commit abba2c7df3
3 changed files with 42 additions and 11 deletions

View File

@ -28,6 +28,23 @@ export function checkDevChatDependency(): boolean {
}
}
export function getValidPythonCommand(): string | undefined {
try {
runCommand('python3 -V');
return 'python3';
} catch (error) {
try {
const version = runCommand('python -V');
if (version.includes('Python 3')) {
return 'python';
}
return undefined;
} catch (error) {
return undefined;
}
}
}
export function getPipxEnvironmentPath(): string | null {
// Get pipx environment
const pipxEnvOutput = runCommand('python3 -m pipx environment').toString();

View File

@ -4,7 +4,7 @@ import { logger } from "../util/logger";
import { UiUtilWrapper } from "../util/uiUtil";
import { TopicManager } from "../topic/topicManager";
import { checkDevChatDependency } from "../contributes/commandsBase";
import { checkDevChatDependency, getValidPythonCommand } from "../contributes/commandsBase";
import { ApiKeyManager } from '../util/apiKey';
@ -37,7 +37,7 @@ export async function dependencyCheck(): Promise<[string, string]> {
// 1. not in a folder
// 2. dependence is invalid
// 3. ready
if (devchatStatus === '' || devchatStatus === 'waiting install devchat') {
if (devchatStatus === '' || devchatStatus === 'Waiting for devchat installation to complete') {
let bOk = true;
let devChat: string | undefined = UiUtilWrapper.getConfiguration('DevChat', 'DevChatPath');
if (!devChat) {
@ -60,20 +60,30 @@ export async function dependencyCheck(): Promise<[string, string]> {
}
}
}
if (devchatStatus === 'not ready') {
if (devchatStatus === 'not ready' || devchatStatus === 'Waiting for Python3 installation to complete') {
// auto install devchat
UiUtilWrapper.runTerminal('DevChat Install', `python3 ${UiUtilWrapper.extensionPath() + "/tools/install.py"}`);
devchatStatus = 'waiting install devchat';
isVersionChangeCompare = true;
// check whether python3 exist
const pythonCommand = getValidPythonCommand();
if (!pythonCommand && devchatStatus === 'not ready') {
UiUtilWrapper.showErrorMessage('Python3 not found.');
devchatStatus = 'Waiting for Python3 installation to complete';
isVersionChangeCompare = true;
} else if (!pythonCommand) {
// Waiting for Python3 installation to complete
} else {
UiUtilWrapper.runTerminal('DevChat Install', `${pythonCommand} ${UiUtilWrapper.extensionPath() + "/tools/install.py"}`);
devchatStatus = 'Waiting for devchat installation to complete';
isVersionChangeCompare = true;
}
}
// check api key
if (apiKeyStatus === '' || apiKeyStatus === 'please set api key') {
if (apiKeyStatus === '' || apiKeyStatus === 'Please set the API key') {
const bOk = await ApiKeyManager.getApiKey();
if (bOk) {
apiKeyStatus = 'ready';
} else {
apiKeyStatus = 'please set api key';
apiKeyStatus = 'Please set the API key';
}
}

View File

@ -2,6 +2,10 @@ import os
import subprocess
import sys
# replace python3 with sys.executable, we will do everything in the same envrionment
pythonCommand = sys.executable
def check_pipx_installed():
try:
subprocess.run(["pipx", "--version"], check=True)
@ -12,7 +16,7 @@ def check_pipx_installed():
def install_pipx():
print("Installing pipx...")
try:
subprocess.run(["python3", "-m", "pip", "install", "pipx", "--force"], check=True)
subprocess.run([pythonCommand, "-m", "pip", "install", "pipx", "--force"], check=True)
print("pipx installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing pipx:", e, file=sys.stderr)
@ -20,8 +24,8 @@ def install_pipx():
def add_pipx_to_path():
print("Adding pipx to PATH...")
subprocess.run(["python3", "-m", "pipx", "ensurepath"], check=True)
result = subprocess.run(["python3", "-m", "pipx", "environment"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
subprocess.run([pythonCommand, "-m", "pipx", "ensurepath"], check=True)
result = subprocess.run([pythonCommand, "-m", "pipx", "environment"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
pipx_path_line = [line for line in result.stdout.splitlines() if "PIPX_BIN_DIR" in line]
if pipx_path_line:
pipx_path = pipx_path_line[0].split('=')[-1].strip()