<commit_before>/**
 * Module to wrap and abstract access to boot2docker.
 * @module b2d
 */

'use strict';

var EventEmitter = require('events').EventEmitter;
var events = new EventEmitter();
module.exports.events = events;

var fs = require('fs');
var deps = require('./deps.js');
var kenv = require('./kEnv.js');

var B2D_EXECUTABLE = 'boot2docker';

function getShell() {
  return deps.lookup('shell');
}

var isInstalled = function(callback) {
  var cmd = 'which boot2docker';
  getShell().exec(cmd, function(err, output) {
    if (!err && output) {
      callback(err, true);
    } else if (err && !output) {
      callback(null, false);
    } else {
      callback(err, false);
    }
  });
};
module.exports.isInstalled = isInstalled;

function verifyB2dInstalled(callback) {
  isInstalled(function(err, isIntalled) {
    if (err) {
      throw err;
    } else if (!isInstalled) {
      throw new Error('Boot2docker is NOT installed!');
    } else {
      callback();
    }
  });
}

var hasProfile = function(callback) {
  var B2D_PROFILE = deps.lookup('config').sysConfRoot + '/b2d.profile';
  if (fs.existsSync(B2D_PROFILE)) {
    kenv.setB2dProf(B2D_PROFILE);
    callback(true);
  } else {
    callback(false);
  }
};
module.exports.hasProfile = hasProfile;

function verifyB2dProfile(callback) {
  hasProfile(function(hasProfile) {
    if (hasProfile) {
      callback();
    } else {
      throw new Error('You need a Boot2Docker profile. Try kbox install!');
    }
  });
}

function _execB2dCommand(cmd, callback) {
  getShell().exec(B2D_EXECUTABLE + ' ' + cmd, function(err, output) {
    if (err) {
      if (callback) {
        callback(err, output);
      }
      else {
        throw err;
      }
    } else {
      callback(err, output);
    }
  });
}

function execB2dCommand(cmd, callback) {
  verifyB2dInstalled(function() {
    verifyB2dProfile(function() {
      _execB2dCommand(cmd, callback);
    });
  });
}

/**
 * Stops VM.
 * @arg {function} callback - Callback function called on completion
 * @example
 * var b2d = require(...);
 * b2d.down(function() {
 *   console.log('VM stopped!');
 * });
 */
var down = function(b2d, attempts, callback) {
  b2d.events.emit('pre-down');
  execB2dCommand('down', function(err, output) {
    if (err && attempts === 0) {
      throw err;
    }
    else if (err && attempts > 0) {
      --attempts;
      down(b2d, attempts, callback);
    }
    else {
      b2d.events.emit('post-down');
      callback();
    }
  });
};
module.exports.down = down;

/**
 * Starts VM.
 * @arg {function} callback - Callback function called on completion
 * @example
 * var b2d = require(...);
 * b2d.up(function() {
 *   console.log('VM is now running!');
 * });
 */
var up = function(b2d, attempts, callback) {
  b2d.events.emit('pre-up');
  execB2dCommand('up', function(err, output) {
    if (err && attempts === 0) {
      throw err;
    }
    else if (err && attempts > 0) {
      --attempts;
      up(b2d, attempts, callback);
    }
    else {
      b2d.events.emit('post-up');
      callback();
    }
  });
};
module.exports.up = up;

function removeTrailing(src, s) {
  var end = src.substr(src.length - s.length, s.length);
  if (end === s) {
    return src.substr(0, src.length - s.length);
  } else {
    return src;
  }
}

function removeTrailingNewline(src) {
  return removeTrailing(src, '\n');
}

/**
 * Queries boot2docker for VM host's status.
 * @arg {function} callback - Callback function called on completion
 * @returns {string} status - Status of VM host<commit_msg>#106: Cleaning things up via BENLINT<commit_after>/**
 * Module to wrap and abstract access to boot2docker.
 * @module b2d
 */

'use strict';

