<commit_before>// Copyright 2011 The MOE Authors All Rights Reserved.

package com.google.devtools.moe.client.directives;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.devtools.moe.client.AppContext;
import com.google.devtools.moe.client.CommandRunner.CommandException;
import com.google.devtools.moe.client.MoeOptions;
import com.google.devtools.moe.client.Ui.Task;
import com.google.devtools.moe.client.codebase.Codebase;
import com.google.devtools.moe.client.codebase.CodebaseCreationError;
import com.google.devtools.moe.client.parser.Parser;
import com.google.devtools.moe.client.parser.Parser.ParseError;
import com.google.devtools.moe.client.project.InvalidProject;
import com.google.devtools.moe.client.project.ProjectContext;

import org.kohsuke.args4j.Option;

/**
 * Print the head revision of a repository.
 *
 * @author <EMAIL> (<NAME>)
 */
public class CreateCodebaseDirective implements Directive {

  private final CreateCodebaseOptions options = new CreateCodebaseOptions();

  public CreateCodebaseDirective() {}

  @Override
  public CreateCodebaseOptions getFlags() {
    return options;
  }

  @Override
  public int perform() {
    ProjectContext context;
    try {
      context = AppContext.RUN.contextFactory.makeProjectContext(options.configFilename);
    } catch (InvalidProject e) {
      AppContext.RUN.ui.error(e, "Error creating project");
      return 1;
    }

    Task createCodebaseTask =
        AppContext.RUN.ui.pushTask("create_codebase", "Creating codebase " + options.codebase);
    Codebase c;
    try {
      c = Parser.parseExpression(options.codebase).createCodebase(context);
    } catch (ParseError e) {
      AppContext.RUN.ui.error(e, "Error creating codebase");
      return 1;
    } catch (CodebaseCreationError e) {
      AppContext.RUN.ui.error(e, "Error creating codebase");
      return 1;
    }
    AppContext.RUN.ui.info(
        String.format("Codebase \"%s\" created at %s", c.toString(), c.getPath()));

    try {
      maybeWriteTar(c);
    } catch (CommandException e) {
      AppContext.RUN.ui.error(e, "Error creating codebase tarfile");
      return 1;
    }

    AppContext.RUN.ui.popTaskAndPersist(createCodebaseTask, c.getPath());
    return 0;
  }

  /**
   * If the user specified --tarfile, then tar up the codebase at the specified location.
   * @throws CommandException 
   */
  private void maybeWriteTar(Codebase codebase)
      throws CommandException {
    Preconditions.checkNotNull(codebase);
    String tarfilePath = options.tarfile;
    if (Strings.isNullOrEmpty(tarfilePath)) {
      return;
    }

    AppContext.RUN.cmd.runCommand(
        "tar",
        ImmutableList.of("-c", "-f", tarfilePath, "."),
        codebase.getPath().getAbsolutePath());
    AppContext.RUN.ui.info(
        String.format("tar of codebase \"%s\" created at %s", codebase.toString(), tarfilePath));
  }

  static class CreateCodebaseOptions extends MoeOptions {<commit_msg>Set a dummy mtime, owner, and group on files in the generated tar.<commit_after>// Copyright 2011 The MOE Authors All Rights Reserved.

package com.google.devtools.moe.client.directives;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.devtools.moe.client.AppContext;
import com.google.devtools.moe.client.CommandRunner.CommandException;
import com.google.devtools.moe.client.MoeOptions;
import com.google.devtools.moe.client.Ui.Task;
import com.google.devtools.moe.client.codebase.Codebase;
import com.google.devtools.moe.client.codebase.CodebaseCreationError;
import com.google.devtools.moe.client.parser.Parser;
import com.google.devtools.moe.client.parser.Parser.ParseError;
import com.google.devtools.moe.client.project.InvalidProject;
import com.google.devtools.moe.client.project.ProjectContext;

import org.kohsuke.args4j.Option;

/**
 * Print the head revision of a repository.
 *
 * @author <EMAIL> (<NAME>)
 */
public class CreateCodebaseDirective implements Directive {

  private final CreateCodebaseOptions options = new CreateCodebaseOptions();

  public CreateCodebaseDirective() {}

  @Override
  public CreateCodebaseOptions getFlags() {
    return options;
  }

  @Override
  public int perform() {
    ProjectContext context;
    try {
      context = AppContext.RUN.contextFactory.makeProjectContext(options.configFilename);
    } catch (InvalidProject e) {
      AppContext.RUN.ui.error(e, "Error creating project");
      return 1;
    }

    Task createCodebaseTask =
        AppContext.RUN.ui.pushTask("create_codebase", "Creating codebase " + options.codebase);
    Codebase c;
    try {
      c = Parser.parseExpression(options.codebase).createCodebase(context);
    } catch (ParseError e) {
      AppContext.RUN.ui.error(e, "Error creating codebase");
      return 1;
    } catch (CodebaseCreationError e) {
      AppContext.RUN.ui.error(e, "Error creating codebase");
      return 1;
    }
    AppContext.RUN.ui.info(
        String.format("Codebase \"%s\" created at %s", c.toString(), c.getPath()));

    try {
      maybeWriteTar(c);
    } catch (CommandException e) {
      AppContext.RUN.ui.error(e, "Error creating codebase tarfile");
      return 1;
    }

    AppContext.RUN.ui.popTaskAndPersist(createCodebaseTask, c.getPath());
    return 0;
  }

  /**
   * If the user specified --tarfile, then tar up the codebase at the specified location.
   * @throws CommandException 
   */
  private void maybeWriteTar(Codebase codebase)
      throws CommandException {
    Preconditions.checkNotNull(codebase);
    String tarfilePath = options.tarfile;
    if (Strings.isNullOrEmpty(tarfilePath)) {
      return;
    }

    AppContext.RUN.cmd.runCommand(
        "tar",
        ImmutableList.of(
            "--mtime=1970-01-01", "--owner=0", "--group=0", "-c", "-f", tarfilePath, "."),
        codebase.getPath().getAbsolutePath());
    AppContext.RUN.ui.info(
        String.format("tar of codebase \"%s\" created at %s", codebase.toString(), tarfilePath));
  }

  static class CreateCodebaseOptions extends MoeOptions {
{'fraction_non_alphanumeric': 0.11834032935098482, 'fraction_numerical': 0.004843396835647401, 'mean_word_length': 3.671945701357466, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4443912', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1875, 'n_words': 394}