<commit_before>'use strict';

const fse = require('fs-extra');
const childProcess = require('child_process');
const temp = require('temp').track();
const path = require('path');
const should = require('should/as-function');

/**
 * Sandbox
 * =======
 *
 * This class implements a dummy sandbox, i.e. a sandbox meant to test the
 * system (it exposes all the methods and properties needed by other services)
 * without the constraint of having to be secure (so it works everywhere, not
 * just on special system configurations).
 *
 * NOTE: no real world evaluator should ever use this sandbox.
 *
 *
 * API
 * ---
 *
 * In order to run a command inside the sandbox, a service will need to use the
 * following API. The API allows to initialize a new sandbox (when a Sandbox
 * object is created), to copy files inside the sandbox, and to execute them.
 *
 */
module.exports = class Sandbox {
  /**
   * Initializes a new Sandbox object by creating a temporary directory in the
   * local filesystem. This temporary directory will be used for all operations
   * and will be automatically deleted.
   *
   * @todo Check if the temp.track() command actually deletes the directory
   *       (it advertises that it does, but it doesn't seem to do it on my PC,
   *       maybe we need to introduce a Sandbox.delete() method).
   * @constructor
   */
  constructor() {
    this._tempdir = temp.mkdirSync('sandbox');
    this._timeLimit = null;
    this._memoryLimit = null;
    this._stdin = null;
    this._stdout = null;
    this._stderr = null;
  }

  /**
   * This method takes a file path and a file name. It makes a copy of the file
   * located at the specified path, and puts the copy into the sandbox with a
   * new name.
   *
   * @param {string} the absolute path of the file to copy.
   * @param {?string} the new name of the file. If not specified, the name is
                      left unchanged.
   */
  add(filename, newFilename) {
    // Sanity checks
    should(filename).be.a.String();
    should(fse.statSync(filename).isFile()).be.true();
    if (newFilename) {
      should(newFilename).be.a.String();
      should(newFilename.length).be.above(0);
    }

    try {
      newFilename = newFilename || path.basename(filename);
      newFilename = path.join(this._tempdir, newFilename);
      fse.copySync(filename, newFilename);
    } catch (err) {
      console.error(err);
    }
<commit_msg>Add missing sandbox methods to dummy.js<commit_after>'use strict';

const fse = require('fs-extra');
const childProcess = require('child_process');
const temp = require('temp').track();
const path = require('path');
const should = require('should/as-function');

/**
 * Sandbox
 * =======
 *
 * This class implements a dummy sandbox, i.e. a sandbox meant to test the
 * system (it exposes all the methods and properties needed by other services)
 * without the constraint of having to be secure (so it works everywhere, not
 * just on special system configurations).
 *
 * NOTE: no real world evaluator should ever use this sandbox.
 *
 *
 * API
 * ---
 *
 * In order to run a command inside the sandbox, a service will need to use the
 * following API. The API allows to initialize a new sandbox (when a Sandbox
 * object is created), to copy files inside the sandbox, and to execute them.
 *
 */
module.exports = class Sandbox {
  /**
   * Initializes a new Sandbox object by creating a temporary directory in the
   * local filesystem. This temporary directory will be used for all operations
   * and will be automatically deleted.
   *
   * @todo Check if the temp.track() command actually deletes the directory
   *       (it advertises that it does, but it doesn't seem to do it on my PC,
   *       maybe we need to introduce a Sandbox.delete() method).
   * @constructor
   */
  constructor() {
    this._tempdir = temp.mkdirSync('sandbox');
    this._timeLimit = null;
    this._memoryLimit = null;
    this._stdin = null;
    this._stdout = null;
    this._stderr = null;
  }

  /**
   * Deletes the sandbox. Functions of this object shouldn't be called after
   * this
   */
  teardown() {
    fse.removeSync(this._tempdir);
  }

  /**
   * Returns the absolute path to the root directory of the sandbox.
   *
   * @return {string} the path to the sandbox directory
   */
  getRoot() {
    return this._tempdir;
  }

  /**
   * This method takes a file path and a file name. It makes a copy of the file
   * located at the specified path, and puts the copy into the sandbox with a
   * new name.
   *
   * @param {string} the absolute path of the file to copy.
   * @param {?string} the new name of the file. If not specified, the name is
                      left unchanged.
   */
  add(filename, newFilename) {
    // Sanity checks
    should(filename).be.a.String();
    should(fse.statSync(filename).isFile()).be.true();
    if (newFilename) {
      should(newFilename).be.a.String();
      should(newFilename.length).be.above(0);
    }

    try {
      newFilename = newFilename || path.basename(filename);
      newFilename = path.join(this._tempdir, newFilename);
      fse.copySync(filename, newFilename);
    } catch (err) {
      console.error(err);
    }

{'fraction_non_alphanumeric': 0.10460412251974571, 'fraction_numerical': 0.00038528221922558276, 'mean_word_length': 2.9512937595129376, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2199963', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1468, 'n_words': 587}