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
{
/*
}
}
+ 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<File> getMatchingVersionedFiles(String[] templates,
+ String[] roots, String[] versionWhitelist,
+ String[] versionBlacklist, String versionSeparator,
+ boolean exists)
+ {
+ Set<File> matchingFiles = new HashSet<>();
+ if (templates == null)
+ {
+ Console.debug(
+ "getMatchingVersionedFiles called with a null template array");
+ List<File> files = new ArrayList<File>();
+ 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<File> 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<File> files = new ArrayList<File>();
+ files.addAll(matchingFiles);
+ return files;
+ }
-
}