<commit_before>
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipFile
import org.apache.log4j.Logger

class LoadExamples {

	private final String SCRIPT_NAME = "LoadExamples.groovy"

	private final String LOAD_PACKAGE_SCRIPT_NAME = "LoadPackage.groovy"


	private final String SCHEDULER_HOME
	private final String EXAMPLES_ZIP_PATH
	private final String EXAMPLES_DIR_PATH
	private final String TOOLS_DIR
	private final GroovyObject package_loader

	private logger = Logger.getLogger("org.ow2.proactive.scheduler")


	LoadExamples(binding) {

		// Bindings
		this.SCHEDULER_HOME = binding.variables.get("pa.scheduler.home")

		// User variables
		this.EXAMPLES_ZIP_PATH = new File(this.SCHEDULER_HOME, "samples/proactive-examples.zip").absolutePath
		this.TOOLS_DIR = new File(this.SCHEDULER_HOME, "tools")

		// Deduced variables
		this.EXAMPLES_DIR_PATH = this.EXAMPLES_ZIP_PATH.substring(0,this.EXAMPLES_ZIP_PATH.lastIndexOf("."))

		// Create a new instance of the package loader
		File load_package_script = new File(this.TOOLS_DIR, this.LOAD_PACKAGE_SCRIPT_NAME)
		if (load_package_script.exists()) {
			GroovyClassLoader gcl = new GroovyClassLoader()
			Class loadPackageClass = gcl.parseClass(load_package_script)
			this.package_loader= (GroovyObject) loadPackageClass.newInstance(binding)
		} else {
			return
		}
	}


	def unzipFile(src, dest) {
		def zipFile = new ZipFile(src)
		zipFile.entries().each { it ->
			def path = Paths.get(dest + "/" + it.name)
			if(it.directory){
				Files.createDirectories(path)
			}
			else {
				def parentDir = path.getParent()
				if (!Files.exists(parentDir)) {
					Files.createDirectories(parentDir)
				}
				Files.copy(zipFile.getInputStream(it), path)
			}
		}
	}

	def writeToOutput(String output) {
		logger.info("[" + this.SCRIPT_NAME + "] " + output)
	}


