<commit_before>import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger
import groovy.lang.Binding
import groovy.transform.Synchronized

import org.cloudifysource.utilitydomain.context.ServiceContextFactory

public class CloudifyExecutorUtils {
    
    static def counter = new AtomicInteger()
    static def threadPool = Executors.newFixedThreadPool(50, { r -> return new Thread(r as Runnable, "alien-executor-" + counter.incrementAndGet()) } as ThreadFactory )
    static def call = { c -> threadPool.submit(c as Callable) }

    
    /**
     * execute a script bash or batch
     * 
     * @param script
     * @param argsMap
     * @return
     */
    static def executeScript(script, Map argsMap) {
        
        // Execute bash script
        def context = ServiceContextFactory.getServiceContext()
        def serviceDirectory = context.getServiceDirectory()
        println "service dir is: ${serviceDirectory}; script is: ${script}"
        def fullPathScript = "${serviceDirectory}/${script}";
        new AntBuilder().sequential {
            echo(message: "${fullPathScript} will be mark as executable...")
            chmod(file: "${fullPathScript}", perm:"+xr")
        }
        
        String[] environment = new EnvironmentBuilder().buildShOrBatchEnvironment(argsMap);
        
        println "Executing file ${serviceDirectory}/${script}.\n environment is: ${script}"
        def scriptProcess = "${serviceDirectory}/${script}".execute(environment, null)
        scriptProcess.consumeProcessOutput(System.out, System.out)

        scriptProcess.waitFor()
        def scriptExitValue = scriptProcess.exitValue()
        
        print """
      ----------${script} : bash : Return Code ----------
      Return Code : $scriptExitValue
      ---------------------------------\n
      """
        if(scriptExitValue) {
            throw new RuntimeException("Error executing the script ${script} (return code: $scriptExitValue)")
        }
        
        return scriptOut;
    }
    
    
    /**
     * Execute a goovy script. Argument are passed to the groovy via the GroovyShell Binding, where they are injected as variables.
     * Consider to pass them as env vars
     * for a closure, we should not use the ServiceContextFactory as the context instance is already injected in the service file
     * Therefore, the caller script should have a defined "context" variable
     * */
    static def executeGroovy(context, groovyScript,  Map argsMap) {
        def serviceDirectory = context.getServiceDirectory()
        
        Binding binding = new EnvironmentBuilder().buildGroovyEnvironment(context, argsMap)

        //hack for a MissingPropertyException thrown for CloudifyExecutorUtils<commit_msg>Fix Groovy script error<commit_after>import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger
import groovy.lang.Binding
import groovy.transform.Synchronized

import org.cloudifysource.utilitydomain.context.ServiceContextFactory

public class CloudifyExecutorUtils {
    
    static def counter = new AtomicInteger()
    static def threadPool = Executors.newFixedThreadPool(50, { r -> return new Thread(r as Runnable, "alien-executor-" + counter.incrementAndGet()) } as ThreadFactory )
    static def call = { c -> threadPool.submit(c as Callable) }

    
    /**
     * execute a script bash or batch
     * 
     * @param script
     * @param argsMap
     * @return
     */
    static def executeScript(script, Map argsMap) {
        
        // Execute bash script
        def context = ServiceContextFactory.getServiceContext()
        def serviceDirectory = context.getServiceDirectory()
        println "service dir is: ${serviceDirectory}; script is: ${script}"
        def fullPathScript = "${serviceDirectory}/${script}";
        new AntBuilder().sequential {
            echo(message: "${fullPathScript} will be mark as executable...")
            chmod(file: "${fullPathScript}", perm:"+xr")
        }
        
        String[] environment = new EnvironmentBuilder().buildShOrBatchEnvironment(argsMap);
        
        println "Executing file ${serviceDirectory}/${script}.\n environment is: ${script}"
        def scriptProcess = "${serviceDirectory}/${script}".execute(environment, null)
        scriptProcess.consumeProcessOutput(System.out, System.out)

        scriptProcess.waitFor()
        def scriptExitValue = scriptProcess.exitValue()
        
        print """
      ----------${script} : bash : Return Code ----------
      Return Code : $scriptExitValue
      ---------------------------------\n
      """
        if(scriptExitValue) {
            throw new RuntimeException("Error executing the script ${script} (return code: $scriptExitValue)")
        }
    }
    
    
    /**
     * Execute a goovy script. Argument are passed to the groovy via the GroovyShell Binding, where they are injected as variables.
     * Consider to pass them as env vars
     * for a closure, we should not use the ServiceContextFactory as the context instance is already injected in the service file
     * Therefore, the caller script should have a defined "context" variable
     * */
    static def executeGroovy(context, groovyScript,  Map argsMap) {
        def serviceDirectory = context.getServiceDirectory()
        
        Binding binding = new EnvironmentBuilder().buildGroovyEnvironment(context, argsMap)

        //hack for a MissingPropertyException thrown for CloudifyExecutorUtils
{'fraction_non_alphanumeric': 0.10892172961138478, 'fraction_numerical': 0.0007297938332421091, 'mean_word_length': 2.696561024949427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5004447', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1334, 'n_words': 416}