<commit_before>var
FilePath = require('filepath').FilePath,
SSH      = require('ssh2'),
Promise  = require('./promise'),
U        = require('./u'),
Objects  = require('./objects');


exports.newSSH = Objects.factory({

  initialize: function (spec) {
    Object.defineProperties(this, {
      log: {
        value: spec.log
      },
      connection: {
        value: new SSH.Client()
      }
    });
  },

  // opts.host
  // opts.port
  // opts.username
  // opts.password
  // opts.privateKey
  // opts.sock
  connect: function (opts) {
    opts = U.clone(opts || Object.create(null));

    var
    self = this,
    connection = this.connection,
    promise;

    function connect(opts) {
      return new Promise(function (resolve, reject) {
        connection.on('ready', function () {
          console.log('CONNECT 5')
          return resolve(self);
        });
        connection.on('error', function (err) {
          console.log('CONNECT 6')
        });
        connection.on('error', reject);
        try {
          connection.connect(opts);
        } catch (err) {
          return reject(err);
        }
          console.log('CONNECT 4')
      });
    }

    console.log('CONNECT 1')
    if (opts.privateKey) {
      promise = FilePath
        .create(opts.privateKey)
        .read()
        .then(function (keytext) {
          console.log('CONNECT 3')
          opts.privateKey = keytext;
          return connect(opts);
        });
    } else {
      promise = connect(opts);
    }
    console.log('CONNECT 2')
    return promise;
  },

  // opts.host
  // opts.port
  // opts.username
  // opts.password
  // opts.privateKey
  hop: function (args) {
    var
    self       = this,
    log        = this.log,
    host       = args.host,
    port       = args.port || 22,
    username   = args.username,
    password   = <PASSWORD>,
    privateKey = args.privateKey;

    return new Promise(function (resolve, reject) {
      var
      cmd = 'nc '+ host +' '+ port,
      cxn2;
      self.connection.exec(cmd, function (err, stream) {
        if (err) {
          self.connection.end();
          return reject(err);
        }
        cxn2 = exports.newSSH({
          log: log
        });
        cxn2.connection.on('ready', function () {
          return resolve(cxn2);
        });
        cxn2.connection.on('error', reject);
        try {
          cxn2.connect({
            sock       : stream,
            username   : username,
            password   : password,
            privateKey : privateKey
          }).then(resolve, reject);
          console.log('LIB 5')
        } catch (err) {
          return reject(err);
        }
      });
    });
  },

  exec: function (command) {
    var
    self = this,
    log = this.log;
    return new Promise(function (resolve, reject) {
      self.connection.exec(command, function (err, stream) {
        if (err) {
          self.connection.end()
          return reject(err);
        }
        stream.setEncoding('utf8');
        stream.on('close', function (code, signal) {
          log.info('ssh exec - close - code=%s - signal=%s - %s', code, signal, command);
          log.stdout('ssh exec - close - code = %s - signal = %s', code, signal);
          return resolve(self);
        });
        stream.on('data', function (data) {
          log.info('ssh exec - stdout - %s', data);
          log.stdout('ssh> '+ data);
        });
        stream.stderr.on('data', function (data) {<commit_msg>Remove debug statements.<commit_after>var
FilePath = require('filepath').FilePath,
SSH      = require('ssh2'),
Promise  = require('./promise'),
U        = require('./u'),
Objects  = require('./objects');


exports.newSSH = Objects.factory({

  initialize: function (spec) {
    Object.defineProperties(this, {
      log: {
        value: spec.log
      },
      connection: {
        value: new SSH.Client()
      }
    });
  },

  // opts.host
  // opts.port
  // opts.username
  // opts.password
  // opts.privateKey
  // opts.sock
  connect: function (opts) {
    opts = U.clone(opts || Object.create(null));

    var
    self = this,
    connection = this.connection,
    promise;

    function connect(opts) {
      return new Promise(function (resolve, reject) {
        connection.on('ready', function () {
          return resolve(self);
        });
        connection.on('error', reject);
        try {
          connection.connect(opts);
        } catch (err) {
          return reject(err);
        }
      });
    }

    if (opts.privateKey) {
      promise = FilePath
        .create(opts.privateKey)
        .read()
        .then(function (keytext) {
          opts.privateKey = keytext;
          return connect(opts);
        });
    } else {
      promise = connect(opts);
    }
    return promise;
  },

  // opts.host
  // opts.port
  // opts.username
  // opts.password
  // opts.privateKey
  hop: function (args) {
    var
    self       = this,
    log        = this.log,
    host       = args.host,
    port       = args.port || 22,
    username   = args.username,
    password   = args.password,
    privateKey = args.privateKey;

    return new Promise(function (resolve, reject) {
      var
      cmd = 'nc '+ host +' '+ port,
      cxn2;
      self.connection.exec(cmd, function (err, stream) {
        if (err) {
          self.connection.end();
          return reject(err);
        }
        cxn2 = exports.newSSH({
          log: log
        });
        cxn2.connection.on('ready', function () {
          return resolve(cxn2);
        });
        cxn2.connection.on('error', reject);
        try {
          cxn2.connect({
            sock       : stream,
            username   : username,
            password   : password,
            privateKey : privateKey
          }).then(resolve, reject);
        } catch (err) {
          return reject(err);
        }
      });
    });
  },

  exec: function (command) {
    var
    self = this,
    log = this.log;
    return new Promise(function (resolve, reject) {
      self.connection.exec(command, function (err, stream) {
        if (err) {
          self.connection.end()
          return reject(err);
        }
        stream.setEncoding('utf8');
        stream.on('close', function (code, signal) {
          log.info('ssh exec - close - code=%s - signal=%s - %s', code, signal, command);
          log.stdout('ssh exec - close - code = %s - signal = %s', code, signal);
          return resolve(self);
        });
        stream.on('data', function (data) {
          log.info('ssh exec - stdout - %s', data);
          log.stdout('ssh> '+ data);
        });
        stream.stderr.on('data', function (data) {
{'fraction_non_alphanumeric': 0.14429278536073195, 'fraction_numerical': 0.004049797510124494, 'mean_word_length': 1.7039740470397404, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '5844717', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1951, 'n_words': 435}