<commit_before>package pro.savant.circumflex
package cluster

import java.io._
import core._, xml._, cache._
import scala.collection.mutable.HashMap
import collection.JavaConversions._
import java.util.regex.Pattern
import java.util.Properties
import java.util.jar._
import org.apache.commons.io.{IOUtils, FileUtils}

class Cluster(val project: Project)
    extends StructHolder
    with XmlFile { cluster =>

  def elemName = "cluster"

  def baseDir = project.baseDir

  val clusterDir = new File(baseDir, "src/cluster")

  def descriptorFile = new File(clusterDir, "cluster.xml")

  val _id = attr("id")
  def id = _id.getOrElse("")

  val _mainClass = text("main-class")
  def mainClass = _mainClass.getOrElse(
    throw new IllegalArgumentException("<main-class> tag not specified."))

  val resources = new ClusterResources(this)

  val servers = new ListHolder[Server] {

    def elemName = "servers"

    def read = {
      case "server" => new Server(cluster)
    }

  }

  def getServer(id: String) = servers.children.find(_.id == id)

  def server(id: String) = getServer(id).get

  def nodes: Seq[Node] = servers.children.flatMap(_.children)

  def getNode(id: String) = {
    var _id = id
    if (_id.startsWith(cluster.id + "-"))
      _id = _id.substring(cluster.id.length + 1)
    nodes.find(_.toString == cluster.id + "-" + _id)
  }

  def node(id: String) = getNode(id).get

  override def toString = id

  val classesDir = new File(baseDir, "target/classes")
  val dependencyDir = new File(baseDir, "target/lib")
  val targetDir = new File(baseDir, "target/cluster")
  val workDir = new File(targetDir, "work")

  val _mainCxProps = new CacheCell[PropsFile](
    new PropsFile(new File(classesDir, "cx.properties")))
  def mainCxProps = _mainCxProps.get.toMap

  val _clusterCxProps = new CacheCell[PropsFile](
    new PropsFile(new File(clusterDir, "cx.properties")))
  def clusterCxProps = _clusterCxProps.get.toMap

  /*! All nodes are built one by one, using work directory as the root of their
  classes. */
  def buildAll() {
    if (!classesDir.isDirectory) {
      CL_LOG.info("Rebuilding the project with Maven.")
      project.rootProject.mvn("clean", "install").execute().join()
      CL_LOG.info("Project built successfully.")
    }
    servers.children.foreach(_.buildAll())
  }

  // Load on instantiate
  load()

}

class ClusterResources(val cluster: Cluster)
    extends StructHolder { res =>

  def elemName = "resources"

  val _dir = attr("dir")
  val dir = new File(cluster.clusterDir, _dir.getOrElse("resources"))

  val _filter = attr("filter")
  val filterPattern = try {
    Pattern.compile(_filter())
  } catch {
    case e: Exception =>
      DEFAULT_FILTER_PATTERN
  }

}

class Server(val cluster: Cluster)
    extends ListHolder[Node] { server =>

  def elemName = "server"

  def project = cluster.project

  val _id = attr("id")
  def id = _id.get.orElse(address.get).getOrElse("")

  val address = attr("address")

  val user = attr("user")

  val dir = attr("dir")

  def read = {
    case "node" => new Node(server)
  }

  def getNode(name: String) = children.find(_.name() == name)

  def node(name: String) = getNode(name).get

  override def toString = user() + "@" + address() + ":" + dir()<commit_msg>Removed dir attribute from server.<commit_after>package pro.savant.circumflex
package cluster

import java.io._
import core._, xml._, cache._
import scala.collection.mutable.HashMap
import collection.JavaConversions._
import java.util.regex.Pattern
import java.util.Properties
import java.util.jar._
import org.apache.commons.io.{IOUtils, FileUtils}

class Cluster(val project: Project)
    extends StructHolder
    with XmlFile { cluster =>

  def elemName = "cluster"

  def baseDir = project.baseDir

  val clusterDir = new File(baseDir, "src/cluster")

  def descriptorFile = new File(clusterDir, "cluster.xml")

  val _id = attr("id")
  def id = _id.getOrElse("")

  val _mainClass = text("main-class")
  def mainClass = _mainClass.getOrElse(
    throw new IllegalArgumentException("<main-class> tag not specified."))

  val resources = new ClusterResources(this)

  val servers = new ListHolder[Server] {

    def elemName = "servers"

    def read = {
      case "server" => new Server(cluster)
    }

  }

  def getServer(id: String) = servers.children.find(_.id == id)

  def server(id: String) = getServer(id).get

  def nodes: Seq[Node] = servers.children.flatMap(_.children)

  def getNode(id: String) = {
    var _id = id
    if (_id.startsWith(cluster.id + "-"))
      _id = _id.substring(cluster.id.length + 1)
    nodes.find(_.toString == cluster.id + "-" + _id)
  }

  def node(id: String) = getNode(id).get

  override def toString = id

  val classesDir = new File(baseDir, "target/classes")
  val dependencyDir = new File(baseDir, "target/lib")
  val targetDir = new File(baseDir, "target/cluster")
  val workDir = new File(targetDir, "work")

  val _mainCxProps = new CacheCell[PropsFile](
    new PropsFile(new File(classesDir, "cx.properties")))
  def mainCxProps = _mainCxProps.get.toMap

  val _clusterCxProps = new CacheCell[PropsFile](
    new PropsFile(new File(clusterDir, "cx.properties")))
  def clusterCxProps = _clusterCxProps.get.toMap

  /*! All nodes are built one by one, using work directory as the root of their
  classes. */
  def buildAll() {
    if (!classesDir.isDirectory) {
      CL_LOG.info("Rebuilding the project with Maven.")
      project.rootProject.mvn("clean", "install").execute().join()
      CL_LOG.info("Project built successfully.")
    }
    servers.children.foreach(_.buildAll())
  }

  // Load on instantiate
  load()

}

class ClusterResources(val cluster: Cluster)
    extends StructHolder { res =>

  def elemName = "resources"

  val _dir = attr("dir")
  val dir = new File(cluster.clusterDir, _dir.getOrElse("resources"))

  val _filter = attr("filter")
  val filterPattern = try {
    Pattern.compile(_filter())
  } catch {
    case e: Exception =>
      DEFAULT_FILTER_PATTERN
  }

}

class Server(val cluster: Cluster)
    extends ListHolder[Node] { server =>

  def elemName = "server"

  def project = cluster.project

  val _id = attr("id")
  def id = _id.get.orElse(address.get).getOrElse("")

  val address = attr("address")

  val user = attr("user")

  def read = {
    case "node" => new Node(server)
  }

  def getNode(name: String) = children.find(_.name() == name)

  def node(name: String) = getNode(name).get

  override def toString = user() + "@" + address()
{'fraction_non_alphanumeric': 0.12872962165487542, 'fraction_numerical': 0.00030759766225776686, 'mean_word_length': 3.6350677120456165, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1769073', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2060, 'n_words': 547}