From 7b260498229f79ed4fbc82c3de45bc1656b9bbc8 Mon Sep 17 00:00:00 2001 From: bow <bow@bow.web.id> Date: Tue, 29 Jul 2014 14:23:51 +0200 Subject: [PATCH] Add relative symlink option to Ln wrapper --- .../nl/lumc/sasc/biopet/function/Ln.scala | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/function/Ln.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/function/Ln.scala index d7e9aad05..b3d148bab 100644 --- a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/function/Ln.scala +++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/function/Ln.scala @@ -1,10 +1,11 @@ package nl.lumc.sasc.biopet.function -import nl.lumc.sasc.biopet.core.config.Configurable -import org.broadinstitute.gatk.queue.function.InProcessFunction -import org.broadinstitute.gatk.utils.commandline.{ Input, Output } import java.io.File import scala.sys.process.Process +import org.apache.commons.io.FilenameUtils; +import org.broadinstitute.gatk.queue.function.InProcessFunction +import org.broadinstitute.gatk.utils.commandline.{ Input, Output } +import nl.lumc.sasc.biopet.core.config.Configurable class Ln(val root: Configurable) extends InProcessFunction with Configurable { this.analysisName = getClass.getSimpleName @@ -15,8 +16,61 @@ class Ln(val root: Configurable) extends InProcessFunction with Configurable { @Output(doc = "Link destination") var out: File = _ + var relative: Boolean = true + + private lazy val inCanonical: String = { + // need to remove "/~" to correctly expand path with tilde + in.getCanonicalPath().replace("/~", "") + } + + private lazy val outCanonical: String = { + out.getCanonicalPath().replace("/~", "") + } + + private lazy val inToks: Array[String] = { + inCanonical.split(File.separator) + } + + private lazy val outToks: Array[String] = { + outCanonical.split(File.separator) + } + + private lazy val commonPrefixLength: Int = { + val maxLength = scala.math.min(inToks.length, outToks.length) + var i: Int = 0; + while (i < maxLength && inToks(i) == outToks(i)) i += 1; + i + } + + private lazy val inUnique: String = { + inToks.slice(commonPrefixLength, inToks.length).mkString(File.separator) + } + + private lazy val outUnique: String = { + outToks.slice(commonPrefixLength, outToks.length).mkString(File.separator) + } + + private lazy val inRelative: String = { + // calculate 'distance' from output directory to input + // which is the number of directory walks required to get to the inUnique directory from outDir + val outDir = FilenameUtils.getFullPathNoEndSeparator(outUnique) + val dist: Int = scala.math.max(0, outDir.split(File.separator).length - 1) + val result = + if (dist > 0) + ((".." + File.separator) * dist) + File.separator + inUnique + else + inUnique + result + } + override def run { - val cmd = "ln -s " + in + " " + out + val cmd = + if (relative) { + // workaround until we have `ln` that works with relative path (i.e. `ln -r`) + "ln -s " + inRelative + " " + outCanonical + } else { + "ln -s " + inCanonical + " " + outCanonical + } val process = Process(cmd).run System.out.println("cmd: '" + cmd + "', exitcode: " + process.exitValue) } -- GitLab