-
Notifications
You must be signed in to change notification settings - Fork 30
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 file system watcher for dvc.yaml #1282
Merged
Merged
Changes from all commits
Commits
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 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,62 @@ | ||
import { Event, EventEmitter } from 'vscode' | ||
import { Disposable } from '@hediet/std/disposable' | ||
import { Deferred } from '@hediet/std/synchronization' | ||
import { isSameOrChild, loadYaml, PartialDvcYaml } from '..' | ||
import { findFiles } from '../workspace' | ||
import { join } from '../../test/util/path' | ||
import { createFileSystemWatcher } from '../watcher' | ||
|
||
export class FileSystemData { | ||
public readonly dispose = Disposable.fn() | ||
|
||
public readonly onDidUpdate: Event<{ path: string; yaml: PartialDvcYaml }> | ||
|
||
private readonly dvcRoot: string | ||
|
||
private readonly updated = this.dispose.track( | ||
new EventEmitter<{ path: string; yaml: PartialDvcYaml }>() | ||
) | ||
|
||
private readonly deferred = new Deferred() | ||
private readonly initialized = this.deferred.promise | ||
|
||
constructor(dvcRoot: string) { | ||
this.dvcRoot = dvcRoot | ||
this.onDidUpdate = this.updated.event | ||
|
||
this.watchDvcYaml() | ||
this.initialize() | ||
} | ||
|
||
public isReady() { | ||
return this.initialized | ||
} | ||
|
||
private async initialize() { | ||
const files = await findFiles(join('**', 'dvc.yaml')) | ||
const filesInRepo = files.filter(file => isSameOrChild(this.dvcRoot, file)) | ||
|
||
filesInRepo.map(path => { | ||
const yaml = loadYaml<PartialDvcYaml>(path) | ||
if (yaml) { | ||
this.updated.fire({ path, yaml }) | ||
} | ||
}) | ||
|
||
this.deferred.resolve() | ||
} | ||
|
||
private watchDvcYaml() { | ||
this.dispose.track( | ||
createFileSystemWatcher(join(this.dvcRoot, '**', 'dvc.yaml'), path => { | ||
if (!path) { | ||
return | ||
} | ||
const yaml = loadYaml<PartialDvcYaml>(path) | ||
if (yaml) { | ||
this.updated.fire({ path, yaml }) | ||
} | ||
}) | ||
) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { basename, extname, join, relative, resolve } from 'path' | ||
import { existsSync, lstatSync, readdir, removeSync } from 'fs-extra' | ||
import { | ||
existsSync, | ||
lstatSync, | ||
readdir, | ||
readFileSync, | ||
removeSync | ||
} from 'fs-extra' | ||
import { load } from 'js-yaml' | ||
import { Uri } from 'vscode' | ||
import { definedAndNonEmpty } from '../util/array' | ||
import { Logger } from '../common/logger' | ||
|
||
export const exists = (path: string): boolean => existsSync(path) | ||
|
||
|
@@ -59,6 +67,7 @@ export type PartialDvcYaml = { | |
train: { outs: (string | Record<string, { checkpoint?: boolean }>)[] } | ||
} | ||
} | ||
|
||
export const isAnyDvcYaml = (path?: string): boolean => | ||
!!( | ||
path && | ||
|
@@ -71,3 +80,11 @@ export const relativeWithUri = (dvcRoot: string, uri: Uri) => | |
relative(dvcRoot, uri.fsPath) | ||
|
||
export const removeDir = (path: string): void => removeSync(path) | ||
|
||
export const loadYaml = <T>(path: string): T | undefined => { | ||
try { | ||
return load(readFileSync(path, 'utf-8')) as T | ||
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. [F] Don't want everything to fall over if the file is corrupted. |
||
} catch { | ||
Logger.error(`failed to load yaml ${path}`) | ||
} | ||
} |
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,59 @@ | ||
import { afterEach, beforeEach, describe, it, suite } from 'mocha' | ||
import { stub, restore } from 'sinon' | ||
import { expect } from 'chai' | ||
import { Disposable } from '../../../../extension' | ||
import { FileSystemData } from '../../../../fileSystem/data' | ||
import { dvcDemoPath } from '../../../util' | ||
import * as FileSystem from '../../../../fileSystem' | ||
import * as Watcher from '../../../../fileSystem/watcher' | ||
import { getFirstArgOfCall } from '../../util' | ||
import { join } from '../../../util/path' | ||
|
||
suite('Experiments Data Test Suite', () => { | ||
const disposable = Disposable.fn() | ||
const mockWatcher = { | ||
dispose: stub() | ||
} as Disposable | ||
|
||
beforeEach(() => { | ||
restore() | ||
}) | ||
|
||
afterEach(() => { | ||
disposable.dispose() | ||
}) | ||
|
||
describe('FileSystemData', () => { | ||
it('should read the dvc.yaml from the demo path and send an event containing the path and the yaml', async () => { | ||
stub(Watcher, 'createFileSystemWatcher').returns(mockWatcher) | ||
const data = disposable.track(new FileSystemData(dvcDemoPath)) | ||
|
||
disposable.track( | ||
data.onDidUpdate(({ path, yaml }) => { | ||
expect(path).to.equal(dvcDemoPath) | ||
expect(yaml.stages.train.outs).to.deep.equal([ | ||
{ 'model.pt': { checkpoint: true } } | ||
]) | ||
}) | ||
) | ||
|
||
await data.isReady() | ||
}) | ||
|
||
it('should create a watcher with the expected glob', async () => { | ||
const mockCreateFileSystemWatcher = stub( | ||
Watcher, | ||
'createFileSystemWatcher' | ||
).returns(mockWatcher) | ||
stub(FileSystem, 'loadYaml').returns(undefined) | ||
const data = disposable.track(new FileSystemData(dvcDemoPath)) | ||
|
||
expect(mockCreateFileSystemWatcher).to.be.calledOnce | ||
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.equal( | ||
join(dvcDemoPath, '**', 'dvc.yaml') | ||
) | ||
|
||
await data.isReady() | ||
}) | ||
}) | ||
}) |
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
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.
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.
[F] We have to find all of the files in the workspace and then cut down (limitation of using
workspace
from VS Code.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.
It's not possible to use a
${dvcRoot}/**/dvc.yaml
pattern? Stranger things have happened, so I won't be too surprised if so.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.
I did test that, it didn't return anything.