-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
537 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
package models | ||
|
||
import scala.concurrent.Future | ||
import play.api.libs.json.JsObject | ||
|
||
abstract class Analyzer { | ||
def analyze(artifact: Artifact): Future[JsObject] | ||
val name: String | ||
val version: String | ||
val description: String | ||
val dataTypeList: Seq[String] | ||
val author: String | ||
val url: String | ||
val license: String | ||
val id = (name + "_" + version).replaceAll("\\.", "_") | ||
val id: String = (name + "_" + version).replaceAll("\\.", "_") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,16 @@ | ||
package models | ||
|
||
import java.io.{ BufferedReader, InputStreamReader } | ||
import java.nio.file.Path | ||
|
||
import scala.concurrent.{ ExecutionContext, Future, blocking } | ||
import scala.sys.process.{ BasicIO, Process, ProcessIO } | ||
|
||
import akka.stream.Materializer | ||
|
||
import play.api.Logger | ||
import play.api.libs.json.{ JsObject, JsString, Json } | ||
|
||
import com.fasterxml.jackson.core.JsonParseException | ||
import com.fasterxml.jackson.databind.JsonMappingException | ||
import play.api.libs.json.JsObject | ||
|
||
case class ExternalAnalyzer( | ||
name: String, | ||
version: String, | ||
description: String, | ||
dataTypeList: Seq[String], | ||
author: String, | ||
url: String, | ||
license: String, | ||
command: Path, | ||
config: JsObject)(implicit val ec: ExecutionContext) extends Analyzer { | ||
|
||
val log = Logger(getClass) | ||
private val osexec = if (System.getProperty("os.name").toLowerCase.contains("win")) | ||
(c: String) ⇒ s"""cmd /c $c""" | ||
else | ||
(c: String) ⇒ s"""sh -c "./$c" """ | ||
|
||
override def analyze(artifact: Artifact): Future[JsObject] = { | ||
Future { | ||
val input = artifact match { | ||
case FileArtifact(file, attributes) ⇒ attributes + ("file" → JsString(file.getAbsoluteFile.toString)) + ("config" → config) | ||
case DataArtifact(data, attributes) ⇒ attributes + ("data" → JsString(data)) + ("config" → config) | ||
} | ||
val output = new StringBuffer | ||
val error = new StringBuffer | ||
try { | ||
log.info(s"Execute ${osexec(command.getFileName.toString)} in ${command.getParent.toFile.getAbsoluteFile.getName}") | ||
val exitValue = Process(osexec(command.getFileName.toString), command.getParent.toFile).run( | ||
new ProcessIO( | ||
{ stdin ⇒ | ||
try stdin.write(input.toString.getBytes("UTF-8")) | ||
finally stdin.close() | ||
}, | ||
{ stdout ⇒ | ||
val reader = new BufferedReader(new InputStreamReader(stdout, "UTF-8")) | ||
try BasicIO.processLinesFully { line ⇒ | ||
output.append(line).append(System.lineSeparator()) | ||
() | ||
}(reader.readLine) | ||
finally reader.close() | ||
}, | ||
{ stderr ⇒ | ||
val reader = new BufferedReader(new InputStreamReader(stderr, "UTF-8")) | ||
try BasicIO.processLinesFully { line ⇒ | ||
error.append(line).append(System.lineSeparator()) | ||
() | ||
}(reader.readLine) | ||
finally reader.close() | ||
})).exitValue | ||
Json.parse(output.toString).as[JsObject] | ||
} | ||
catch { | ||
case _: JsonMappingException ⇒ | ||
error.append(output) | ||
JsObject(Seq("errorMessage" → JsString(s"Error: Invalid output\n$error"))) | ||
case _: JsonParseException ⇒ | ||
error.append(output) | ||
JsObject(Seq("errorMessage" → JsString(s"Error: Invalid output\n$error"))) | ||
case t: Throwable ⇒ | ||
JsObject(Seq("errorMessage" → JsString(t.getMessage + ":" + t.getStackTrace().mkString("", "\n\t", "\n")))) | ||
} | ||
} | ||
} | ||
} | ||
name: String, | ||
version: String, | ||
description: String, | ||
dataTypeList: Seq[String], | ||
author: String, | ||
url: String, | ||
license: String, | ||
command: Path, | ||
config: JsObject) extends Analyzer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
package models | ||
|
||
import play.api.libs.json.JsObject | ||
import scala.concurrent.Future | ||
import java.util.Date | ||
import scala.util.Success | ||
import scala.util.Failure | ||
|
||
import scala.concurrent.Future | ||
import scala.util.{ Failure, Success } | ||
|
||
object JobStatus extends Enumeration { | ||
type Type = Value | ||
val InProgress, Success, Failure = Value | ||
} | ||
case class Job(id: String, analyzerId: String, artifact: Artifact, report: Future[JsObject]) { | ||
|
||
case class Job(id: String, analyzer: Analyzer, artifact: Artifact, report: Future[Report]) { | ||
val date: Date = new Date() | ||
|
||
def status: JobStatus.Type = report.value match { | ||
case Some(Success(x)) ⇒ (x \ "success").asOpt[Boolean] match { | ||
case Some(true) ⇒ JobStatus.Success | ||
case _ ⇒ JobStatus.Failure | ||
} | ||
case Some(Failure(_)) ⇒ JobStatus.Failure | ||
case None ⇒ JobStatus.InProgress | ||
case Some(Success(SuccessReport(_, _, _))) ⇒ JobStatus.Success | ||
case Some(Success(FailureReport(_))) ⇒ JobStatus.Failure | ||
case Some(Failure(_)) ⇒ JobStatus.Failure | ||
case None ⇒ JobStatus.InProgress | ||
} | ||
} |
Oops, something went wrong.