<commit_before>// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var Promise = require('promise');

var os = require('os');
var fs = require('fs');
var path = require('path');
var childProcess = require('child_process');

var ComparisonAdapter = require('./comparisonAdapter');

var tmpCounter = 0;

/**
 * Shell comparison adapter
 *
 * @class ShellAdapter
 * @extends ComparisonAdapter
 */
var ShellAdapter = ComparisonAdapter.extend(

	/** @lends ShellAdapter.prototype */
	{
		/**
		 * Generates a unique temp. path
		 *
		 * @method _generateTempPath
		 * @param {string} id
		 * @return {string}
		 * @private
		 */
		_generateTempPath: function (id) {
			tmpCounter++;
			return path.join(os.tmpDir(), id + +(new Date())) + '_' + tmpCounter + '.png';
		},


		/**
		 * Gets the selected environment variables
		 *
		 * @method getEnv
		 * @return {object}
		 */
		getEnv: function () {
			return this.getOptions().env || {};
		},

		/**
		 * Gets the current working directory where the comparison should be executed
		 *
		 * @method getCwd
		 * @return {string}
		 */
		getCwd: function () {
			return this.getOptions().cwd;
		},

		/**
		 * Gets the command to execute for comparison
		 *
		 * @method getCommand
		 * @return {string}
		 */
		getCommand: function () {
			return this.getOptions().cmd;
		},


		/**
		 * Runs the comparison
		 *
		 * @method run
		 * @return {Promise} With {boolean} for success/failure
		 */
		run : function () {
			return new Promise(function (resolve, reject) {

				var imageA = this.getImageA(),
					pathA = this._generateTempPath('A'),
					imageB = this.getImageB(),
					pathB = this._generateTempPath('B');

				if (!imageA) {
					throw new Error('First image is not given.');
				}
				if (!imageB) {
					throw new Error('Second image is not given.');
				}

				imageA.writeImage(pathA, function (err) {

					if (err) {
						reject(err);

					} else {

						imageB.writeImage(pathB, function (err) {

							var cwd, env, cmd, proc, result = { code: null };

							if (err) {
								reject(err);

							} else {

								cwd = this.getCwd();
								env = this.getEnv();
								cmd = this.getCommand();

								if (!cwd && !fs.existsSync(cwd)) {
									throw new Error("The working directory doesn't exist: " + cwd);
								}
								if (!cmd) {
									throw new Error("Command wasn't defined.");
								}

								proc = childProcess.exec(this.getCommand(), {
									cwd: cwd,
									env: env,
									maxBuffer: 5 * 1024 * 1024

								}, function (error, stdout) {

									var image;

									if (error) {
										reject(error);

									} else {

										image = PNGImage.loadImage(stdout, function (error) {
<commit_msg>Fail fast on shell comparison adapter<commit_after>// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var Promise = require('promise');

var os = require('os');
var fs = require('fs');
var path = require('path');
var childProcess = require('child_process');

var ComparisonAdapter = require('./comparisonAdapter');

var tmpCounter = 0;

/**
 * Shell comparison adapter
 *
 * @class ShellAdapter
 * @extends ComparisonAdapter
 */
var ShellAdapter = ComparisonAdapter.extend(

	/** @lends ShellAdapter.prototype */
	{
		/**
		 * Generates a unique temp. path
		 *
		 * @method _generateTempPath
		 * @param {string} id
		 * @return {string}
		 * @private
		 */
		_generateTempPath: function (id) {
			tmpCounter++;
			return path.join(os.tmpDir(), id + +(new Date())) + '_' + tmpCounter + '.png';
		},


		/**
		 * Gets the selected environment variables
		 *
		 * @method getEnv
		 * @return {object}
		 */
		getEnv: function () {
			return this.getOptions().env || {};
		},

		/**
		 * Gets the current working directory where the comparison should be executed
		 *
		 * @method getCwd
		 * @return {string}
		 */
		getCwd: function () {
			return this.getOptions().cwd;
		},

		/**
		 * Gets the command to execute for comparison
		 *
		 * @method getCommand
		 * @return {string}
		 */
		getCommand: function () {
			return this.getOptions().cmd;
		},


		/**
		 * Runs the comparison
		 *
		 * @method run
		 * @return {Promise} With {boolean} for success/failure
		 */
		run : function () {
			return new Promise(function (resolve, reject) {

				var imageA = this.getImageA(),
					pathA,
					imageB = this.getImageB(),
					pathB,
					cwd, env, cmd;

				cwd = this.getCwd();
				env = this.getEnv();
				cmd = this.getCommand();

				if (!imageA) {
					throw new Error('First image is not given.');
				}
				if (!imageB) {
					throw new Error('Second image is not given.');
				}

				if (!cwd && !fs.existsSync(cwd)) {
					throw new Error("The working directory doesn't exist: " + cwd);
				}
				if (!cmd) {
					throw new Error("Command wasn't defined.");
				}

				pathA = this._generateTempPath('A');
				imageA.writeImage(pathA, function (err) {

					if (err) {
						reject(err);

					} else {

						pathB = this._generateTempPath('B');
						imageB.writeImage(pathB, function (err) {

							var proc, result = { code: null };

							if (err) {
								reject(err);

							} else {

								proc = childProcess.exec(this.getCommand(), {
									cwd: cwd,
									env: env,
									maxBuffer: 5 * 1024 * 1024

								}, function (error, stdout) {

									var image;

									if (error) {
										reject(error);

									} else {

										image = PNGImage.loadImage(stdout, function (error) {

{'fraction_non_alphanumeric': 0.1406837300877036, 'fraction_numerical': 0.005011634150706998, 'mean_word_length': 2.4052407068860453, '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': '233504', 'n_tokens_mistral': 2314, 'n_tokens_neox': 1667, 'n_words': 479}