var EventEmitter = require('events').EventEmitter;
var events = new EventEmitter();
module.exports.events = events;

var fs = require('fs');
var path = require('path');
var deps = require('./deps.js');
var kenv = require('./kEnv.js');

var B2D_EXECUTABLE = 'boot2docker';

function getShell() {
  return deps.lookup('shell');
}

var isInstalled = function(callback) {
  var cmd = 'which boot2docker';
  getShell().exec(cmd, function(err, output) {
    if (!err && output) {
      callback(err, true);
    } else if (err && !output) {
      callback(null, false);
    } else {
      callback(err, false);
    }
  });
};
module.exports.isInstalled = isInstalled;

function verifyB2dInstalled(callback) {
  isInstalled(function(err, isIntalled) {
    if (err) {
      throw err;
    } else if (!isInstalled) {
      throw new Error('Boot2docker is NOT installed!');
    } else {
      callback();
    }
  });
}

var hasProfile = function(callback) {
  var B2D_PROFILE = path.join(deps.lookup('config').sysConfRoot, '/b2d.profile');
  if (fs.existsSync(B2D_PROFILE)) {
    kenv.setB2dProf(B2D_PROFILE);
    callback(true);
  } else {
    callback(false);
  }
};
module.exports.hasProfile = hasProfile;

function verifyB2dProfile(callback) {
  hasProfile(function(hasProfile) {
    if (hasProfile) {
      callback();
    } else {
      throw new Error('You need a Boot2Docker profile. Try kbox install!');
    }
  });
}

function _execB2dCommand(cmd, callback) {
  getShell().exec(B2D_EXECUTABLE + ' ' + cmd, function(err, output) {
    if (err) {
      if (callback) {
        callback(err, output);
      }
      else {
        throw err;
      }
    } else {
      callback(err, output);
    }
  });
}

function execB2dCommand(cmd, callback) {
  verifyB2dInstalled(function() {
    verifyB2dProfile(function() {
      _execB2dCommand(cmd, callback);
    });
  });
}

var _down = function(b2d, attempts, callback) {
  execB2dCommand('down', function(err, output) {
    if (err && attempts === 0) {
      throw err;
    }
    else if (err && attempts > 0) {
      _down(b2d, --attempts, callback);
    }
    else {
      b2d.events.emit('post-down');
      callback();
    }
  });
};

/**
 * Stops VM.
 * @arg {function} callback - Callback function called on completion
 * @example
 * var b2d = require(...);
 * b2d.down(function() {
 *   console.log('VM stopped!');
 * });
 */
module.exports.down = function(b2d, attempts, callback) {
  b2d.events.emit('pre-down');
  _down(b2d, attempts, callback);
};

var _up = function(b2d, attempts, callback) {
  execB2dCommand('up', function(err, output) {
    if (err && attempts === 0) {
      throw err;
    }
    else if (err && attempts > 0) {
      _up(b2d, --attempts, callback);
    }
    else {
      b2d.events.emit('post-up');
      callback();
    }
  });
};

/**
 * Starts VM.
 * @arg {function} callback - Callback function called on completion
 * @example
 * var b2d = require(...);
 * b2d.up(function() {
 *   console.log('VM is now running!');
 * });
 */
module.exports.up = function(b2d, attempts, callback) {
  b2d.events.emit('pre-up');
  _up(b2d, attempts, callback);
};

function removeTrailing(src, s) {
  var end = src.substr(src.length - s.length, s.length);
  if (end === s) {
    return src.substr(0, src.length - s.length);
  } else {
    return src;
  }
}

function removeTrailingNewline(src) {
  return removeTrailing(src, '\n');
}

/**
 * Queries boot2docker for VM host's status.
 * @arg {function} callback - Callback function called on completion
 * @returns {string} status - Status of VM host
{'fraction_non_alphanumeric': 0.1606674941387395, 'fraction_numerical': 0.011998345055854365, 'mean_word_length': 3.0066298342541438, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7334920', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2501, 'n_words': 573}