Skip to content

Commit

Permalink
Remove DatabaseDumpReader namespace and change class name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoltanar committed Aug 16, 2024
1 parent b004c98 commit 9eeb62f
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Happy Reader/View/Tabs/SettingsTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private async void UpdateVndbData(object sender, RoutedEventArgs e)
UpdateVndbButton.Content = $"{UpdateVndbText} (in progress)";
UpdateVndbButton.IsEnabled = false;
MultiLogger.PreviousLogTime = null;
var updateResult = await Happy_Apps_Core.DumpReader.Program.Execute(UpdateLoggingAction);
var updateResult = await Happy_Apps_Core.DumpReader.DumpReaderStarter.Execute(UpdateLoggingAction);
if (updateResult.Success)
{

Expand Down
14 changes: 7 additions & 7 deletions HappySearchObjectClasses/DumpReader/DumpReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public DumpReader(string dumpFolder, string currentDatabaseFilePath, int userId,
File.Copy(currentDatabaseFilePath, inProgressDbFile);
//DRB = dump reader backup
var backupPath = GetDatabaseFile(currentDbFile, "-DRB");
Program.PrintLogLine([$"Backing up database to {backupPath}"]);
DumpReaderStarter.PrintLogLine([$"Backing up database to {backupPath}"]);
File.Copy(currentDbFile.FullName, backupPath);
UserId = userId;
DumpFolder = dumpFolder;
Expand All @@ -65,8 +65,8 @@ private string GetDatabaseFile(FileInfo currentDatabaseFile, string suffix)
}
public void Run(DateTime dumpDate, int[] previousVnIds, int[] previousCharacterIds)
{
Program.PrintLogLine(["Starting Dump Reader..."]);
Program.PrintLogLine(["Loading Tag/Trait Dump files..."]);
DumpReaderStarter.PrintLogLine(["Starting Dump Reader..."]);
DumpReaderStarter.PrintLogLine(["Loading Tag/Trait Dump files..."]);
DumpFiles.Load();
var votesFilePath = FindVotesFile();
Load<ListedProducer>((i, t) => Database.Producers.Add(i, false, true, t), "db\\producers");
Expand Down Expand Up @@ -102,9 +102,9 @@ public void Run(DateTime dumpDate, int[] previousVnIds, int[] previousCharacterI
Database.VisualNovels.Add(i, false, true, t);
ResolveUserVnForVn(i, t);
}, "db\\vn");
Program.PrintLogLine([$"Added {newTitleCount} new titles."]);
DumpReaderStarter.PrintLogLine([$"Added {newTitleCount} new titles."]);
Database.SaveLatestDumpUpdate(dumpDate);
Program.PrintLogLine(["Completed."]);
DumpReaderStarter.PrintLogLine(["Completed."]);
}

