forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry pic fixes into release for tests. (#14673)
* Added FSWatching base class and made related changes (#14605) * Add FSWatching locator base class * Correct glob pattern to not match python3.2whoa * Add documentation of python binary watcher * Fix lint errors * Update ignore list * Add disposable registry * Modify FSWatching Locator * Code reviews * Use string[] * Remove list disposable getter * Fix failing global virtual env watcher tests (#14633) Co-authored-by: Kartik Raj <karraj@microsoft.com>
- Loading branch information
1 parent
2c2b7e1
commit 5eab81d
Showing
9 changed files
with
204 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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 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 { | ||
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 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 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 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 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.