Skip to content

Commit

Permalink
Refactored ScriptRunner class
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Nov 27, 2018
1 parent cba4332 commit 15ee2f1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
13 changes: 13 additions & 0 deletions src/main/groovy/nextflow/Session.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class Session implements ISession {
*/
List<Path> configFiles

/**
* Local path where script generated classes are saved
*/
private Path classesDir

private Path binDir

private Map<String,Path> binEntries = [:]
Expand Down Expand Up @@ -200,6 +205,8 @@ class Session implements ISession {

WorkflowStats getWorkflowStats() { workflowStats }

Path getClassesDir() { classesDir }

boolean ansiLog

private AnsiLogObserver ansiLogObserver
Expand Down Expand Up @@ -326,6 +333,9 @@ class Session implements ISession {
this.setScriptName(scriptPath.name)
}

// set the byte-code target directory
this.classesDir = FileHelper.createLocalDir()

this.observers = createObservers()
this.statsEnabled = observers.any { it.enableMetrics() }

Expand Down Expand Up @@ -612,6 +622,9 @@ class Session implements ISession {

// -- shutdown s3 uploader
shutdownS3Uploader()

// -- cleanup script classes dir
classesDir.deleteDir()
}
finally {
// -- update the history file
Expand Down
75 changes: 38 additions & 37 deletions src/main/groovy/nextflow/script/ScriptRunner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import nextflow.ast.NextflowXform
import nextflow.config.ConfigBuilder
import nextflow.exception.AbortOperationException
import nextflow.exception.AbortRunException
import nextflow.file.FileHelper
import nextflow.util.ConfigHelper
import nextflow.util.Duration
import nextflow.util.HistoryFile
Expand Down Expand Up @@ -60,7 +59,7 @@ class ScriptRunner {
/**
* The interpreted script object
*/
private BaseScript script
private BaseScript scriptObj

/**
* The pipeline file (it may be null when it's provided as string)
Expand All @@ -87,6 +86,11 @@ class ScriptRunner {
*/
private String commandLine

/**
* Groovy compiler config object used to parse the nextflow script
*/
private CompilerConfiguration compilerConfig

/**
* The used configuration profile
*/
Expand Down Expand Up @@ -130,7 +134,7 @@ class ScriptRunner {
/**
* @return The interpreted script object
*/
BaseScript getScriptObj() { script }
BaseScript getScriptObj() { scriptObj }

/**
* @return The result produced by the script execution
Expand All @@ -153,13 +157,13 @@ class ScriptRunner {
assert scriptText

// init session
session.init(scriptFile?.main)
init(scriptFile?.main, args)

// start session
session.start()
try {
// parse the script
script = parseScript(scriptText, args)
parseScript(scriptText)
// validate the config
validate()
// run the code
Expand Down Expand Up @@ -191,14 +195,14 @@ class ScriptRunner {
assert methodName

// init session
session.init(scriptFile.main)
init(scriptFile.main, args)

script = parseScript(scriptText, args)
parseScript(scriptText)
def values = args ? args.collect { parseValue(it) } : null

def methodsToTest
if ( methodName == '%all' ) {
methodsToTest = script.metaClass.getMethods().findAll { it.name.startsWith('test') }.collect { it.name }.unique()
methodsToTest = scriptObj.metaClass.getMethods().findAll { it.name.startsWith('test') }.collect { it.name }.unique()
}
else {
methodsToTest = methodName.split(',') *.trim()
Expand All @@ -211,13 +215,13 @@ class ScriptRunner {

for( String name: methodsToTest ) {
try {
def metaMethod = script.metaClass.getMethods().find { it.name == name }
def metaMethod = scriptObj.metaClass.getMethods().find { it.name == name }

if( !metaMethod ) {
log.error "Unknown function '$name' -- Check the name spelling"
}
else {
def result = metaMethod.invoke(script, values as Object[] )
def result = metaMethod.invoke(scriptObj, values as Object[] )
if( result != null ) {
log.info "SUCCESS: '$name' == $result"
}
Expand Down Expand Up @@ -247,13 +251,10 @@ class ScriptRunner {
}
}

protected BaseScript parseScript( File file, List<String> args = null ) {
assert file
parseScript( file.text, args )
}
protected void init(Path scriptPath, List<String> args = null) {

session.init(scriptPath)

protected BaseScript parseScript( String scriptText, List<String> args = null) {
log.debug "> Script parsing"
session.binding.setArgs( new ArgsList(args) )
session.binding.setParams( (Map)session.config.params )
// TODO add test for this property
Expand All @@ -277,11 +278,22 @@ class ScriptRunner {
importCustomizer.addImports( MemoryUnit.name )
importCustomizer.addStaticStars( Nextflow.name )

def config = new CompilerConfiguration()
config.addCompilationCustomizers( importCustomizer )
config.scriptBaseClass = BaseScript.class.name
config.addCompilationCustomizers( new ASTTransformationCustomizer(NextflowDSL))
config.addCompilationCustomizers( new ASTTransformationCustomizer(NextflowXform))
compilerConfig = new CompilerConfiguration()
compilerConfig.addCompilationCustomizers( importCustomizer )
compilerConfig.scriptBaseClass = BaseScript.class.name
compilerConfig.addCompilationCustomizers( new ASTTransformationCustomizer(NextflowDSL))
compilerConfig.addCompilationCustomizers( new ASTTransformationCustomizer(NextflowXform))

compilerConfig.setTargetDirectory(session.classesDir.toFile())
}

protected BaseScript parseScript( File file ) {
assert file
parseScript( file.text )
}

protected BaseScript parseScript( String scriptText ) {
log.debug "> Script parsing"

// extend the class-loader if required
def gcl = new GroovyClassLoader()
Expand All @@ -292,23 +304,13 @@ class ScriptRunner {
gcl.addClasspath(path.toString())
}

// set the byte-code target directory
def targetDir = FileHelper.createLocalDir()
config.setTargetDirectory(targetDir.toFile())
// add the directory of generated classes to the lib path
// so that it can be propagated to remote note (when necessary)
session.getLibDir().add(targetDir)

// set the script class-loader
session.classLoader = gcl

// run and wait for termination
BaseScript result
def groovy = new GroovyShell(gcl, session.binding, config)
result = groovy.parse(scriptText, session.scriptClassName) as BaseScript

session.onShutdown { targetDir.deleteDir() }
return result
def groovy = new GroovyShell(gcl, session.binding, compilerConfig)
scriptObj = groovy.parse(scriptText, session.scriptClassName) as BaseScript
}

/**
Expand All @@ -334,7 +336,7 @@ class ScriptRunner {
}

@PackageScope void checkConfig() {
session.validateConfig(script.getProcessNames())
session.validateConfig(scriptObj.getProcessNames())
}

@PackageScope VersionNumber getCurrentVersion() {
Expand Down Expand Up @@ -374,10 +376,9 @@ class ScriptRunner {
*/
protected run() {
log.debug "> Launching execution"
assert script, "Missing script instance to run"

assert scriptObj, "Missing script instance to run"
// -- launch the script execution
output = script.run()
output = scriptObj.run()
}

protected terminate() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nextflow/util/RemoteSession.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class RemoteSession implements Serializable, Closeable {
RemoteSession(Session session) {
sessionId = session.getUniqueId()
statsEnabled = session.statsEnabled
// the main dir containing script classes
zip(session.classesDir)
// add all libs path
for( Path entry : session.getLibDir() ) {
libs.add( zip(entry) )
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/groovy/nextflow/util/RemoteSessionTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class RemoteSessionTest extends Specification {

def session = new Session()
session.@libDir = [ path1, path2 ]
session.classesDir = Files.createTempDirectory('test')

when:
def remote = new RemoteSession(session)
Expand All @@ -87,6 +88,7 @@ class RemoteSessionTest extends Specification {
remote?.close()
path1?.deleteDir()
path2?.deleteDir()
session?.classesDir?.deleteDir()
}

}

0 comments on commit 15ee2f1

Please sign in to comment.