<commit_before>var Tessel = require('./tessel'),
  commands = require('./commands'),
  logs = require('../logs'),
  fs = require('fs')
  ;

var RUN_PATH = '/tmp/remote-script/';
var PUSH_PATH = '/app/';
var PUSH_START_SCRIPT_NAME = 'start';
var NODE_PUSH_SCRIPT = __dirname + '/../../resources/start_node_script.sh';
/*
  Run a script on a Tessel
    param opts: unused so far
    param push: whether or not this script is pushed into RAM or flash
*/
Tessel.prototype.deployScript = function(opts, push) {
  var self = this;

  var filepath = push ? PUSH_PATH : RUN_PATH;
  if (push) {
    filepath = PUSH_PATH;
  }

  // Stop running any existing scripts
  stopRunningScript(self, filepath, function() {
    // Delete any code that was previously at this file path
    deleteOldCode(self, filepath, function() {
      // Create the folder again
      createNewCodeFolder(self, filepath, function() {
        // Bundle and send tarred code to T2
        untarStdin(self, filepath, opts, function(ret, entryPoint) {
          // If we are pushing
          if (push) {
            // Push the script into flash
            pushScript(self, opts.entryPoint);
          }
          // If we are pushing
          else {
            // Push the code into ram
            runScript(self, filepath, ret, opts.entryPoint);
          }
        });
      });
    });
  });
};

function stopRunningScript(t, filepath, callback) {
  // Stop running any scripts
  t.connection.exec(commands.stopRunningScript(filepath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    remoteProcess.once('close', callback);
  });
}

function deleteOldCode(t, filepath, callback) {
  // Remove the old scripts
  t.connection.exec(commands.deleteFolder(filepath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    // Once they are removed...
    remoteProcess.once('close', callback);
  });
}

function createNewCodeFolder(t, filepath, callback) {
  t.connection.exec(commands.createFolder(filepath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    remoteProcess.once('close', callback);
  });
}

function untarStdin(t, filepath, opts, callback) {
  t.connection.exec(commands.untarStdin(filepath), function(err, remoteProcess) {
    remoteProcess.stderr.on('data', function(d) {
      logs.err('Unable to deploy code:', d.toString());
      t.connection.end(function() {
        process.exit(1);
      });
    });

    var tesselClassic = require('tessel');

    // TODO: This section unnecessarily synchronous
    t.getName(function (err, tesselname) {
      // Gather details about the file structure of the script being sent
      var ret = tesselClassic.analyzeScript(process.cwd() + '/' + opts.entryPoint, {
        verbose: opts.verbose
      });
      // Tar up the code to improve transfer rates
      tesselClassic.tarCode(ret.pushdir, {
        node: true
      }, function(err, bundle) {
        // RAM or Flash for log
        var memtype;
        if (opts._[0] === 'push') {
          memtype = 'Flash';
        } else {
          memtype = 'RAM';
        }
        // Log write
        logs.info('Writing %s to %s on %s (%d kB)...', opts.entryPoint, memtype, tesselname, bundle.length/1000);
        // Throw any unfortunate errors
        if (err) {
          throw err;
        }

        // Wait for the transfer to finish...
        remoteProcess.once('close', function() {
          logs.info('Deployed.');
          if (typeof callback === 'function') {
            callback(ret);
          }
        });
        // Write the code bundle to the hardware
        remoteProcess.stdin.end(bundle);
      });
    });
  });
}

function runScript(t, filepath, ret, entryPoint) {
  logs.info('Running %s...', entryPoint);
  t.connection.exec(commands.runScript(filepath, ret.relpath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    // Once we get a SIGINT from the console
    process.once('SIGINT', function() {<commit_msg>de-linted<commit_after>var Tessel = require('./tessel'),
  commands = require('./commands'),
  logs = require('../logs'),
  fs = require('fs');

var RUN_PATH = '/tmp/remote-script/';
var PUSH_PATH = '/app/';
var PUSH_START_SCRIPT_NAME = 'start';
var NODE_PUSH_SCRIPT = __dirname + '/../../resources/start_node_script.sh';
/*
  Run a script on a Tessel
    param opts: unused so far
    param push: whether or not this script is pushed into RAM or flash
*/
Tessel.prototype.deployScript = function(opts, push) {
  var self = this;

  var filepath = push ? PUSH_PATH : RUN_PATH;
  if (push) {
    filepath = PUSH_PATH;
  }

  // Stop running any existing scripts
  stopRunningScript(self, filepath, function() {
    // Delete any code that was previously at this file path
    deleteOldCode(self, filepath, function() {
      // Create the folder again
      createNewCodeFolder(self, filepath, function() {
        // Bundle and send tarred code to T2
        untarStdin(self, filepath, opts, function(ret) {
          // If we are pushing
          if (push) {
            // Push the script into flash
            pushScript(self, opts.entryPoint);
          }
          // If we are pushing
          else {
            // Push the code into ram
            runScript(self, filepath, ret, opts.entryPoint);
          }
        });
      });
    });
  });
};

function stopRunningScript(t, filepath, callback) {
  // Stop running any scripts
  t.connection.exec(commands.stopRunningScript(filepath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    remoteProcess.once('close', callback);
  });
}

function deleteOldCode(t, filepath, callback) {
  // Remove the old scripts
  t.connection.exec(commands.deleteFolder(filepath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    // Once they are removed...
    remoteProcess.once('close', callback);
  });
}

function createNewCodeFolder(t, filepath, callback) {
  t.connection.exec(commands.createFolder(filepath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    remoteProcess.once('close', callback);
  });
}

function untarStdin(t, filepath, opts, callback) {
  t.connection.exec(commands.untarStdin(filepath), function(err, remoteProcess) {
    remoteProcess.stderr.on('data', function(d) {
      logs.err('Unable to deploy code:', d.toString());
      t.connection.end(function() {
        process.exit(1);
      });
    });

    var tesselClassic = require('tessel');

    // TODO: This section unnecessarily synchronous
    t.getName(function(err, tesselname) {
      // Gather details about the file structure of the script being sent
      var ret = tesselClassic.analyzeScript(process.cwd() + '/' + opts.entryPoint, {
        verbose: opts.verbose
      });
      // Tar up the code to improve transfer rates
      tesselClassic.tarCode(ret.pushdir, {
        node: true
      }, function(err, bundle) {
        // RAM or Flash for log
        var memtype;
        if (opts._[0] === 'push') {
          memtype = 'Flash';
        } else {
          memtype = 'RAM';
        }
        // Log write
        logs.info('Writing %s to %s on %s (%d kB)...', opts.entryPoint, memtype, tesselname, bundle.length / 1000);
        // Throw any unfortunate errors
        if (err) {
          throw err;
        }

        // Wait for the transfer to finish...
        remoteProcess.once('close', function() {
          logs.info('Deployed.');
          if (typeof callback === 'function') {
            callback(ret);
          }
        });
        // Write the code bundle to the hardware
        remoteProcess.stdin.end(bundle);
      });
    });
  });
}

function runScript(t, filepath, ret, entryPoint) {
  logs.info('Running %s...', entryPoint);
  t.connection.exec(commands.runScript(filepath, ret.relpath), function(err, remoteProcess) {
    if (err) {
      throw err;
    }

    // Once we get a SIGINT from the console
    process.once('SIGINT', function() {
{'fraction_non_alphanumeric': 0.13146713321669581, 'fraction_numerical': 0.0017495626093476631, 'mean_word_length': 2.4040833687792427, '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': '1029895', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2353, 'n_words': 643}