Version.scala 3.43 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
8
9
10
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
Peter van 't Hof's avatar
Peter van 't Hof committed
12
13
14
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package nl.lumc.sasc.biopet.core

import java.io.File

import nl.lumc.sasc.biopet.utils.Logging
import org.broadinstitute.gatk.queue.function.QFunction

import scala.collection.mutable
import scala.sys.process.{ Process, ProcessLogger }
import scala.util.matching.Regex

/**
 * Created by pjvan_thof on 10/13/15.
 */
trait Version extends QFunction {
  /** Command to get version of executable */
  def versionCommand: String

  /** Regex to get version from version command output */
  def versionRegex: Regex

  /** Allowed exit codes for the version command */
  protected[core] def versionExitcode = List(0)

  /** Executes the version command */
  private[core] def getVersionInternal: Option[String] = {
    if (versionCommand == null || versionRegex == null) None
    else Version.getVersionInternal(versionCommand, versionRegex, versionExitcode)
  }

  /** Get version from cache otherwise execute the version command  */
  def getVersion: Option[String] = {
    if (!Version.versionCache.contains(versionCommand))
      getVersionInternal match {
        case Some(version) => Version.versionCache += versionCommand -> version
        case _             =>
      }
    Version.versionCache.get(versionCommand)
  }

  override def freezeFieldValues(): Unit = {
    super.freezeFieldValues()
    addJobReportBinding("version", getVersion.getOrElse("NA"))
  }
}

object Version extends Logging {
  private[core] val versionCache: mutable.Map[String, String] = mutable.Map()

  /** Executes the version command */
  private[core] def getVersionInternal(versionCommand: String,
                                       versionRegex: Regex,
                                       versionExitcode: List[Int] = List(0)): Option[String] = {
    if (versionCache.contains(versionCommand)) return versionCache.get(versionCommand)
    else if (versionCommand == null || versionRegex == null) return None
    else {
      val stdout = new StringBuffer()
      val stderr = new StringBuffer()
      def outputLog = "Version command: \n" + versionCommand +
        "\n output log: \n stdout: \n" + stdout.toString +
        "\n stderr: \n" + stderr.toString
      val process = Process(versionCommand).run(ProcessLogger(stdout append _ + "\n", stderr append _ + "\n"))
      if (!versionExitcode.contains(process.exitValue())) {
        logger.warn("getVersion give exit code " + process.exitValue + ", version not found \n" + outputLog)
        return None
      }
      for (line <- stdout.toString.split("\n") ++ stderr.toString.split("\n")) {
        line match {
          case versionRegex(m) => return Some(m)
          case _               =>
        }
      }
      logger.warn("getVersion give a exit code " + process.exitValue + " but no version was found, executable correct? \n" + outputLog)
      None
    }
  }

}