From: Jim Procter Date: Mon, 11 Nov 2024 16:24:23 +0000 (+0000) Subject: Merge branch 'develop' into bug/JAL-4477_unmapped_filename_characters X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d7c3dc79c05b6bcfb728e455ed8148650d00d551;p=jalview.git Merge branch 'develop' into bug/JAL-4477_unmapped_filename_characters --- d7c3dc79c05b6bcfb728e455ed8148650d00d551 diff --cc src/jalview/util/FileUtils.java index ce74673,1170f9c..e91408a --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@@ -36,9 -36,13 +36,11 @@@ import java.nio.file.attribute.BasicFil 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; - public class FileUtils { /* @@@ -361,42 -371,133 +370,170 @@@ } } + public static Path getCanonicalPath(Path path) + { + return path.normalize(); + } + + public static Path getCanonicalPath(File file) + { + return getCanonicalPath(file.toPath()); + } + + public static Path getCanonicalPath(String pathString) + { + return getCanonicalPath(Paths.get(pathString)); + } + + public static File getCanonicalFile(File file) + { + return getCanonicalPath(file).toFile(); + } + + public static File getCanonicalFile(String pathString) + { + return getCanonicalPath(Paths.get(pathString)).toFile(); + } + + public static boolean mkdirs(File file) + { + try + { + Files.createDirectories(getCanonicalPath(file)); + return file.exists(); + } catch (IOException e) + { + LaunchUtils.syserr(true, false, "Failed to make directory " + file + + "\n" + e.getStackTrace()); + } + return false; + } + /** + * 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 versionSeparator, + boolean exists) + { + 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; + } + + for (String template : templates) + { + Console.debug("Using template '" + template + "'"); + for (String root : roots) + { + Console.debug("Using root '" + root + "'"); + + // Blacklist. Use a file glob for version and see what's there + 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 (versionSeparator != null) + { + vSB.append(versionSeparator); + } + 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 (versionSeparator != null) + { + // try without a version number too + String nonVersioned = String.format(template, root, ""); + matchingFiles.addAll( + FileUtils.getFilesFromGlob(nonVersioned, false)); + } + } + + // Whitelist. Just add a path for every whitelisted version (or check it + // exists). + if (versionWhitelist != null) + { + Console.debug("Adding " + versionWhitelist.length + + " whitelist versions"); + for (String addVersion : versionWhitelist) + { + StringBuilder vSB = new StringBuilder(); + if (versionSeparator != null) + { + vSB.append(versionSeparator); + } + vSB.append(addVersion); + String versionString = vSB.toString(); + String versionPath = String.format(template, root, + versionString); + Console.debug("Adding whitelist path '" + versionPath + "'"); + File file = new File(versionPath); + if (file.exists() || !exists) + { + matchingFiles.add(file); + } + } + + if (versionSeparator != null) + { + // try without a version number too + String nonVersioned = String.format(template, root, ""); + File file = new File(nonVersioned); + if (file.exists() || !exists) + { + matchingFiles.add(file); + } + } + } + } + } + + List files = new ArrayList(); + files.addAll(matchingFiles); + return files; + } - }