forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Detect ActiveState Python runtimes #20534
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 all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d6ba0a7
Detect ActiveState Python runtimes (#20532)
mitchell-as 3381fe5
Attempt to fix Windows test failure.
mitchell-as b768ae6
Fixed invalid JSON in unit test.
mitchell-as 625dd7c
Reduce state command timeout.
mitchell-as 951d522
Address some PR feedback.
mitchell-as b62dd06
Improve ActiveState State Tool detection.
mitchell-as 97a0b57
Addressing more PR feedback.
mitchell-as 819d16f
Remove unnecessary IFilesystem mocks.
mitchell-as 1850c13
Mock up an ActiveState State Tool installation in tests.
mitchell-as 29633fa
Recommend ActiveState Python runtimes for the workspaces they are ass…
mitchell-as 1a0d3c4
Fixed failing unit test by checking for defined interpreter path.
mitchell-as 1d135ae
Added preference for specifying path to ActiveState's State Tool.
mitchell-as 31545bd
Fixed failing Windows unit test.
mitchell-as b219494
Merge branch 'main' into main
mitchell-as 3ef7cd6
Merge branch 'main' into main
mitchell-as 6309806
Do not rely on receiver to cache project info.
mitchell-as 9b01ec0
ActiveState runtimes are not virtual environments.
mitchell-as 8083090
Updated conditional based on PR feedback.
mitchell-as c0ea1be
PR feedback.
mitchell-as e90ae16
Revert: Added preference for specifying path to ActiveState's State T…
mitchell-as 97f6740
Merge remote-tracking branch 'upstream/main'
mitchell-as b5595b2
Re-added preference for specifying path to ActiveState's State Tool.
mitchell-as 8ade316
Merge remote-tracking branch 'upstream/main'
mitchell-as 6eb0ac3
Fix lint error.
mitchell-as 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
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
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
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
44 changes: 44 additions & 0 deletions
44
src/client/pythonEnvironments/base/locators/lowLevel/activestateLocator.ts
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,44 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { ActiveState } from '../../../common/environmentManagers/activestate'; | ||
import { PythonEnvKind } from '../../info'; | ||
import { BasicEnvInfo, IPythonEnvsIterator } from '../../locator'; | ||
import { traceError, traceVerbose } from '../../../../logging'; | ||
import { LazyResourceBasedLocator } from '../common/resourceBasedLocator'; | ||
import { findInterpretersInDir } from '../../../common/commonUtils'; | ||
|
||
export class ActiveStateLocator extends LazyResourceBasedLocator { | ||
public readonly providerId: string = 'activestate'; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public async *doIterEnvs(): IPythonEnvsIterator<BasicEnvInfo> { | ||
const state = await ActiveState.getState(); | ||
if (state === undefined) { | ||
traceVerbose(`Couldn't locate the state binary.`); | ||
return; | ||
} | ||
const projects = await state.getProjects(); | ||
if (projects === undefined) { | ||
traceVerbose(`Couldn't fetch State Tool projects.`); | ||
return; | ||
} | ||
for (const project of projects) { | ||
if (project.executables) { | ||
for (const dir of project.executables) { | ||
try { | ||
traceVerbose(`Looking for Python in: ${project.name}`); | ||
for await (const exe of findInterpretersInDir(dir)) { | ||
traceVerbose(`Found Python executable: ${exe.filename}`); | ||
yield { kind: PythonEnvKind.ActiveState, executablePath: exe.filename }; | ||
} | ||
} catch (ex) { | ||
traceError(`Failed to process State Tool project: ${JSON.stringify(project)}`, ex); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
133 changes: 133 additions & 0 deletions
133
src/client/pythonEnvironments/common/environmentManagers/activestate.ts
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,133 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import * as path from 'path'; | ||
import { dirname } from 'path'; | ||
import { | ||
arePathsSame, | ||
getPythonSetting, | ||
onDidChangePythonSetting, | ||
pathExists, | ||
shellExecute, | ||
} from '../externalDependencies'; | ||
import { cache } from '../../../common/utils/decorators'; | ||
import { traceError, traceVerbose } from '../../../logging'; | ||
import { getOSType, getUserHomeDir, OSType } from '../../../common/utils/platform'; | ||
|
||
export const ACTIVESTATETOOLPATH_SETTING_KEY = 'activeStateToolPath'; | ||
|
||
const STATE_GENERAL_TIMEOUT = 5000; | ||
|
||
export type ProjectInfo = { | ||
name: string; | ||
organization: string; | ||
local_checkouts: string[]; // eslint-disable-line camelcase | ||
executables: string[]; | ||
karrtikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export async function isActiveStateEnvironment(interpreterPath: string): Promise<boolean> { | ||
const execDir = path.dirname(interpreterPath); | ||
const runtimeDir = path.dirname(execDir); | ||
return pathExists(path.join(runtimeDir, '_runtime_store')); | ||
} | ||
|
||
export class ActiveState { | ||
private static statePromise: Promise<ActiveState | undefined> | undefined; | ||
|
||
public static async getState(): Promise<ActiveState | undefined> { | ||
if (ActiveState.statePromise === undefined) { | ||
ActiveState.statePromise = ActiveState.locate(); | ||
} | ||
return ActiveState.statePromise; | ||
} | ||
|
||
constructor() { | ||
onDidChangePythonSetting(ACTIVESTATETOOLPATH_SETTING_KEY, () => { | ||
ActiveState.statePromise = undefined; | ||
}); | ||
} | ||
|
||
public static getStateToolDir(): string | undefined { | ||
const home = getUserHomeDir(); | ||
if (!home) { | ||
return undefined; | ||
} | ||
return getOSType() === OSType.Windows | ||
? path.join(home, 'AppData', 'Local', 'ActiveState', 'StateTool') | ||
: path.join(home, '.local', 'ActiveState', 'StateTool'); | ||
} | ||
|
||
private static async locate(): Promise<ActiveState | undefined> { | ||
const stateToolDir = this.getStateToolDir(); | ||
const stateCommand = | ||
getPythonSetting<string>(ACTIVESTATETOOLPATH_SETTING_KEY) ?? ActiveState.defaultStateCommand; | ||
if (stateToolDir && ((await pathExists(stateToolDir)) || stateCommand !== this.defaultStateCommand)) { | ||
return new ActiveState(); | ||
} | ||
return undefined; | ||
} | ||
|
||
public async getProjects(): Promise<ProjectInfo[] | undefined> { | ||
return this.getProjectsCached(); | ||
} | ||
|
||
private static readonly defaultStateCommand: string = 'state'; | ||
|
||
@cache(30_000, true, 10_000) | ||
karrtikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// eslint-disable-next-line class-methods-use-this | ||
private async getProjectsCached(): Promise<ProjectInfo[] | undefined> { | ||
try { | ||
const stateCommand = | ||
getPythonSetting<string>(ACTIVESTATETOOLPATH_SETTING_KEY) ?? ActiveState.defaultStateCommand; | ||
const result = await shellExecute(`${stateCommand} projects -o editor`, { | ||
timeout: STATE_GENERAL_TIMEOUT, | ||
}); | ||
if (!result) { | ||
return undefined; | ||
} | ||
let output = result.stdout.trimEnd(); | ||
if (output[output.length - 1] === '\0') { | ||
// '\0' is a record separator. | ||
output = output.substring(0, output.length - 1); | ||
} | ||
traceVerbose(`${stateCommand} projects -o editor: ${output}`); | ||
const projects = JSON.parse(output); | ||
ActiveState.setCachedProjectInfo(projects); | ||
return projects; | ||
} catch (ex) { | ||
traceError(ex); | ||
return undefined; | ||
} | ||
} | ||
|
||
// Stored copy of known projects. isActiveStateEnvironmentForWorkspace() is | ||
// not async, so getProjects() cannot be used. ActiveStateLocator sets this | ||
// when it resolves project info. | ||
private static cachedProjectInfo: ProjectInfo[] = []; | ||
|
||
public static getCachedProjectInfo(): ProjectInfo[] { | ||
return this.cachedProjectInfo; | ||
} | ||
|
||
private static setCachedProjectInfo(projects: ProjectInfo[]): void { | ||
this.cachedProjectInfo = projects; | ||
} | ||
} | ||
|
||
export function isActiveStateEnvironmentForWorkspace(interpreterPath: string, workspacePath: string): boolean { | ||
const interpreterDir = dirname(interpreterPath); | ||
for (const project of ActiveState.getCachedProjectInfo()) { | ||
if (project.executables) { | ||
for (const [i, dir] of project.executables.entries()) { | ||
// Note multiple checkouts for the same interpreter may exist. | ||
// Check them all. | ||
if (arePathsSame(dir, interpreterDir) && arePathsSame(workspacePath, project.local_checkouts[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} |
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
Oops, something went wrong.
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.