<commit_before>package cook.target

import java.io.File
import java.util.Date

import org.apache.tools.ant.DirectoryScanner

import cook.util._

class Target(
    val path: String,
    val name: String,
    val cmds: Seq[String],
    val inputs: Seq[File],
    val deps: Seq[String],
    val exeCmds: Seq[String]) {

  def fullname(): String = {
    path + ":" + name
  }

  /**
   * Only executeable target can be run by "cook run"
   */
  def isExecutable = (exeCmds != null)

  /**
   * Target it self doesn't care about deps, it only care about the input and output,
   * deps will be done by Target Manager.
   */
  def build() {
    // TODO(timgreen): add cache detect
    //
    // 0. save current timestamp
    // 1. check whether all inputs is ready
    // 2. run cmd
    // 3. check whether all output is generated

    val currentTimestamp = new Date().getTime
    checkIntputs()
    if (isBuilded) {
      throw new TargetException(
          "One target should never been build twice: target \"%s\"".format(name))
    }
    run(cmds)
    isBuilded = true
  }

  def outputs(): Seq[String] = {
    if (!isBuilded) {
      throw new TargetException(
          "Outputs will become available after build: target \"%s\"".format(name))
    }

    val ds = new DirectoryScanner
    ds.setBasedir(outputDir)
    ds.scan
    ds.getIncludedFiles.toSeq
  }

  def outputDir(): File = {
    FileUtil("%s/%s/%s%s".format(Target.COOK_BUILD, path, Target.OUTPUT_PREFIX, name))
  }

  def depTargets(): Seq[TargetLabel] = {
    deps.map {
      new TargetLabel(path, _)
    }
  }

  private[target]
  def checkIntputs() {
    checkFiles(inputs)
  }

  def mkOutputDir() {
    if (!outputDir.mkdirs) {
      throw new TargetException(
          "Can not create output dir for target \"%s\": %s".format(outputDir.getPath, name))
    }
  }

  def checkFiles(files: Seq[File]) {
    for (f <- inputs) {
      if (!f.exists) {
        throw new TargetException("Required input file \"%s\" not exists".format(f.getPath))
      }
    }
  }

  def run(cmds: Seq[String]) {
    mkOutputDir
    val pb = new ProcessBuilder(cmds: _*)
    pb.directory(outputDir)
    pb.start
  }<commit_msg>put "INPUTS" "OUTPUT_DIR" in env<commit_after>package cook.target

import java.io.File
import java.util.Date

import org.apache.tools.ant.DirectoryScanner

import cook.util._

class Target(
    val path: String,
    val name: String,
    val cmds: Seq[String],
    val inputs: Seq[File],
    val deps: Seq[String],
    val exeCmds: Seq[String]) {

  def fullname(): String = {
    path + ":" + name
  }

  /**
   * Only executeable target can be run by "cook run"
   */
  def isExecutable = (exeCmds != null)

  /**
   * Target it self doesn't care about deps, it only care about the input and output,
   * deps will be done by Target Manager.
   */
  def build() {
    // TODO(timgreen): add cache detect
    //
    // 0. save current timestamp
    // 1. check whether all inputs is ready
    // 2. run cmd
    // 3. check whether all output is generated

    val currentTimestamp = new Date().getTime
    checkIntputs()
    if (isBuilded) {
      throw new TargetException(
          "One target should never been build twice: target \"%s\"".format(name))
    }
    run(cmds)
    isBuilded = true
  }

  def outputs(): Seq[String] = {
    if (!isBuilded) {
      throw new TargetException(
          "Outputs will become available after build: target \"%s\"".format(name))
    }

    val ds = new DirectoryScanner
    ds.setBasedir(outputDir)
    ds.scan
    ds.getIncludedFiles.toSeq
  }

  def outputDir(): File = {
    FileUtil("%s/%s/%s%s".format(Target.COOK_BUILD, path, Target.OUTPUT_PREFIX, name))
  }

  def depTargets(): Seq[TargetLabel] = {
    deps.map {
      new TargetLabel(path, _)
    }
  }

  private[target]
  def checkIntputs() {
    checkFiles(inputs)
  }

  def mkOutputDir() {
    if (!outputDir.mkdirs) {
      throw new TargetException(
          "Can not create output dir for target \"%s\": %s".format(outputDir.getPath, name))
    }
  }

  def checkFiles(files: Seq[File]) {
    for (f <- inputs) {
      if (!f.exists) {
        throw new TargetException("Required input file \"%s\" not exists".format(f.getPath))
      }
    }
  }

  def run(cmds: Seq[String]) {
    mkOutputDir
    val pb = new ProcessBuilder(cmds: _*)
    pb.directory(outputDir)
    val env = pb.environment
    env.put("OUTPUT_DIR", outputDir.getAbsolutePath)
    env.put("INPUTS", inputs.map(_.getAbsolutePath).mkString(" "))
    pb.start
  }
{'fraction_non_alphanumeric': 0.12693498452012383, 'fraction_numerical': 0.0017691287041132243, 'mean_word_length': 2.5530243519245874, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, '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': '1050030', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1491, 'n_words': 415}