forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBaseGitPackage.cs
More file actions
799 lines (699 loc) · 27.4 KB
/
Copy pathBaseGitPackage.cs
File metadata and controls
799 lines (699 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression;
using NLog;
using Octokit;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Helper.Cache;
using StabilityMatrix.Core.Models.Database;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.Progress;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Python;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Core.Models.Packages;
/// <summary>
/// Base class for packages that are hosted on Github.
/// Author and Name should be the Github username and repository name respectively.
/// </summary>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "VirtualMemberNeverOverridden.Global")]
public abstract class BaseGitPackage : BasePackage
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
protected readonly IGithubApiCache GithubApi;
protected readonly IDownloadService DownloadService;
protected readonly IPrerequisiteHelper PrerequisiteHelper;
public PyVenvRunner? VenvRunner;
public virtual string RepositoryName => Name;
public virtual string RepositoryAuthor => Author;
/// <summary>
/// URL of the hosted web page on launch
/// </summary>
protected string WebUrl = string.Empty;
public override string GithubUrl => $"https://github.com/{RepositoryAuthor}/{RepositoryName}";
public string DownloadLocation => Path.Combine(SettingsManager.LibraryDir, "Packages", $"{Name}.zip");
protected string GetDownloadUrl(DownloadPackageVersionOptions versionOptions)
{
if (!string.IsNullOrWhiteSpace(versionOptions.CommitHash))
{
return $"https://github.com/{RepositoryAuthor}/{RepositoryName}/archive/{versionOptions.CommitHash}.zip";
}
if (!string.IsNullOrWhiteSpace(versionOptions.VersionTag))
{
return $"https://api.github.com/repos/{RepositoryAuthor}/{RepositoryName}/zipball/{versionOptions.VersionTag}";
}
if (!string.IsNullOrWhiteSpace(versionOptions.BranchName))
{
return $"https://api.github.com/repos/{RepositoryAuthor}/{RepositoryName}/zipball/{versionOptions.BranchName}";
}
throw new Exception("No download URL available");
}
protected BaseGitPackage(
IGithubApiCache githubApi,
ISettingsManager settingsManager,
IDownloadService downloadService,
IPrerequisiteHelper prerequisiteHelper
)
: base(settingsManager)
{
GithubApi = githubApi;
DownloadService = downloadService;
PrerequisiteHelper = prerequisiteHelper;
}
public override async Task<DownloadPackageVersionOptions?> GetLatestVersion(
bool includePrerelease = false
)
{
if (ShouldIgnoreReleases)
{
var commits = await GithubApi
.GetAllCommits(RepositoryAuthor, RepositoryName, MainBranch)
.ConfigureAwait(false);
return new DownloadPackageVersionOptions
{
IsLatest = true,
IsPrerelease = false,
BranchName = MainBranch,
CommitHash = commits?.FirstOrDefault()?.Sha
};
}
var releases = await GithubApi.GetAllReleases(RepositoryAuthor, RepositoryName).ConfigureAwait(false);
var releaseList = releases.ToList();
if (releaseList.Count == 0)
{
return new DownloadPackageVersionOptions
{
IsLatest = true,
IsPrerelease = false,
BranchName = MainBranch
};
}
var latestRelease = includePrerelease ? releaseList.First() : releaseList.First(x => !x.Prerelease);
return new DownloadPackageVersionOptions
{
IsLatest = true,
IsPrerelease = latestRelease.Prerelease,
VersionTag = latestRelease.TagName!
};
}
public override Task<IEnumerable<GitCommit>?> GetAllCommits(string branch, int page = 1, int perPage = 10)
{
return GithubApi.GetAllCommits(RepositoryAuthor, RepositoryName, branch, page, perPage);
}
public override async Task<PackageVersionOptions> GetAllVersionOptions()
{
var packageVersionOptions = new PackageVersionOptions();
if (!ShouldIgnoreReleases)
{
var allReleases = await GithubApi
.GetAllReleases(RepositoryAuthor, RepositoryName)
.ConfigureAwait(false);
var releasesList = allReleases.ToList();
if (releasesList.Any())
{
packageVersionOptions.AvailableVersions = releasesList.Select(
r =>
new PackageVersion
{
TagName = r.TagName!,
ReleaseNotesMarkdown = r.Body,
IsPrerelease = r.Prerelease
}
);
}
}
// Branch mode
var allBranches = await GithubApi
.GetAllBranches(RepositoryAuthor, RepositoryName)
.ConfigureAwait(false);
packageVersionOptions.AvailableBranches = allBranches.Select(
b => new PackageVersion { TagName = $"{b.Name}", ReleaseNotesMarkdown = string.Empty }
);
return packageVersionOptions;
}
/// <summary>
/// Setup the virtual environment for the package.
/// </summary>
[MemberNotNull(nameof(VenvRunner))]
public async Task<PyVenvRunner> SetupVenv(
string installedPackagePath,
string venvName = "venv",
bool forceRecreate = false,
Action<ProcessOutput>? onConsoleOutput = null
)
{
if (Interlocked.Exchange(ref VenvRunner, null) is { } oldRunner)
{
await oldRunner.DisposeAsync().ConfigureAwait(false);
}
var venvRunner = await SetupVenvPure(installedPackagePath, venvName, forceRecreate, onConsoleOutput)
.ConfigureAwait(false);
if (Interlocked.Exchange(ref VenvRunner, venvRunner) is { } oldRunner2)
{
await oldRunner2.DisposeAsync().ConfigureAwait(false);
}
Debug.Assert(VenvRunner != null, "VenvRunner != null");
return venvRunner;
}
/// <summary>
/// Like <see cref="SetupVenv"/>, but does not set the <see cref="VenvRunner"/> property.
/// Returns a new <see cref="PyVenvRunner"/> instance.
/// </summary>
public async Task<PyVenvRunner> SetupVenvPure(
string installedPackagePath,
string venvName = "venv",
bool forceRecreate = false,
Action<ProcessOutput>? onConsoleOutput = null
)
{
var venvRunner = await PyBaseInstall
.Default.CreateVenvRunnerAsync(
Path.Combine(installedPackagePath, venvName),
workingDirectory: installedPackagePath,
environmentVariables: SettingsManager.Settings.EnvironmentVariables,
withDefaultTclTkEnv: Compat.IsWindows,
withQueriedTclTkEnv: Compat.IsUnix
)
.ConfigureAwait(false);
if (forceRecreate || !venvRunner.Exists())
{
await venvRunner.Setup(true, onConsoleOutput).ConfigureAwait(false);
}
if (!Compat.IsWindows)
return venvRunner;
try
{
await PrerequisiteHelper.AddMissingLibsToVenv(installedPackagePath).ConfigureAwait(false);
}
catch (Exception e)
{
Logger.Warn(e, "Failed to add missing libs to venv");
}
return venvRunner;
}
public override async Task<IEnumerable<Release>> GetReleaseTags()
{
var allReleases = await GithubApi
.GetAllReleases(RepositoryAuthor, RepositoryName)
.ConfigureAwait(false);
return allReleases;
}
public override async Task DownloadPackage(
string installLocation,
DownloadPackageOptions options,
IProgress<ProgressReport>? progress = null,
CancellationToken cancellationToken = default
)
{
var versionOptions = options.VersionOptions;
const long fiveGigs = 5 * SystemInfo.Gibibyte;
if (SystemInfo.GetDiskFreeSpaceBytes(installLocation) is < fiveGigs)
{
throw new ApplicationException(
$"Not enough space to download {Name} to {installLocation}, need at least 5GB"
);
}
await PrerequisiteHelper
.RunGit(
new[]
{
"clone",
"--branch",
!string.IsNullOrWhiteSpace(versionOptions.VersionTag)
? versionOptions.VersionTag
: versionOptions.BranchName ?? MainBranch,
GithubUrl,
installLocation
},
progress?.AsProcessOutputHandler()
)
.ConfigureAwait(false);
if (!versionOptions.IsLatest && !string.IsNullOrWhiteSpace(versionOptions.CommitHash))
{
await PrerequisiteHelper
.RunGit(
new[] { "checkout", versionOptions.CommitHash },
progress?.AsProcessOutputHandler(),
installLocation
)
.ConfigureAwait(false);
}
progress?.Report(new ProgressReport(100, message: "Download Complete"));
}
protected Task UnzipPackage(string installLocation, IProgress<ProgressReport>? progress = null)
{
using var zip = ZipFile.OpenRead(DownloadLocation);
var zipDirName = string.Empty;
var totalEntries = zip.Entries.Count;
var currentEntry = 0;
foreach (var entry in zip.Entries)
{
currentEntry++;
if (string.IsNullOrWhiteSpace(entry.Name) && entry.FullName.EndsWith("/"))
{
if (string.IsNullOrWhiteSpace(zipDirName))
{
zipDirName = entry.FullName;
}
var folderPath = Path.Combine(
installLocation,
entry.FullName.Replace(zipDirName, string.Empty)
);
Directory.CreateDirectory(folderPath);
continue;
}
var destinationPath = Path.GetFullPath(
Path.Combine(installLocation, entry.FullName.Replace(zipDirName, string.Empty))
);
entry.ExtractToFile(destinationPath, true);
progress?.Report(
new ProgressReport(
current: Convert.ToUInt64(currentEntry),
total: Convert.ToUInt64(totalEntries)
)
);
}
return Task.CompletedTask;
}
public override async Task<bool> CheckForUpdates(InstalledPackage package)
{
var currentVersion = package.Version;
if (currentVersion is null or { InstalledReleaseVersion: null, InstalledBranch: null })
{
Logger.Warn(
"Could not check updates for package {Name}, version is invalid: {@currentVersion}",
Name,
currentVersion
);
return false;
}
try
{
if (currentVersion.IsReleaseMode)
{
var latestVersion = await GetLatestVersion(currentVersion.IsPrerelease).ConfigureAwait(false);
UpdateAvailable = latestVersion.VersionTag != currentVersion.InstalledReleaseVersion;
return UpdateAvailable;
}
var allCommits = (
await GetAllCommits(currentVersion.InstalledBranch!).ConfigureAwait(false)
)?.ToList();
if (allCommits == null || allCommits.Count == 0)
{
Logger.Warn("No commits found for {Package}", package.PackageName);
return false;
}
var latestCommitHash = allCommits.First().Sha;
return latestCommitHash != currentVersion.InstalledCommitSha;
}
catch (ApiException e)
{
Logger.Warn(e, "Failed to check for package updates");
return false;
}
}
public override async Task<DownloadPackageVersionOptions?> GetUpdate(InstalledPackage installedPackage)
{
var currentVersion = installedPackage.Version;
if (currentVersion is null or { InstalledReleaseVersion: null, InstalledBranch: null })
{
Logger.Warn(
"Could not check updates for package {Name}, version is invalid: {@currentVersion}",
Name,
currentVersion
);
return null;
}
var versionOptions = new DownloadPackageVersionOptions { IsLatest = true };
try
{
if (currentVersion.IsReleaseMode)
{
var latestVersion = await GetLatestVersion(currentVersion.IsPrerelease).ConfigureAwait(false);
versionOptions.IsPrerelease = latestVersion.IsPrerelease;
versionOptions.VersionTag = latestVersion.VersionTag;
return versionOptions;
}
var allCommits = (
await GetAllCommits(currentVersion.InstalledBranch!).ConfigureAwait(false)
)?.ToList();
if (allCommits == null || allCommits.Count == 0)
{
Logger.Warn("No commits found for {Package}", installedPackage.PackageName);
return null;
}
var latestCommitHash = allCommits.First().Sha;
versionOptions.CommitHash = latestCommitHash;
versionOptions.BranchName = currentVersion.InstalledBranch;
return versionOptions;
}
catch (ApiException e)
{
Logger.Warn(e, "Failed to check for package updates");
return null;
}
}
public override async Task<InstalledPackageVersion> Update(
string installLocation,
InstalledPackage installedPackage,
UpdatePackageOptions options,
IProgress<ProgressReport>? progress = null,
Action<ProcessOutput>? onConsoleOutput = null,
CancellationToken cancellationToken = default
)
{
if (installedPackage.Version == null)
throw new NullReferenceException("Version is null");
if (!Directory.Exists(Path.Combine(installedPackage.FullPath!, ".git")))
{
Logger.Info("not a git repo, initializing...");
progress?.Report(new ProgressReport(-1f, "Initializing git repo", isIndeterminate: true));
await PrerequisiteHelper
.RunGit("init", onConsoleOutput, installedPackage.FullPath)
.ConfigureAwait(false);
await PrerequisiteHelper
.RunGit(
new[] { "remote", "add", "origin", GithubUrl },
onConsoleOutput,
installedPackage.FullPath
)
.ConfigureAwait(false);
}
var sharedFolderMethodToUse =
installedPackage.PreferredSharedFolderMethod ?? RecommendedSharedFolderMethod;
// Temporarily remove symlinks if using Symlink method
if (sharedFolderMethodToUse == SharedFolderMethod.Symlink)
{
if (SharedFolders is not null)
{
Helper.SharedFolders.RemoveLinksForPackage(
SharedFolders,
new DirectoryPath(installedPackage.FullPath!)
);
}
if (SharedOutputFolders is not null && installedPackage.UseSharedOutputFolder)
{
Helper.SharedFolders.RemoveLinksForPackage(
SharedOutputFolders,
new DirectoryPath(installedPackage.FullPath!)
);
}
}
var versionOptions = options.VersionOptions;
if (!string.IsNullOrWhiteSpace(versionOptions.VersionTag))
{
progress?.Report(new ProgressReport(-1f, "Fetching tags...", isIndeterminate: true));
await PrerequisiteHelper
.RunGit(new[] { "fetch", "--tags", "--force" }, onConsoleOutput, installedPackage.FullPath)
.ConfigureAwait(false);
progress?.Report(
new ProgressReport(-1f, $"Checking out {versionOptions.VersionTag}", isIndeterminate: true)
);
await PrerequisiteHelper
.RunGit(
new[] { "checkout", versionOptions.VersionTag, "--force" },
onConsoleOutput,
installedPackage.FullPath
)
.ConfigureAwait(false);
await InstallPackage(
installLocation,
installedPackage,
options.AsInstallOptions(),
progress,
onConsoleOutput,
cancellationToken
)
.ConfigureAwait(false);
return new InstalledPackageVersion
{
InstalledReleaseVersion = versionOptions.VersionTag,
IsPrerelease = versionOptions.IsPrerelease
};
}
// fetch
progress?.Report(new ProgressReport(-1f, "Fetching data...", isIndeterminate: true));
await PrerequisiteHelper
.RunGit(new[] { "fetch", "--force" }, onConsoleOutput, installedPackage.FullPath)
.ConfigureAwait(false);
if (versionOptions.IsLatest)
{
// checkout
progress?.Report(
new ProgressReport(
-1f,
$"Checking out {installedPackage.Version.InstalledBranch}...",
isIndeterminate: true
)
);
await PrerequisiteHelper
.RunGit(
new[] { "checkout", versionOptions.BranchName!, "--force" },
onConsoleOutput,
installedPackage.FullPath
)
.ConfigureAwait(false);
// pull
progress?.Report(new ProgressReport(-1f, "Pulling changes...", isIndeterminate: true));
await PrerequisiteHelper
.RunGit(
new[] { "pull", "--autostash", "origin", installedPackage.Version.InstalledBranch! },
onConsoleOutput,
installedPackage.FullPath!
)
.ConfigureAwait(false);
}
else
{
// checkout
progress?.Report(
new ProgressReport(
-1f,
$"Checking out {installedPackage.Version.InstalledBranch}...",
isIndeterminate: true
)
);
await PrerequisiteHelper
.RunGit(
new[] { "checkout", versionOptions.CommitHash!, "--force" },
onConsoleOutput,
installedPackage.FullPath
)
.ConfigureAwait(false);
}
await InstallPackage(
installLocation,
installedPackage,
options.AsInstallOptions(),
progress,
onConsoleOutput,
cancellationToken
)
.ConfigureAwait(false);
return new InstalledPackageVersion
{
InstalledBranch = versionOptions.BranchName,
InstalledCommitSha = versionOptions.CommitHash,
IsPrerelease = versionOptions.IsPrerelease
};
}
private async Task FixInfinityFolders(DirectoryPath rootDirectory, string infinityFolderName)
{
// Skip if first infinity not found
if (
rootDirectory.JoinDir(infinityFolderName)
is not { Exists: true, IsSymbolicLink: false } firstInfinity
)
{
return;
}
var depth = 0;
var currentDir = rootDirectory;
while (currentDir.JoinDir(infinityFolderName) is { Exists: true, IsSymbolicLink: false } newInfinity)
{
depth++;
currentDir = newInfinity;
}
Logger.Info("Found {Depth} infinity folders from {FirstPath}", depth, firstInfinity.ToString());
// Move all items in infinity folder to root
Logger.Info("Moving infinity folders content to root: {Path}", currentDir.ToString());
await FileTransfers.MoveAllFilesAndDirectories(currentDir, rootDirectory).ConfigureAwait(false);
// Move any files from first infinity by enumeration just in case
foreach (var file in firstInfinity.EnumerateFiles())
{
await file.MoveToDirectoryAsync(rootDirectory).ConfigureAwait(false);
}
// Delete infinity folders chain from first
Logger.Info("Deleting infinity folders: {Path}", currentDir.ToString());
await firstInfinity.DeleteAsync(true).ConfigureAwait(false);
}
private async Task FixForgeInfinity()
{
var modelsDir = new DirectoryPath(SettingsManager.ModelsDirectory);
var rootDirectory = modelsDir.JoinDir("StableDiffusion").JoinDir("sd");
var infinityFolderName = "sd";
var firstInfinity = rootDirectory.JoinDir(infinityFolderName);
var depth = 0;
var currentDir = rootDirectory;
while (currentDir.JoinDir(infinityFolderName) is { Exists: true, IsSymbolicLink: false } newInfinity)
{
depth++;
currentDir = newInfinity;
}
if (depth <= 5)
{
Logger.Info("not really that infinity, aborting");
return;
}
Logger.Info("Found {Depth} infinity folders from {FirstPath}", depth, firstInfinity.ToString());
// Move all items in infinity folder to root
Logger.Info("Moving infinity folders content to root: {Path}", currentDir.ToString());
await FileTransfers
.MoveAllFilesAndDirectories(currentDir, rootDirectory, overwriteIfHashMatches: true)
.ConfigureAwait(false);
// Move any files from first infinity by enumeration just in case
var leftoverFiles = firstInfinity.EnumerateFiles(searchOption: SearchOption.AllDirectories);
foreach (var file in leftoverFiles)
{
await file.MoveToWithIncrementAsync(rootDirectory.JoinFile(file.Name)).ConfigureAwait(false);
}
if (!firstInfinity.EnumerateFiles(searchOption: SearchOption.AllDirectories).Any())
{
// Delete infinity folders chain from first
Logger.Info("Deleting infinity folders: {Path}", currentDir.ToString());
await firstInfinity.DeleteAsync(true).ConfigureAwait(false);
}
}
public override async Task SetupModelFolders(
DirectoryPath installDirectory,
SharedFolderMethod sharedFolderMethod
)
{
if (sharedFolderMethod is SharedFolderMethod.Configuration && SharedFolderLayout is not null)
{
await SharedFoldersConfigHelper
.UpdateConfigFileForSharedAsync(
SharedFolderLayout,
installDirectory.FullPath,
SettingsManager.ModelsDirectory
)
.ConfigureAwait(false);
}
else if (sharedFolderMethod is SharedFolderMethod.Symlink && SharedFolders is { } sharedFolders)
{
var modelsDir = new DirectoryPath(SettingsManager.ModelsDirectory);
// fix infinity controlnet folders
await FixInfinityFolders(modelsDir.JoinDir("ControlNet"), "ControlNet").ConfigureAwait(false);
await FixForgeInfinity().ConfigureAwait(false);
// fix duplicate links in models dir
// see https://github.com/LykosAI/StabilityMatrix/issues/338
string[] duplicatePaths =
[
Path.Combine("ControlNet", "ControlNet"),
Path.Combine("IPAdapter", "base"),
Path.Combine("IPAdapter", "sd15"),
Path.Combine("IPAdapter", "sdxl")
];
foreach (var duplicatePath in duplicatePaths)
{
var linkDir = modelsDir.JoinDir(duplicatePath);
if (!linkDir.IsSymbolicLink)
continue;
Logger.Info("Removing duplicate junction at {Path}", linkDir.ToString());
await linkDir.DeleteAsync(false).ConfigureAwait(false);
}
await Helper
.SharedFolders.UpdateLinksForPackage(
sharedFolders,
SettingsManager.ModelsDirectory,
installDirectory
)
.ConfigureAwait(false);
}
}
public override Task UpdateModelFolders(
DirectoryPath installDirectory,
SharedFolderMethod sharedFolderMethod
) => SetupModelFolders(installDirectory, sharedFolderMethod);
public override Task RemoveModelFolderLinks(
DirectoryPath installDirectory,
SharedFolderMethod sharedFolderMethod
)
{
// Auto handling for SharedFolderLayout
if (sharedFolderMethod is SharedFolderMethod.Configuration && SharedFolderLayout is not null)
{
return SharedFoldersConfigHelper.UpdateConfigFileForDefaultAsync(
SharedFolderLayout,
installDirectory.FullPath
);
}
if (SharedFolders is not null && sharedFolderMethod is SharedFolderMethod.Symlink)
{
Helper.SharedFolders.RemoveLinksForPackage(SharedFolders, installDirectory);
}
return Task.CompletedTask;
}
public override Task SetupOutputFolderLinks(DirectoryPath installDirectory)
{
if (SharedOutputFolders is { } sharedOutputFolders)
{
return Helper.SharedFolders.UpdateLinksForPackage(
sharedOutputFolders,
SettingsManager.ImagesDirectory,
installDirectory,
recursiveDelete: true
);
}
return Task.CompletedTask;
}
public override Task RemoveOutputFolderLinks(DirectoryPath installDirectory)
{
if (SharedOutputFolders is { } sharedOutputFolders)
{
Helper.SharedFolders.RemoveLinksForPackage(sharedOutputFolders, installDirectory);
}
return Task.CompletedTask;
}
// Send input to the running process.
public virtual void SendInput(string input)
{
var process = VenvRunner?.Process;
if (process == null)
{
Logger.Warn("No process running for {Name}", Name);
return;
}
process.StandardInput.WriteLine(input);
}
public virtual async Task SendInputAsync(string input)
{
var process = VenvRunner?.Process;
if (process == null)
{
Logger.Warn("No process running for {Name}", Name);
return;
}
await process.StandardInput.WriteLineAsync(input).ConfigureAwait(false);
}
/// <inheritdoc />
public override void Shutdown()
{
if (VenvRunner is not null)
{
VenvRunner.Dispose();
VenvRunner = null;
}
}
/// <inheritdoc />
public override async Task WaitForShutdown()
{
if (VenvRunner is not null)
{
await VenvRunner.DisposeAsync().ConfigureAwait(false);
VenvRunner = null;
}
}
}