	def run() {
		writeToOutput(" Automatic deployment of proactive packages ...")

		writeToOutput(" Variables : ")
		writeToOutput(" EXAMPLES_ZIP_PATH " + this.EXAMPLES_ZIP_PATH)
		writeToOutput(" EXAMPLES_DIR_PATH " + this.EXAMPLES_DIR_PATH)

		writeToOutput(" Actions : ")

		// If the unzipped dir already exists, stop the script execution
		def examples_dir = new File(this.EXAMPLES_DIR_PATH)
		if (examples_dir.exists())
		{
			writeToOutput(this.EXAMPLES_DIR_PATH + " already exists, delete it to redeploy packages.")
			writeToOutput("Terminated.")
			return
		}

		// Unzip the examples
		def examples_zip = new File(this.EXAMPLES_ZIP_PATH)
		if (!examples_zip.exists())
		{
			writeToOutput(this.EXAMPLES_ZIP_PATH + " not found!")
			return

		}
		unzipFile(examples_zip, this.EXAMPLES_DIR_PATH)
		writeToOutput(this.EXAMPLES_ZIP_PATH + " extracted!")


		//retrieve all (package, bucket) pairs
		def package_bucket_map = [:]
		examples_dir.eachFile() { package_dir ->
			def metadata_file = new File(package_dir.absolutePath, "METADATA.json")
			if (metadata_file.exists()) {
				def slurper = new groovy.json.JsonSlurper()
				def metadata_file_map = (Map) slurper.parseText(metadata_file.text)
				def bucket_name = metadata_file_map.get("catalog").get("bucket")
				package_bucket_map.put(package_dir, bucket_name)
			}
		}

		// Retrieve the ordered bucket list
		def ordered_bucket_list = []
		def ordered_bucket_list_file = new File(examples_dir, "ordered_bucket_list")
		ordered_bucket_list_file.text.split(",").each { bucket_name ->
			ordered_bucket_list.add(bucket_name)
			// Define a specific filter on package directories to only consider those targeting the current bucket
			def file_filter = new FileFilter() {
				boolean accept(File package_dir) {
					//def package_dir = new File(pathname)
					def metadata_file = new File(package_dir.absolutePath, "METADATA.json")
					if (metadata_file.exists()) {
						// From json to map
						def slurper = new groovy.json.JsonSlurper()
						def metadata_file_map = (Map) slurper.parseText(metadata_file.text)
						return bucket_name = metadata_file_map.get("catalog").get("bucket")
					}
					return false
				}
			}

			// For each package targeting the current bucket call the package loader class<commit_msg>Fix issue when filtering packages by targeted bucket<commit_after>
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipFile
import org.apache.log4j.Logger

class LoadExamples {

	private final String SCRIPT_NAME = "LoadExamples.groovy"

	private final String LOAD_PACKAGE_SCRIPT_NAME = "LoadPackage.groovy"


	private final String SCHEDULER_HOME
	private final String EXAMPLES_ZIP_PATH
	private final String EXAMPLES_DIR_PATH
	private final String TOOLS_DIR
	private final GroovyObject package_loader

	private logger = Logger.getLogger("org.ow2.proactive.scheduler")


	LoadExamples(binding) {

		// Bindings
		this.SCHEDULER_HOME = binding.variables.get("pa.scheduler.home")

		// User variables
		this.EXAMPLES_ZIP_PATH = new File(this.SCHEDULER_HOME, "samples/proactive-examples.zip").absolutePath
		this.TOOLS_DIR = new File(this.SCHEDULER_HOME, "tools")

		// Deduced variables
		this.EXAMPLES_DIR_PATH = this.EXAMPLES_ZIP_PATH.substring(0,this.EXAMPLES_ZIP_PATH.lastIndexOf("."))

		// Create a new instance of the package loader
		File load_package_script = new File(this.TOOLS_DIR, this.LOAD_PACKAGE_SCRIPT_NAME)
		if (load_package_script.exists()) {
			GroovyClassLoader gcl = new GroovyClassLoader()
			Class loadPackageClass = gcl.parseClass(load_package_script)
			this.package_loader= (GroovyObject) loadPackageClass.newInstance(binding)
		} else {
			return
		}
	}


	def unzipFile(src, dest) {
		def zipFile = new ZipFile(src)
		zipFile.entries().each { it ->
			def path = Paths.get(dest + "/" + it.name)
			if(it.directory){
				Files.createDirectories(path)
			}
			else {
				def parentDir = path.getParent()
				if (!Files.exists(parentDir)) {
					Files.createDirectories(parentDir)
				}
				Files.copy(zipFile.getInputStream(it), path)
			}
		}
	}

	def writeToOutput(String output) {
		logger.info("[" + this.SCRIPT_NAME + "] " + output)
	}


	def run() {
		writeToOutput(" Automatic deployment of proactive packages ...")

		writeToOutput(" Variables : ")
		writeToOutput(" EXAMPLES_ZIP_PATH " + this.EXAMPLES_ZIP_PATH)
		writeToOutput(" EXAMPLES_DIR_PATH " + this.EXAMPLES_DIR_PATH)

		writeToOutput(" Actions : ")

		// If the unzipped dir already exists, stop the script execution
		def examples_dir = new File(this.EXAMPLES_DIR_PATH)
		if (examples_dir.exists())
		{
			writeToOutput(this.EXAMPLES_DIR_PATH + " already exists, delete it to redeploy packages.")
			writeToOutput("Terminated.")
			return
		}

		// Unzip the examples
		def examples_zip = new File(this.EXAMPLES_ZIP_PATH)
		if (!examples_zip.exists())
		{
			writeToOutput(this.EXAMPLES_ZIP_PATH + " not found!")
			return

		}
		unzipFile(examples_zip, this.EXAMPLES_DIR_PATH)
		writeToOutput(this.EXAMPLES_ZIP_PATH + " extracted!")


		//retrieve all (package, bucket) pairs
		def package_bucket_map = [:]
		examples_dir.eachFile() { package_dir ->
			def metadata_file = new File(package_dir.absolutePath, "METADATA.json")
			if (metadata_file.exists()) {
				def slurper = new groovy.json.JsonSlurper()
				def metadata_file_map = (Map) slurper.parseText(metadata_file.text)
				def bucket_name = metadata_file_map.get("catalog").get("bucket")
				package_bucket_map.put(package_dir, bucket_name)
			}
		}

		// Retrieve the ordered bucket list
		def ordered_bucket_list = []
		def ordered_bucket_list_file = new File(examples_dir, "ordered_bucket_list")
		ordered_bucket_list_file.text.split(",").each { bucket_name ->
			ordered_bucket_list.add(bucket_name)
			// Define a specific filter on package directories to only consider those targeting the current bucket
			def file_filter = new FileFilter() {
				boolean accept(File package_dir) {
					//def package_dir = new File(pathname)
					def metadata_file = new File(package_dir.absolutePath, "METADATA.json")
					if (metadata_file.exists()) {
						// From json to map
						def slurper = new groovy.json.JsonSlurper()
						def metadata_file_map = (Map) slurper.parseText(metadata_file.text)
						return bucket_name == metadata_file_map.get("catalog").get("bucket")
					}
					return false
				}
			}

			// For each package targeting the current bucket call the package loader class
{'fraction_non_alphanumeric': 0.10257955641272902, 'fraction_numerical': 0.0007232401157184186, 'mean_word_length': 4.192115143929913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2235152', 'n_tokens_mistral': 3149, 'n_tokens_neox': 2729, 'n_words': 578}