2010-06-23 02:39:20 +08:00
|
|
|
package org.broadinstitute.sting.queue.util
|
|
|
|
|
|
|
|
|
|
import java.io.{IOException, File}
|
|
|
|
|
|
2010-08-10 00:42:48 +08:00
|
|
|
/**
|
|
|
|
|
* A collection of utilities for modifying java.io.
|
|
|
|
|
*/
|
2010-06-23 02:39:20 +08:00
|
|
|
object IOUtils {
|
2010-08-10 00:42:48 +08:00
|
|
|
/** The current directory "." */
|
2010-06-23 02:39:20 +08:00
|
|
|
val CURRENT_DIR = new File(".")
|
|
|
|
|
|
2010-08-10 00:42:48 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the sub path rooted at the parent.
|
|
|
|
|
* If the sub path is already absolute, returns the sub path.
|
|
|
|
|
* If the parent is the current directory, returns the sub path.
|
|
|
|
|
* If the sub bath is the current directory, returns the parent.
|
|
|
|
|
* Else returns new File(parent, subPath)
|
|
|
|
|
* @param parent The parent directory
|
|
|
|
|
* @param path The sub path to append to the parent, if the path is not absolute.
|
|
|
|
|
* @return The absolute path to the file in the parent dir if the path was not absolute, otherwise the original path.
|
|
|
|
|
*/
|
|
|
|
|
def subDir(dir: File, path: String): File =
|
|
|
|
|
subDir(dir.getAbsoluteFile, new File(path))
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the sub path rooted at the parent.
|
|
|
|
|
* If the sub path is already absolute, returns the sub path.
|
|
|
|
|
* If the parent is the current directory, returns the sub path.
|
|
|
|
|
* If the sub bath is the current directory, returns the parent.
|
|
|
|
|
* Else returns new File(parent, subPath)
|
|
|
|
|
* @param parent The parent directory
|
|
|
|
|
* @param file The sub path to append to the parent, if the path is not absolute.
|
|
|
|
|
* @return The absolute path to the file in the parent dir if the path was not absolute, otherwise the original path.
|
|
|
|
|
*/
|
|
|
|
|
def subDir(parent: File, file: File): File = {
|
2010-06-23 02:39:20 +08:00
|
|
|
if (parent == CURRENT_DIR && file == CURRENT_DIR)
|
2010-08-10 00:42:48 +08:00
|
|
|
CURRENT_DIR.getCanonicalFile.getAbsoluteFile
|
2010-06-23 02:39:20 +08:00
|
|
|
else if (parent == CURRENT_DIR || file.isAbsolute)
|
2010-08-10 00:42:48 +08:00
|
|
|
file.getAbsoluteFile
|
2010-06-23 02:39:20 +08:00
|
|
|
else if (file == CURRENT_DIR)
|
2010-08-10 00:42:48 +08:00
|
|
|
parent.getAbsoluteFile
|
2010-06-23 02:39:20 +08:00
|
|
|
else
|
2010-08-10 00:42:48 +08:00
|
|
|
new File(parent, file.getPath).getAbsoluteFile
|
2010-06-23 02:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
2010-08-10 00:42:48 +08:00
|
|
|
/**
|
|
|
|
|
* Resets the parent of the file to the directory.
|
|
|
|
|
* @param dir New parent directory.
|
|
|
|
|
* @param file Path to the file to be re-rooted.
|
|
|
|
|
* @return Absolute path to the new file.
|
|
|
|
|
*/
|
|
|
|
|
def resetParent(dir: File, file: File) = subDir(dir.getAbsoluteFile, file.getName).getAbsoluteFile
|
2010-06-23 02:39:20 +08:00
|
|
|
|
2010-08-10 00:42:48 +08:00
|
|
|
/**
|
|
|
|
|
* Creates a scatterGatherTempDir directory with the prefix and optional suffix.
|
|
|
|
|
* @param prefix Prefix for the directory name.
|
|
|
|
|
* @param suffix Optional suffix for the directory name. Defaults to "".
|
|
|
|
|
* @return The created temporary directory.
|
|
|
|
|
* @throws IOException if the directory could not be created.
|
|
|
|
|
*/
|
|
|
|
|
def tempDir(prefix: String, suffix: String = "") = {
|
|
|
|
|
val temp = File.createTempFile(prefix + "-", suffix)
|
|
|
|
|
if(!temp.delete)
|
|
|
|
|
throw new IOException("Could not delete sub file: " + temp.getAbsolutePath())
|
|
|
|
|
if(!temp.mkdir)
|
|
|
|
|
throw new IOException("Could not create sub directory: " + temp.getAbsolutePath())
|
|
|
|
|
temp
|
|
|
|
|
}
|
2010-06-23 02:39:20 +08:00
|
|
|
}
|