forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Modify environment info worker to support new type and to work with resolver #13997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07dac60
Modify environment info worker to support new environment type
b3e4222
Do not replace existing fields and return a new object
d31f823
Modify cache to carry deferred instead
87d84e6
Change worker to return interpreter information instead
148aaae
Handle error and rename
6cf5a3e
Fix bug with interpreterInfo.py
eece52a
Code reviews
b5ce54a
Rename old PythonEnvInfo type to InterpreterInfoJson
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { PythonVersion } from '.'; | ||
import { interpreterInfo as getInterpreterInfoCommand, PythonEnvInfo } from '../../../common/process/internal/scripts'; | ||
import { Architecture } from '../../../common/utils/platform'; | ||
import { copyPythonExecInfo, PythonExecInfo } from '../../exec'; | ||
import { parseVersion } from './pythonVersion'; | ||
|
||
type PythonEnvInformation = { | ||
arch: Architecture; | ||
executable: { | ||
filename: string; | ||
sysPrefix: string; | ||
mtime: number; | ||
ctime: number; | ||
}; | ||
version: PythonVersion; | ||
}; | ||
|
||
/** | ||
* Compose full interpreter information based on the given data. | ||
* | ||
* The data format corresponds to the output of the `interpreterInfo.py` script. | ||
* | ||
* @param python - the path to the Python executable | ||
* @param raw - the information returned by the `interpreterInfo.py` script | ||
*/ | ||
export function extractPythonEnvInfo(python: string, raw: PythonEnvInfo): PythonEnvInformation { | ||
const rawVersion = `${raw.versionInfo.slice(0, 3).join('.')}-${raw.versionInfo[3]}`; | ||
const version = parseVersion(rawVersion); | ||
version.sysVersion = raw.sysVersion; | ||
karrtikr marked this conversation as resolved.
Show resolved
Hide resolved
karrtikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
arch: raw.is64Bit ? Architecture.x64 : Architecture.x86, | ||
executable: { | ||
filename: python, | ||
sysPrefix: raw.sysPrefix, | ||
mtime: -1, | ||
ctime: -1, | ||
}, | ||
version: parseVersion(rawVersion), | ||
}; | ||
} | ||
|
||
type ShellExecResult = { | ||
stdout: string; | ||
stderr?: string; | ||
}; | ||
type ShellExecFunc = (command: string, timeout: number) => Promise<ShellExecResult>; | ||
|
||
type Logger = { | ||
info(msg: string): void; | ||
error(msg: string): void; | ||
}; | ||
|
||
/** | ||
* Collect full interpreter information from the given Python executable. | ||
* | ||
* @param python - the information to use when running Python | ||
* @param shellExec - the function to use to exec Python | ||
* @param logger - if provided, used to log failures or other info | ||
*/ | ||
export async function getInterpreterInfo( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of it is copied from |
||
python: PythonExecInfo, | ||
shellExec: ShellExecFunc, | ||
logger?: Logger, | ||
): Promise<PythonEnvInformation | undefined> { | ||
const [args, parse] = getInterpreterInfoCommand(); | ||
const info = copyPythonExecInfo(python, args); | ||
const argv = [info.command, ...info.args]; | ||
|
||
// Concat these together to make a set of quoted strings | ||
const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '\\\\')}"`), ''); | ||
|
||
// Try shell execing the command, followed by the arguments. This will make node kill the process if it | ||
// takes too long. | ||
// Sometimes the python path isn't valid, timeout if that's the case. | ||
// See these two bugs: | ||
// https://github.com/microsoft/vscode-python/issues/7569 | ||
// https://github.com/microsoft/vscode-python/issues/7760 | ||
const result = await shellExec(quoted, 15000); | ||
if (result.stderr) { | ||
if (logger) { | ||
logger.error(`Failed to parse interpreter information for ${argv} stderr: ${result.stderr}`); | ||
} | ||
return undefined; | ||
} | ||
const json = parse(result.stdout); | ||
if (logger) { | ||
logger.info(`Found interpreter for ${argv}`); | ||
} | ||
return extractPythonEnvInfo(python.pythonExecutable, json); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { PythonReleaseLevel, PythonVersion } from '.'; | ||
import { EMPTY_VERSION, parseBasicVersionInfo } from '../../../common/utils/version'; | ||
|
||
export function parseVersion(versionStr: string): PythonVersion { | ||
const parsed = parseBasicVersionInfo<PythonVersion>(versionStr); | ||
if (!parsed) { | ||
if (versionStr === '') { | ||
return EMPTY_VERSION as PythonVersion; | ||
} | ||
throw Error(`invalid version ${versionStr}`); | ||
} | ||
const { version, after } = parsed; | ||
const match = after.match(/^(a|b|rc)(\d+)$/); | ||
if (match) { | ||
const [, levelStr, serialStr] = match; | ||
let level: PythonReleaseLevel; | ||
if (levelStr === 'a') { | ||
level = PythonReleaseLevel.Alpha; | ||
} else if (levelStr === 'b') { | ||
level = PythonReleaseLevel.Beta; | ||
} else if (levelStr === 'rc') { | ||
level = PythonReleaseLevel.Candidate; | ||
} else { | ||
throw Error('unreachable!'); | ||
} | ||
version.release = { | ||
level, | ||
serial: parseInt(serialStr, 10), | ||
}; | ||
} | ||
return version; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.