private void LoadStaff()
Expand Down Expand Up @@ -266,7 +266,7 @@ private void LoadAndResolveTags()
if (i.Ignore) return;
VnTags.Add(i);
}, "db\\tags_vn");
Program.PrintLogLine(["Resolving Tags..."]);
DumpReaderStarter.PrintLogLine(["Resolving Tags..."]);
var groupedTags = VnTags.GroupBy(t => (t.TagId, t.VnId)).ToArray();
WrapInTransaction(trans =>
{
Expand All @@ -289,7 +289,7 @@ private void LoadAndResolveTags()

private void Load<T>(Action<T, SQLiteTransaction> addToList, string filePath, bool useHeaderFile = true) where T : DumpItem, new()
{
Program.PrintLogLine([$"Loading for {typeof(T).Name}..."]);
DumpReaderStarter.PrintLogLine([$"Loading for {typeof(T).Name}..."]);
new T().SetDumpHeaders((useHeaderFile
? File.ReadAllLines(Path.Combine(DumpFolder, filePath + ".header")).Single()
: string.Empty).Split('\t'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DatabaseDumpReader;

namespace Happy_Apps_Core.DumpReader;

public static class Program
public static class DumpReaderStarter
{
private static readonly string DumpFolder = Path.Combine(StaticHelpers.AppDataFolder, "Database Dumps");
private const string LatestDbDumpUrl = "https://dl.vndb.org/dump/vndb-db-latest.tar.zst";
Expand Down
2 changes: 1 addition & 1 deletion HappySearchObjectClasses/Happy_Apps_Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<Compile Include="DumpReader\DumpVote.cs" />
<Compile Include="DumpReader\LengthVote.cs" />
<Compile Include="DumpReader\ProducerRelease.cs" />
<Compile Include="DumpReader\Program.cs" />
<Compile Include="DumpReader\DumpReaderStarter.cs" />
<Compile Include="DumpReader\UserLabel.cs" />
<Compile Include="DumpReader\UserVn.cs" />
<Compile Include="DumpReader\VnTag.cs" />
Expand Down
270 changes: 134 additions & 136 deletions HappySearchObjectClasses/RSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,145 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Happy_Apps_Core;

namespace DatabaseDumpReader
namespace Happy_Apps_Core;

internal static class RSync
{
internal static class RSync
{
private const string RsyncExecutableName = @"rsync.exe";
private const string Switches = @"-rptv --del";
private const string RsyncExecutableName = @"rsync.exe";
private const string Switches = @"-rptv --del";

public static bool Run(string rsyncUrl, string destinationFolderPath, out string errorMessage)
{
var filesCopied = new List<string>();
try
{
if (!ValidatePathsAndFiles(destinationFolderPath, out errorMessage, out var originalRsyncFile, out var destinationFolder)) return false;
if (!CopyRSyncFiles(ref errorMessage, destinationFolder, originalRsyncFile, filesCopied)) return false;
Debug.Assert(destinationFolder.Parent != null, "destinationFolder.Parent != null");
var psi = new ProcessStartInfo("cmd.exe")
{
Arguments = $"/C \"\"rsync\" {Switches} \"{rsyncUrl}\" \"{destinationFolder.Name}\"\"",
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = destinationFolder.Parent.FullName
};
StaticHelpers.Logger.ToFile("Starting Rsync and waiting for completion...");
var process = Process.Start(psi);
if (process == null)
{
errorMessage = "Failed to start rsync";
return false;
}
if (process.HasExited)
{
errorMessage = "rsync process exited immediately.";
return false;
}
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_OutputDataReceived;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
errorMessage = "No errors.";
return true;
}
catch (Exception ex)
{
errorMessage = ex.ToString();
return false;
}
finally
{
DeleteFiles(filesCopied);
}
}
public static bool Run(string rsyncUrl, string destinationFolderPath, out string errorMessage)
{
var filesCopied = new List<string>();
try
{
if (!ValidatePathsAndFiles(destinationFolderPath, out errorMessage, out var originalRsyncFile, out var destinationFolder)) return false;
if (!CopyRSyncFiles(ref errorMessage, destinationFolder, originalRsyncFile, filesCopied)) return false;
Debug.Assert(destinationFolder.Parent != null, "destinationFolder.Parent != null");
var psi = new ProcessStartInfo("cmd.exe")
{
Arguments = $"/C \"\"rsync\" {Switches} \"{rsyncUrl}\" \"{destinationFolder.Name}\"\"",
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = destinationFolder.Parent.FullName
};
StaticHelpers.Logger.ToFile("Starting Rsync and waiting for completion...");
var process = Process.Start(psi);
if (process == null)
{
errorMessage = "Failed to start rsync";
return false;
}
if (process.HasExited)
{
errorMessage = "rsync process exited immediately.";
return false;
}
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_OutputDataReceived;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
errorMessage = "No errors.";
return true;
}
catch (Exception ex)
{
errorMessage = ex.ToString();
return false;
}
finally
{
DeleteFiles(filesCopied);
}
}

private static bool CopyRSyncFiles(
ref string errorMessage,
DirectoryInfo destinationFolder,
FileInfo originalRsyncFile,
ICollection<string> filesCopied)
{
try
{
var rsyncDirectory = destinationFolder.Parent;
// ReSharper disable once PossibleNullReferenceException
foreach (var file in originalRsyncFile.Directory.GetFiles())
{
// ReSharper disable once PossibleNullReferenceException
var copyDestination = Path.Combine(rsyncDirectory.FullName, file.Name);
if (File.Exists(copyDestination)) continue;
file.CopyTo(copyDestination);
filesCopied.Add(copyDestination);
}
return true;
}
catch (Exception ex)
{
errorMessage = $"Failed to temporarily copy rsync files to destination path: {ex}";
return false;
}
}
private static bool CopyRSyncFiles(
ref string errorMessage,
DirectoryInfo destinationFolder,
FileInfo originalRsyncFile,
ICollection<string> filesCopied)
{
try
{
var rsyncDirectory = destinationFolder.Parent;
// ReSharper disable once PossibleNullReferenceException
foreach (var file in originalRsyncFile.Directory.GetFiles())
{
// ReSharper disable once PossibleNullReferenceException
var copyDestination = Path.Combine(rsyncDirectory.FullName, file.Name);
if (File.Exists(copyDestination)) continue;
file.CopyTo(copyDestination);
filesCopied.Add(copyDestination);
}
return true;
}
catch (Exception ex)
{
errorMessage = $"Failed to temporarily copy rsync files to destination path: {ex}";
return false;
}
}

private static bool ValidatePathsAndFiles(
string destinationFolderPath,
out string errorMessage,
out FileInfo originalRsyncFile,
out DirectoryInfo destinationFolder)
{
var assemblyDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
// ReSharper disable once PossibleNullReferenceException
originalRsyncFile = new FileInfo(Path.Combine(assemblyDirectory.FullName, "Rsync", RsyncExecutableName));
destinationFolder = new DirectoryInfo(Path.GetFullPath(destinationFolderPath));
if (destinationFolder.Parent == null)
{
errorMessage = "Destination folder cannot be a drive (or root path).";
return false;
}
if (!destinationFolder.Exists)
{
try
{
destinationFolder.Create();
}
catch (IOException ex)
{
errorMessage = $"Failed to create destination folder '{destinationFolder.FullName}': {ex}";
return false;
}
}
if (!originalRsyncFile.Exists)
{
errorMessage = $"rsync executable not found: {originalRsyncFile}";
return false;
}
private static bool ValidatePathsAndFiles(
string destinationFolderPath,
out string errorMessage,
out FileInfo originalRsyncFile,
out DirectoryInfo destinationFolder)
{
var assemblyDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
// ReSharper disable once PossibleNullReferenceException
originalRsyncFile = new FileInfo(Path.Combine(assemblyDirectory.FullName, "Rsync", RsyncExecutableName));
destinationFolder = new DirectoryInfo(Path.GetFullPath(destinationFolderPath));
if (destinationFolder.Parent == null)
{
errorMessage = "Destination folder cannot be a drive (or root path).";
return false;
}
if (!destinationFolder.Exists)
{
try
{
destinationFolder.Create();
}
catch (IOException ex)
{
errorMessage = $"Failed to create destination folder '{destinationFolder.FullName}': {ex}";
return false;
}
}
if (!originalRsyncFile.Exists)
{
errorMessage = $"rsync executable not found: {originalRsyncFile}";
return false;
}

errorMessage = "No errors.";
return true;
}
errorMessage = "No errors.";
return true;
}

private static void DeleteFiles(IEnumerable<string> filesCopied)
{
foreach (var file in filesCopied)
{
try
{
new FileInfo(file).Delete();
}
catch (Exception ex)
{
StaticHelpers.Logger.ToFile($"Failed to delete file '{file}': {ex}");
}
}
}
private static void DeleteFiles(IEnumerable<string> filesCopied)
{
foreach (var file in filesCopied)
{
try
{
new FileInfo(file).Delete();
}
catch (Exception ex)
{
StaticHelpers.Logger.ToFile($"Failed to delete file '{file}': {ex}");
}
}
}

private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrEmpty(e.Data)) return;
Debug.WriteLine(e.Data);
}
}
}
private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrEmpty(e.Data)) return;
Debug.WriteLine(e.Data);
}
}
2 changes: 1 addition & 1 deletion IthVnrSharpLib

0 comments on commit 9eeb62f

Please sign in to comment.