<commit_before>var SSH = require('ssh2'),
    events = require('events'),
    path = require('path'),
    util = require('util');

/**** ssh is an event emitter ****/
util.inherits(ssh, events.EventEmitter);

/**** Finder prototype ****/
var proto = ssh.prototype;

/**** Expose ssh ****/
exports = module.exports = ssh;

/**** ssh constructor ****/
function ssh(options) {

  if (!(this instanceof ssh)) {
    return new ssh(options);
  }

  events.EventEmitter.call(this);

  util._extend(this, options || {});

  this.ssh = new SSH();

  this.ssh.on('ready', this.handleReady.bind(this));
  this.ssh.on('error', this.handleError.bind(this));
  this.ssh.connect({
    username: this.username,
    password: <PASSWORD>,
    host: this.host,
    port: this.port
  });

}

proto.ssh = false;
proto.stdin = false;
proto.username = 'pi';
proto.password = '<PASSWORD>';
proto.host = '10.0.1.1';
proto.port = 22;
proto.type = 'shell';
proto.install_command = 'curl -SLs http://bootstrap.uniontownlabs.org/install | sudo bash';
proto.pi_config = {};

proto.handleError = function(err) {
  this.emit('error', err.toString());
};

proto.handleData = function(data) {
  this.emit('data', data.toString());
};

proto.handleClose = function() {
  this.emit('done');
};

proto.write = function(data) {

  if(! this.stdin) {
    return;
  }

  this.stdin.write(data);

};

proto.end = function() {

  if(! this.stdin) {
    return;
  }

  this.ssh.end();

};

proto.handleReady = function() {

  this[this.type].call(this);

};

proto.shutdown = function() {

  this.ssh.exec('sudo shutdown now', function(err, stream) {

    if(err) {
      return this.handleError(err);
    }

    this.stdin = stream;

    stream.on('error', this.handleError.bind(this));
    stream.on('data', this.handleData.bind(this));
    stream.on('close', this.handleClose.bind(this));
    stream.stderr.on('data', this.handleError.bind(this));

  }.bind(this));

};

proto.upload = function() {

  this.ssh.sftp(function(err, sftp) {

    if(err) {
      return this.handleError(err);
    }

    var dest = '/home/' + this.username + '/' + path.basename(this.file_upload);

    var progress = function(uploaded, chunk, total) {
      var percent = ((uploaded / total) * 100).toFixed(2);
      this.handleData(percent + '% complete');
    }.bind(this);

    sftp.fastPut(this.file_upload, dest, { step: progress }, function(err) {

      if(err) {
        return this.handleError(err);
      }

      this.emit('uploaded');

    }.bind(this));

  }.bind(this));

};

proto.bootstrap = function() {

  var opts = {
    pty: {
      rows: 31,
      cols: 80,
      height: 384,<commit_msg>finder: reset terminal before upload percent change<commit_after>var SSH = require('ssh2'),
    events = require('events'),
    path = require('path'),
    util = require('util');

/**** ssh is an event emitter ****/
util.inherits(ssh, events.EventEmitter);

/**** Finder prototype ****/
var proto = ssh.prototype;

/**** Expose ssh ****/
exports = module.exports = ssh;

/**** ssh constructor ****/
function ssh(options) {

  if (!(this instanceof ssh)) {
    return new ssh(options);
  }

  events.EventEmitter.call(this);

  util._extend(this, options || {});

  this.ssh = new SSH();

  this.ssh.on('ready', this.handleReady.bind(this));
  this.ssh.on('error', this.handleError.bind(this));
  this.ssh.connect({
    username: this.username,
    password: <PASSWORD>,
    host: this.host,
    port: this.port
  });

}

proto.ssh = false;
proto.stdin = false;
proto.username = 'pi';
proto.password = '<PASSWORD>';
proto.host = '10.0.1.1';
proto.port = 22;
proto.type = 'shell';
proto.install_command = 'curl -SLs http://bootstrap.uniontownlabs.org/install | sudo bash';
proto.pi_config = {};

proto.handleError = function(err) {
  this.emit('error', err.toString());
};

proto.handleData = function(data) {
  this.emit('data', data.toString());
};

proto.handleClose = function() {
  this.emit('done');
};

proto.write = function(data) {

  if(! this.stdin) {
    return;
  }

  this.stdin.write(data);

};

proto.end = function() {

  if(! this.stdin) {
    return;
  }

  this.ssh.end();

};

proto.handleReady = function() {

  this[this.type].call(this);

};

proto.shutdown = function() {

  this.ssh.exec('sudo shutdown now', function(err, stream) {

    if(err) {
      return this.handleError(err);
    }

    this.stdin = stream;

    stream.on('error', this.handleError.bind(this));
    stream.on('data', this.handleData.bind(this));
    stream.on('close', this.handleClose.bind(this));
    stream.stderr.on('data', this.handleError.bind(this));

  }.bind(this));

};

proto.upload = function() {

  this.ssh.sftp(function(err, sftp) {

    if(err) {
      return this.handleError(err);
    }

    var dest = '/home/' + this.username + '/' + path.basename(this.file_upload);

    var progress = function(uploaded, chunk, total) {
      var percent = ((uploaded / total) * 100).toFixed(2);
      this.handleData('\x1bc');
      this.handleData(percent + '% complete\n');
    }.bind(this);

    sftp.fastPut(this.file_upload, dest, { step: progress }, function(err) {

      if(err) {
        return this.handleError(err);
      }

      this.emit('uploaded');

    }.bind(this));

  }.bind(this));

};

proto.bootstrap = function() {

  var opts = {
    pty: {
      rows: 31,
      cols: 80,
      height: 384,
{'fraction_non_alphanumeric': 0.1908041697691735, 'fraction_numerical': 0.007259865971705138, 'mean_word_length': 3.220738413197172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7180204', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1821, 'n_words': 348}