From: Ben Soares Date: Wed, 6 Nov 2024 12:13:53 +0000 (+0000) Subject: JAL-4485 Extracted the whitelist/blacklist fileglob finding code into FileUtils for... X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=c33f378461cc617d47f30d6e4bb426724d338d8f;p=jalview.git JAL-4485 Extracted the whitelist/blacklist fileglob finding code into FileUtils for more generic use --- diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 7950de4..5f58903 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -36,7 +36,9 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import jalview.bin.Console; @@ -368,4 +370,123 @@ public class FileUtils return hasExtension ? filename0.substring(0, dot + 1) : filename0; } } + + /** + * Look for files that use a template, with two "%s" formatting entries, to + * look for files with the template substituted with combinations of root and + * version. If versionWhitelist is not null then these paths will be added. If + * versionBlacklist is not null then globbed versions will be looked for and + * these versions excluded. If both are given then both will be included. If + * separator is not null then it will be added before the version number, and + * additionally a path without the separator and version will be looked for or + * added if the whitelist or blacklist are supplied respectively. + * + * @param templates + * @param roots + * @param versionWhitelist + * @param versionBlacklist + * @param separator + * @return + */ + public static List getMatchingVersionedFiles(String[] templates, + String[] roots, String[] versionWhitelist, + String[] versionBlacklist, String separator) + { + Set matchingFiles = new HashSet<>(); + if (templates == null) + { + Console.debug( + "getMatchingVersionedFiles called with a null template array"); + List files = new ArrayList(); + files.addAll(matchingFiles); + return files; + } + + Console.debug("Looking for all versions with a blacklist of size " + + versionBlacklist.length); + for (String template : templates) + { + Console.debug("Using template '" + template + "'"); + for (String root : roots) + { + Console.debug("Using root '" + root + "'"); + if (versionBlacklist != null) + { + String globMatch = String.format(template, root, "*"); + Console.debug("Using glob '" + globMatch + "'"); + List foundFiles = FileUtils.getFilesFromGlob(globMatch, + false); + for (File found : foundFiles) + { + Console.debug("Checking " + found.getPath() + " is okay"); + boolean add = true; + for (String notVersion : versionBlacklist) + { + StringBuilder vSB = new StringBuilder(); + if (separator != null) + { + vSB.append(separator); + } + vSB.append(notVersion); + String versionString = vSB.toString(); + if (String.format(template, root, versionString) + .equals(found.getPath())) + { + add = false; + Console.debug( + "Not adding " + found.getPath() + ": version '" + + notVersion + "' is in the blacklist"); + break; + } + } + if (add) + { + Console.debug("Adding " + found.getPath() + " to list"); + matchingFiles.add(found); + } + } + + if (separator != null) + { + // try without a version number too + String nonVersioned = String.format(template, root, ""); + matchingFiles.addAll( + FileUtils.getFilesFromGlob(nonVersioned, false)); + } + } + + if (versionWhitelist != null) + { + Console.debug("Adding " + versionWhitelist.length + + " whitelist versions"); + for (String addVersion : versionWhitelist) + { + StringBuilder vSB = new StringBuilder(); + if (separator != null) + { + vSB.append(separator); + } + vSB.append(addVersion); + String versionString = vSB.toString(); + String versionPath = String.format(template, root, + versionString); + Console.debug("Adding whitelist path '" + versionPath + "'"); + matchingFiles.add(new File(versionPath)); + } + + if (separator != null) + { + // try without a version number too + String nonVersioned = String.format(template, root, ""); + matchingFiles.add(new File(nonVersioned)); + } + } + } + } + + List files = new ArrayList(); + files.addAll(matchingFiles); + return files; + } + }