forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added FSWatching base class and made related changes #14605
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
10 commits
Select commit
Hold shift + click to select a range
ad675c1
Add FSWatching locator base class
06e6f5b
Correct glob pattern to not match python3.2whoa
c167f57
Add documentation of python binary watcher
375df6b
Fix lint errors
3ef70e3
Update ignore list
5925ab1
Add disposable registry
31f7c54
Modify FSWatching Locator
fec8c1b
Code reviews
21b740a
Use string[]
2260b17
Remove list disposable getter
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { traceWarning } from './logger'; | ||
import { IDisposable } from './types'; | ||
|
||
/** | ||
* Responsible for disposing a list of disposables synchronusly. | ||
*/ | ||
export class DisposableRegistry implements IDisposable { | ||
karrtikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private _list: IDisposable[] = []; | ||
|
||
public dispose(): void { | ||
this._list.forEach((l, index) => { | ||
try { | ||
l.dispose(); | ||
} catch (err) { | ||
traceWarning(`dispose() #${index} failed (${err})`); | ||
} | ||
}); | ||
this._list = []; | ||
} | ||
|
||
public push(disposable?: IDisposable): void { | ||
if (disposable) { | ||
this._list.push(disposable); | ||
} | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
src/client/pythonEnvironments/base/locators/lowLevel/fsWatchingLocator.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,82 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { FileChangeType } from '../../../../common/platform/fileSystemWatcher'; | ||
import { sleep } from '../../../../common/utils/async'; | ||
import { watchLocationForPythonBinaries } from '../../../common/pythonBinariesWatcher'; | ||
import { PythonEnvKind } from '../../info'; | ||
import { Locator } from '../../locator'; | ||
|
||
/** | ||
* The base for Python envs locators who watch the file system. | ||
* Most low-level locators should be using this. | ||
* | ||
* Subclasses can call `this.emitter.fire()` * to emit events. | ||
*/ | ||
export abstract class FSWatchingLocator extends Locator { | ||
private initialized = false; | ||
|
||
constructor( | ||
/** | ||
* Location(s) to watch for python binaries. | ||
*/ | ||
private readonly getRoots: () => Promise<string[]> | string | string[], | ||
/** | ||
* Returns the kind of environment specific to locator given the path to exectuable. | ||
*/ | ||
private readonly getKind: (executable: string) => Promise<PythonEnvKind>, | ||
private readonly opts: { | ||
/** | ||
* Glob which represents basename of the executable to watch. | ||
*/ | ||
executableBaseGlob?: string, | ||
/** | ||
* Time to wait before handling an environment-created event. | ||
*/ | ||
delayOnCreated?: number, // milliseconds | ||
} = {}, | ||
) { | ||
super(); | ||
} | ||
|
||
public async initialize(): Promise<void> { | ||
if (this.initialized) { | ||
return; | ||
} | ||
this.initialized = true; | ||
await this.startWatchers(); | ||
} | ||
|
||
public dispose(): void { | ||
super.dispose(); | ||
this.initialized = false; | ||
} | ||
|
||
private async startWatchers(): Promise<void> { | ||
let roots = await this.getRoots(); | ||
if (typeof roots === 'string') { | ||
roots = [roots]; | ||
} | ||
roots.forEach((root) => this.startWatcher(root)); | ||
} | ||
|
||
private startWatcher(root: string): void { | ||
this.disposables.push( | ||
watchLocationForPythonBinaries( | ||
root, | ||
async (type: FileChangeType, executable: string) => { | ||
if (type === FileChangeType.Created) { | ||
if (this.opts.delayOnCreated !== undefined) { | ||
// Note detecting kind of env depends on the file structure around the | ||
// executable, so we need to wait before attempting to detect it. | ||
await sleep(this.opts.delayOnCreated); | ||
} | ||
} | ||
const kind = await this.getKind(executable); | ||
this.emitter.fire({ type, kind }); | ||
}, | ||
this.opts.executableBaseGlob, | ||
), | ||
); | ||
} | ||
} |
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.