Skip to content

Commit

Permalink
Two changes to try to prevent data migration changes to info.xml file…
Browse files Browse the repository at this point in the history
…s from affecting selection of version to use when merging HearThisPack files:

1) During data migration, preserve the original file modification time. Note: for most current HT users, the latest migration that modifies this file has probably already happened, so this is not likely to help much.
2) During the merge, omit info.xml from consideration when comparing file date/times. Currently there are only very rare situations (skipping) that this file will be changed independently of recording, so this is probably safe. However, this needs to be reviewed when the changes for HT-359 are merged because that will make it slightly more likely to change the info.xml file alone.
  • Loading branch information
tombogle committed Jan 4, 2022
1 parent c77950d commit 567d812
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
6 changes: 0 additions & 6 deletions src/HearThis/Communication/IAndroidLink.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace HearThis.Communication
{
/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/HearThis/Communication/WindowsLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ public bool TryListFiles(string androidPath, out string list)
var sb = new StringBuilder();
foreach (var file in Directory.EnumerateFiles(path, "*.*"))
{
sb.Append(Path.GetFileName(file));
var filename = Path.GetFileName(file);
// REVIEW: When merging this into HT-359, we'll need to consider
// whether/when changes to info.xml files might need to be regarded
// as significant for determining which version to use in merge, since
// it will be more common for the info file to change without any
// clips being modified.
if (filename == "info.xml")
continue;
sb.Append(filename);
sb.Append(";");
sb.Append(
new FileInfo(file).LastWriteTimeUtc.ToString(
Expand Down
6 changes: 3 additions & 3 deletions src/HearThis/Publishing/ClipRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,15 @@ public static bool ShiftClipsAtOrAfterBlockIfAllClipsAreBeforeDate(string projec
Func<ChapterRecordingInfoBase> getRecordingInfo)
{
var result = ShiftClips(projectName, bookName, chapterNumber1Based, iBlock, 1,
getRecordingInfo, cutoff:cutoff);
getRecordingInfo, cutoff:cutoff, preserveModifiedTime:true);
if (result.Error != null)
throw result.Error;
return result.Attempted == result.SuccessfulMoves;
}

private static ClipShiftingResult ShiftClips(string projectName, string bookName, int chapterNumber,
int iStartBlock, int offset, Func<ChapterRecordingInfoBase> getRecordingInfo,
int blockCount = MaxValue, DateTime cutoff = default)
int blockCount = MaxValue, DateTime cutoff = default, bool preserveModifiedTime = false)
{
Debug.Assert(offset != 0);
ClipShiftingResult result = null;
Expand Down Expand Up @@ -451,7 +451,7 @@ private static ClipShiftingResult ShiftClips(string projectName, string bookName

result.LastAttemptedMove = null;

getRecordingInfo().AdjustLineNumbers(iStartBlock, offset, blockCount);
getRecordingInfo().AdjustLineNumbers(iStartBlock, offset, blockCount, preserveModifiedTime);
}
catch (Exception e)
{
Expand Down
16 changes: 11 additions & 5 deletions src/HearThis/Script/ChapterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ public void RemoveRecordings()
Save();
}

protected override void Save()
{
Save(FilePath);
}
protected override void Save(bool preserveModifiedTime = false) => Save(_filePath, preserveModifiedTime);

private void Save(string filePath)
private void Save(string filePath, bool preserveModifiedTime = false)
{
var finfo = new FileInfo(filePath);
var modified = finfo.LastWriteTimeUtc;

if (!XmlSerializationHelper.SerializeToFile(filePath, this, out var error))
{
Logger.WriteError(error);
Expand All @@ -275,6 +275,12 @@ private void Save(string filePath)
// magically go away.
throw new Exception("Unable to save file: " + filePath, error);
}

if (preserveModifiedTime)
{
finfo.LastWriteTimeUtc = modified;
finfo.Attributes |= FileAttributes.Archive;
}
}

public string ToXmlString()
Expand Down
7 changes: 4 additions & 3 deletions src/HearThis/Script/ChapterRecordingInfoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public abstract class ChapterRecordingInfoBase
{
public abstract IReadOnlyList<ScriptLine> RecordingInfo { get; }

public void AdjustLineNumbers(int blockNumberOfStartingShiftedClip0Based, int shiftedBy, int blockCount = MaxValue)
public void AdjustLineNumbers(int blockNumberOfStartingShiftedClip0Based, int shiftedBy, int blockCount = MaxValue,
bool preserveModifiedTime = false)
{
// Note: ScriptLine.Number is 1-based, not 0-based
foreach (var recordingInfo in RecordingInfo
Expand All @@ -18,11 +19,11 @@ public void AdjustLineNumbers(int blockNumberOfStartingShiftedClip0Based, int sh
recordingInfo.Number += shiftedBy;
}

Save();
Save(preserveModifiedTime);
}

public abstract void OnScriptBlockRecorded(ScriptLine selectedScriptBlock);

protected abstract void Save();
protected abstract void Save(bool preserveModifiedTime = false);
}
}
10 changes: 7 additions & 3 deletions src/HearThisTests/ClipRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using HearThis.Script;
using NUnit.Framework;
using SIL.IO;
using static System.Int32;
using DateTime = System.DateTime;

namespace HearThisTests
Expand Down Expand Up @@ -1503,13 +1502,14 @@ public void ShiftClipsAtOrAfterBlockIfAllClipsAreBeforeDate_AllFilesModifiedBefo
const int kTestChapter = 1;

var chapterFolder = ClipRepository.GetChapterFolder(testProject, kTestBook, kTestChapter);
ChapterRecordingInfoBase info;
TestChapterInfo info;
if (includeClip0)
info = new TestChapterInfo(1, 2, 3, 8); // Intentionally omitted 4, just to make sure the logic is okay with having one missing.
else
info = new TestChapterInfo(2, 3, 8); // Intentionally omitted 4, just to make sure the logic is okay with having one missing.
info.RecordingInfo[1].SkippedChanged += sender => { }; // code requires us to have a handler before we can set it.
info.RecordingInfo[1].Skipped = true;
info.ExpectedPreserveModifiedTime = true;

try
{
Expand All @@ -1524,13 +1524,15 @@ public void ShiftClipsAtOrAfterBlockIfAllClipsAreBeforeDate_AllFilesModifiedBefo
// SUT
Assert.IsTrue(ClipRepository.ShiftClipsAtOrAfterBlockIfAllClipsAreBeforeDate(
testProject, kTestBook, kTestChapter, 1, DateTime.UtcNow, () => info));

Assert.AreEqual(includeClip0 ? 5 : 4, Directory.GetFiles(chapterFolder).Length);
Assert.That(File.Exists(Path.Combine(chapterFolder, "8.wav")));
Assert.That(File.Exists(Path.Combine(chapterFolder, "4.wav")));
Assert.That(File.Exists(Path.Combine(chapterFolder, "3.skip")));
Assert.That(File.Exists(Path.Combine(chapterFolder, "2.wav")));
Assert.IsFalse(File.Exists(Path.Combine(chapterFolder, "1.wav")));
Assert.AreEqual(includeClip0, File.Exists(file0));
Assert.AreEqual(1, info.SaveCallCount);

int i = 0;
if (includeClip0)
Expand Down Expand Up @@ -1838,6 +1840,7 @@ private class TestChapterInfo : ChapterRecordingInfoBase
private readonly List<ScriptLine> _recordings;

public int SaveCallCount { get; private set; }
public bool ExpectedPreserveModifiedTime { get; set; }

public TestChapterInfo(params int[] scriptLineNumbers)
{
Expand All @@ -1852,8 +1855,9 @@ public override void OnScriptBlockRecorded(ScriptLine selectedScriptBlock)
throw new NotImplementedException();
}

protected override void Save()
protected override void Save(bool preserveModifiedTime = false)
{
Assert.AreEqual(ExpectedPreserveModifiedTime, preserveModifiedTime);
SaveCallCount++;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/HearThisTests/ScriptProviderBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,9 @@ public override void OnScriptBlockRecorded(ScriptLine scriptBlock)
_recordings.Add(scriptBlock);
}

protected override void Save()
protected override void Save(bool preserveModifiedTime = false)
{
Assert.IsTrue(preserveModifiedTime);
SaveCallCount++;
}
}
Expand Down

0 comments on commit 567d812

Please sign in to comment.