mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Add handler for only new files parameter
This commit is contained in:
@@ -39,13 +39,14 @@ public class CommandRunner implements Runnable {
|
||||
System.out.println("Safemode active. No files will be changed!");
|
||||
}
|
||||
|
||||
FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate());
|
||||
LastExecutionHandler lastExecutionHandler = config.isOnlyNewFiles() ? new LastExecutionHandler("./last-executions.yml") : null;
|
||||
FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate(), lastExecutionHandler);
|
||||
FileProcessor fileProcessor = new CachedFileProcessor(new MkvFileProcessor(config.getMkvToolNix(), fileFilter));
|
||||
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(config.getPreferredSubtitles().toArray(new String[0]), config.getForcedKeywords(), config.getCommentaryKeywords(), config.getHearingImpaired());
|
||||
|
||||
AttributeUpdater kernel = config.getCoherent() != null
|
||||
? new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor)
|
||||
: new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor);
|
||||
? new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler)
|
||||
: new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
|
||||
kernel.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -21,6 +19,7 @@ public class FileFilter {
|
||||
private final Set<String> excluded;
|
||||
private final Pattern includePattern;
|
||||
private final Date filterDate;
|
||||
private final LastExecutionHandler lastExecutionHandler;
|
||||
|
||||
private final String EXTENSION_GROUP = "extension";
|
||||
private final Pattern extensionPattern = Pattern.compile(String.format(".*(?<%s>\\..*)", EXTENSION_GROUP));
|
||||
@@ -33,8 +32,9 @@ public class FileFilter {
|
||||
}
|
||||
|
||||
if (!hasMatchingPattern(pathName)
|
||||
|| !isNewer(pathName)
|
||||
|| isExcluded(pathName, new HashSet<>(excluded))) {
|
||||
|| isExcluded(pathName, new HashSet<>(excluded))
|
||||
|| lastExecutionHandler != null && !isNewOrChanged(pathName)
|
||||
|| !isNewer(pathName, filterDate)) {
|
||||
log.debug("Excluded {}", pathName);
|
||||
ResultStatistic.getInstance().excluded();
|
||||
return false;
|
||||
@@ -52,19 +52,20 @@ public class FileFilter {
|
||||
return includePattern.matcher(pathName.getName()).matches();
|
||||
}
|
||||
|
||||
private boolean isNewer(File pathName) {
|
||||
if (filterDate == null) return true;
|
||||
private boolean isNewer(File pathName, Date date) {
|
||||
if (date == null) return true;
|
||||
try {
|
||||
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
|
||||
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()));
|
||||
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()), date)
|
||||
|| isNewer(DateUtils.convert(attributes.lastModifiedTime().toMillis()), date);
|
||||
} catch (IOException e) {
|
||||
log.warn("File attributes could not be read", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isNewer(Date creationDate) {
|
||||
return creationDate.toInstant().isAfter(filterDate.toInstant());
|
||||
private boolean isNewer(Date creationDate, Date compareDate) {
|
||||
return creationDate.toInstant().isAfter(compareDate.toInstant());
|
||||
}
|
||||
|
||||
private boolean isExcluded(File pathName, Set<String> excludedDirs) {
|
||||
@@ -86,4 +87,9 @@ public class FileFilter {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isNewOrChanged(File pathname) {
|
||||
Date lastExecutionDate = lastExecutionHandler.get(pathname.getAbsolutePath());
|
||||
return lastExecutionDate == null || isNewer(pathname, lastExecutionDate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
|
||||
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
public class LastExecutionHandler {
|
||||
private final File file;
|
||||
private YAML lastFileExecution;
|
||||
|
||||
public LastExecutionHandler(String path) {
|
||||
file = new File(path);
|
||||
try {
|
||||
lastFileExecution = loadLastFileExecution(file);
|
||||
} catch (YamlInvalidContentException | IOException e) {
|
||||
log.warn("Couldn't find or read {}", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
public YAML loadLastFileExecution(File file) throws YamlInvalidContentException, IOException {
|
||||
if (file.exists() && file.isFile()) {
|
||||
return new YAML(file);
|
||||
}
|
||||
return new YAML("");
|
||||
}
|
||||
|
||||
public Date get(String path) {
|
||||
if (!lastFileExecution.isSet(path)) return null;
|
||||
try {
|
||||
return Date.from(Instant.parse(lastFileExecution.getString(path)));
|
||||
} catch (YamlKeyNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(String path) {
|
||||
update(path, Date.from(Instant.now()));
|
||||
}
|
||||
|
||||
public void update(String path, Date execution) {
|
||||
if (lastFileExecution == null) return;
|
||||
lastFileExecution.set(path, execution.toInstant());
|
||||
}
|
||||
|
||||
public void persist() {
|
||||
try {
|
||||
lastFileExecution.save(file);
|
||||
} catch (IOException e) {
|
||||
log.warn("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
|
||||
@@ -25,14 +26,16 @@ public abstract class AttributeUpdater {
|
||||
protected final InputConfig config;
|
||||
protected final FileProcessor fileProcessor;
|
||||
protected final AttributeChangeProcessor attributeChangeProcessor;
|
||||
protected final LastExecutionHandler lastExecutionHandler;
|
||||
protected final ResultStatistic statistic = ResultStatistic.getInstance();
|
||||
|
||||
private final ExecutorService executor;
|
||||
|
||||
public AttributeUpdater(InputConfig config, FileProcessor fileProcessor, AttributeChangeProcessor attributeChangeProcessor) {
|
||||
public AttributeUpdater(InputConfig config, FileProcessor fileProcessor, AttributeChangeProcessor attributeChangeProcessor, LastExecutionHandler lastExecutionHandler) {
|
||||
this.config = config;
|
||||
this.fileProcessor = fileProcessor;
|
||||
this.attributeChangeProcessor = attributeChangeProcessor;
|
||||
this.lastExecutionHandler = lastExecutionHandler;
|
||||
this.executor = Executors.newFixedThreadPool(config.getThreads());
|
||||
}
|
||||
|
||||
@@ -62,7 +65,7 @@ public abstract class AttributeUpdater {
|
||||
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||
}
|
||||
|
||||
// writeLastExecutionDate();
|
||||
lastExecutionHandler.persist();
|
||||
|
||||
statistic.stopTimer();
|
||||
statistic.print();
|
||||
@@ -84,6 +87,7 @@ public abstract class AttributeUpdater {
|
||||
* @param fileInfo contains information about file and desired configuration.
|
||||
*/
|
||||
protected void checkStatusAndUpdate(FileInfo fileInfo) {
|
||||
if (lastExecutionHandler != null) lastExecutionHandler.update(fileInfo.getFile().getAbsolutePath());
|
||||
if (!fileInfo.getChanges().isEmpty()) {
|
||||
statistic.changePlanned();
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
@@ -15,8 +16,8 @@ import java.util.Set;
|
||||
@Slf4j
|
||||
public class CoherentAttributeUpdater extends SingleFileAttributeUpdater {
|
||||
|
||||
public CoherentAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor) {
|
||||
super(config, processor, attributeChangeProcessor);
|
||||
public CoherentAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor, LastExecutionHandler lastExecutionHandler) {
|
||||
super(config, processor, attributeChangeProcessor, lastExecutionHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -12,8 +13,8 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class SingleFileAttributeUpdater extends AttributeUpdater {
|
||||
|
||||
public SingleFileAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor) {
|
||||
super(config, processor, attributeChangeProcessor);
|
||||
public SingleFileAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor, LastExecutionHandler lastExecutionHandler) {
|
||||
super(config, processor, attributeChangeProcessor, lastExecutionHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -53,8 +53,8 @@ public class InputConfig implements CommandLine.IVersionProvider {
|
||||
private boolean forceCoherent;
|
||||
|
||||
// TODO: implement usage
|
||||
// @Option(names = {"-n", "--only-new-file"}, description = "sets filter-date to last successful execution (overwrites input of filter-date)")
|
||||
// private boolean onlyNewFiles;
|
||||
@Option(names = {"-n", "--only-new-file"}, description = "ignores all files unchanged and previously processed")
|
||||
private boolean onlyNewFiles;
|
||||
@Option(names = {"-d", "--filter-date"}, defaultValue = Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
|
||||
private Date filterDate;
|
||||
@Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern")
|
||||
|
||||
@@ -71,7 +71,7 @@ class FileFilterTest {
|
||||
when(file.toPath()).thenReturn(Path.of(TEST_FILE));
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
FileFilter fileFilter = new FileFilter(excludedDirs, Pattern.compile(pattern), new Date(currentTime + filterDateOffset));
|
||||
FileFilter fileFilter = new FileFilter(excludedDirs, Pattern.compile(pattern), new Date(currentTime + filterDateOffset), null);
|
||||
|
||||
try (MockedStatic<DateUtils> mockedFiles = Mockito.mockStatic(DateUtils.class)) {
|
||||
mockedFiles
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LastExecutionHandlerTest {
|
||||
private static final String LAST_EXECUTION_YML = "./last-execution.yml";
|
||||
|
||||
@AfterEach
|
||||
void destruct() {
|
||||
File file = new File("./last-execution.yml");
|
||||
if (file.exists()) file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingFile() throws IOException {
|
||||
LastExecutionHandler underTest = new LastExecutionHandler("./last-execution.yml");
|
||||
assertNull(underTest.get("/arst"));
|
||||
underTest.update("/arst");
|
||||
assertNotNull(underTest.get("/arst"));
|
||||
underTest.persist();
|
||||
File file = new File("./last-execution.yml");
|
||||
assertTrue(file.exists());
|
||||
assertTrue(Files.readString(file.toPath()).contains("/arst: "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyFile() throws IOException {
|
||||
File file = new File(LAST_EXECUTION_YML);
|
||||
file.createNewFile();
|
||||
missingFile(); // does the checks needed for empty file case
|
||||
}
|
||||
|
||||
@Test
|
||||
void existingFileNoChanges() throws IOException {
|
||||
File file = new File(LAST_EXECUTION_YML);
|
||||
file.createNewFile();
|
||||
Files.writeString(file.toPath(), "/arst: \"" + Instant.now() + "\"");
|
||||
String expected = Files.readString(file.toPath());
|
||||
|
||||
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
||||
assertNotNull(underTest.get("/arst"));
|
||||
underTest.persist();
|
||||
File file1 = new File(LAST_EXECUTION_YML);
|
||||
assertTrue(file1.exists());
|
||||
assertEquals(expected, Files.readString(file.toPath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void existingFileWithChanges() throws IOException {
|
||||
File file = new File(LAST_EXECUTION_YML);
|
||||
file.createNewFile();
|
||||
Files.writeString(file.toPath(), "/arst: \"" + Instant.now() + "\"");
|
||||
String expected = Files.readString(file.toPath());
|
||||
|
||||
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
||||
assertNotNull(underTest.get("/arst"));
|
||||
underTest.update("/arst");
|
||||
assertNotNull(underTest.get("/arst"));
|
||||
underTest.persist();
|
||||
File file1 = new File(LAST_EXECUTION_YML);
|
||||
assertTrue(file1.exists());
|
||||
assertNotEquals(expected, Files.readString(file.toPath()));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -36,7 +37,7 @@ class AttributeUpdaterTest {
|
||||
InputConfig config = new InputConfig();
|
||||
config.setThreads(1);
|
||||
config.setSafeMode(true);
|
||||
AttributeUpdater underTest = new AttributeUpdater(config, null, null) {
|
||||
AttributeUpdater underTest = new AttributeUpdater(config, null, null, new LastExecutionHandler("")) {
|
||||
@Override
|
||||
protected List<File> getFiles() {
|
||||
return List.of();
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.CommandRunner;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.TrackAttributes;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
@@ -66,7 +66,8 @@ class CoherentAttributeUpdaterTest {
|
||||
new CommandLine(commandRunner).parseArgs("-a", "ger:ger", "/arst");
|
||||
InputConfig config = commandRunner.getConfig();
|
||||
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(config.getPreferredSubtitles().toArray(new String[0]), config.getForcedKeywords(), config.getCommentaryKeywords(), config.getHearingImpaired());
|
||||
CoherentAttributeUpdater updater = new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor);
|
||||
LastExecutionHandler lastExecutionHandler = new LastExecutionHandler("");
|
||||
CoherentAttributeUpdater updater = new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
|
||||
Set<FileInfo> matchedFiles = new HashSet<>(fileInfoMock.size() * 2);
|
||||
|
||||
List<File> files = new ArrayList<>();
|
||||
@@ -193,7 +194,8 @@ class CoherentAttributeUpdaterTest {
|
||||
doReturn(testMkvFiles).when(fileProcessor).loadFiles(any());
|
||||
|
||||
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(new String[]{"pref"}, Set.of("forced"), Set.of("commentary"), Set.of("SDH"));
|
||||
CoherentAttributeUpdater underTest = new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor);
|
||||
LastExecutionHandler lastExecutionHandler = new LastExecutionHandler("");
|
||||
CoherentAttributeUpdater underTest = new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
|
||||
|
||||
underTest.process(new File(""));
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.TrackAttributes;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
@@ -16,7 +16,6 @@ import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.PathUtils.TEST_DIR;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.PathUtils.TEST_FILE;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TrackAttributeUtil.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -78,7 +77,8 @@ class SingleFileAttributeUpdaterTest {
|
||||
FileProcessor fileProcessor = spy(FileProcessor.class);
|
||||
doReturn(fileInfo).when(fileProcessor).readAttributes(any());
|
||||
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(new String[]{"pref"}, Set.of("forced"), Set.of("commentary"), Set.of("SDH"));
|
||||
SingleFileAttributeUpdater underTest = new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor);
|
||||
LastExecutionHandler lastExecutionHandler = new LastExecutionHandler("");
|
||||
SingleFileAttributeUpdater underTest = new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
|
||||
|
||||
underTest.process(fileInfo.getFile());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user