-
Notifications
You must be signed in to change notification settings - Fork 300
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
Add API to consume new Python API (but no used) #8324
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { inject, injectable, named } from 'inversify'; | ||
import { SemVer } from 'semver'; | ||
import { Memento } from 'vscode'; | ||
import { IPythonApiProvider } from '../../api/types'; | ||
import { IFileSystem } from '../platform/types'; | ||
import { GLOBAL_MEMENTO, IMemento } from '../types'; | ||
import { createDeferredFromPromise } from '../utils/async'; | ||
|
||
const CACHEKEY_FOR_CONDA_INFO = 'CONDA_INFORMATION_CACHE'; | ||
|
||
@injectable() | ||
export class CondaService { | ||
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. Exposes the CondaVersion, CondaFile behind a class with caching & wraps the Python API call (possible its not available, etc) |
||
private _file?: string; | ||
private _version?: SemVer; | ||
constructor( | ||
@inject(IPythonApiProvider) private readonly pythonApi: IPythonApiProvider, | ||
@inject(IMemento) @named(GLOBAL_MEMENTO) private readonly globalState: Memento, | ||
@inject(IFileSystem) private readonly fs: IFileSystem | ||
) {} | ||
async getCondaVersion() { | ||
if (this._version) { | ||
return this._version; | ||
} | ||
const latestInfo = this.pythonApi | ||
.getApi() | ||
.then((api) => (api.getCondaVersion ? api.getCondaVersion() : undefined)); | ||
void latestInfo.then((version) => { | ||
this._version = version; | ||
void this.udpateCache(); | ||
}); | ||
const cachedInfo = createDeferredFromPromise(this.getCachedInformation()); | ||
await Promise.race([cachedInfo, latestInfo]); | ||
if (cachedInfo.completed && cachedInfo.value?.version) { | ||
return (this._version = cachedInfo.value.version); | ||
} | ||
return latestInfo; | ||
} | ||
async getCondaFile() { | ||
if (this._file) { | ||
return this._file; | ||
} | ||
const latestInfo = this.pythonApi.getApi().then((api) => (api.getCondaFile ? api.getCondaFile() : undefined)); | ||
void latestInfo.then((file) => { | ||
this._file = file; | ||
void this.udpateCache(); | ||
}); | ||
const cachedInfo = createDeferredFromPromise(this.getCachedInformation()); | ||
await Promise.race([cachedInfo, latestInfo]); | ||
if (cachedInfo.completed && cachedInfo.value?.file) { | ||
return (this._file = cachedInfo.value.file); | ||
} | ||
return latestInfo; | ||
} | ||
private async udpateCache() { | ||
if (!this._file || !this._version) { | ||
return; | ||
} | ||
const fileHash = await this.fs.getFileHash(this._file); | ||
await this.globalState.update(CACHEKEY_FOR_CONDA_INFO, { | ||
version: this._version.raw, | ||
file: this._file, | ||
fileHash | ||
}); | ||
} | ||
/** | ||
* If the last modified date of the conda file is the same as when we last checked, | ||
* then we can assume the version is the same. | ||
* Even if not, we'll update this with the latest information. | ||
*/ | ||
private async getCachedInformation(): Promise<{ version: SemVer; file: string } | undefined> { | ||
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. Comments added how & why we cache |
||
const cachedInfo = this.globalState.get<{ version: string; file: string; fileHash: string } | undefined>( | ||
CACHEKEY_FOR_CONDA_INFO, | ||
undefined | ||
); | ||
if (!cachedInfo) { | ||
return; | ||
} | ||
const fileHash = await this.fs.getFileHash(cachedInfo.file); | ||
if (cachedInfo.fileHash === fileHash) { | ||
return { | ||
version: new SemVer(cachedInfo.version), | ||
file: cachedInfo.file | ||
}; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New API exposed by python extension. Possible users don't have the latest python extenison hence we need to take that into account.