<commit_before>
var sqlite3 = require('sqlite3');
var path = require('path');
var vm = require('vm');
var fs = require('fs');
var _ = require('lodash');

/**
 * This is more ore less what meteor "main.js" does to detect
 * the meteor-tool version corresponding to the releaseName.
 * This function must me run inside a Fiber!
 */
module.exports = function getMeteorToolPath (home, releaseName, callback) {
  "use strict";

  var pathToMeteor  = path.join(home, '.meteor');
  var meteorSymLink = path.join(pathToMeteor, fs.readlinkSync(path.join(pathToMeteor, 'meteor')));

  var track      = releaseName.split('@')[0];
  var version    = releaseName.split('@')[1];
  var components = _.initial(meteorSymLink.split(path.sep));
  var mtPath     = '/' + path.join.apply(path, components.concat([ 'tools' ]));
  var arch       = _.last(_.last(components).split('-'));

  var dbFile = runInIsolatedContext(function (require, pathToConfig) {
    return require(pathToConfig).getPackageStorage();
  }, [ require, path.join(mtPath, 'config') ]);

  var db = new sqlite3.Database(dbFile);

  db.all('SELECT content FROM releaseVersions WHERE track="' + track + '" AND version="' + version + '"', {}, function (err, result) {
    if (err) {
      return callback(err);
    }
    if (result.lenth === 0) {
      return callback(new Error('cannot find relase ' + releaseName));
    }
    var release      = JSON.parse(result[0].content);
    var toolsPkg     = release.tool.split('@')[0];
    var toolsVersion = release.tool.split('@')[1];

    var feedback = runInIsolatedContext(function (require, root, toolsPkg, toolsVersion) {
      "use strict"
      var path         = require('path');
      var config       = require(path.join(root, 'config'));
      var isopack      = require(path.join(root, 'isopack'));
      var tropohouse   = require(path.join(root, 'tropohouse'));
      var packagePath  = tropohouse.default.packagePath(toolsPkg, toolsVersion);
      var toolIsopack  = new isopack.Isopack;

      toolIsopack.initFromPath(toolsPkg, packagePath);
      return {
        toolsOnDisk: toolIsopack.toolsOnDisk,
        packagePath: packagePath
      };
    }, [ require, mtPath, toolsPkg, toolsVersion ]);

    var toolRecord = _.findWhere(feedback.toolsOnDisk, { arch: arch });

    if (!toolRecord) {
      return callback(new Error('cannot find tool for arch ' + arch));
    }

    db.close(function () {
      callback(null, path.join(feedback.packagePath, toolRecord.path));
    });
  });
}

function runInIsolatedContext (func, args) {
  var code = '(' + func.toString() + ')';
  return vm.runInNewContext(code).apply({}, args);
}
<commit_msg>Simplified tool package detection logic<commit_after>
var sqlite3 = require('sqlite3');
var path = require('path');
var fs = require('fs');
var _ = require('lodash');

/**
 * This is more ore less what meteor "main.js" does to detect
 * the meteor-tool version corresponding to the releaseName.
 * This function must me run inside a Fiber!
 */
module.exports = function getMeteorToolPath (home, releaseName, callback) {
  "use strict";

  var pathToMeteor  = path.join(home, '.meteor');
  var meteorSymLink = path.join(pathToMeteor, fs.readlinkSync(path.join(pathToMeteor, 'meteor')));

  var track      = releaseName.split('@')[0];
  var version    = releaseName.split('@')[1];
  var components = _.initial(meteorSymLink.split(path.sep));
  var mtPath     = '/' + path.join.apply(path, components.concat([ 'tools' ]));
  var arch       = _.last(_.last(components).split('-'));

  var dbFile = path.join(home, '.meteor', 'package-metadata', 'v2.0.1', 'packages.data.db');

  var db = new sqlite3.Database(dbFile);

  db.all('SELECT content FROM releaseVersions WHERE track="' + track + '" AND version="' + version + '"', {}, function (err, result) {
    if (err) {
      return callback(err);
    }

    if (result.lenth === 0) {
      return callback(new Error('cannot find relase ' + releaseName));
    }

    var release      = JSON.parse(result[0].content);
    var toolsPkg     = release.tool.split('@')[0];
    var toolsVersion = release.tool.split('@')[1];
    var packagePath  = path.join(home, '.meteor', 'packages', toolsPkg, toolsVersion);
    var isopackPath  = path.join(packagePath, 'isopack.json');
    var isopackJSON  = JSON.parse(fs.readFileSync(isopackPath, 'utf8'));
    var toolRecord   = _.findWhere(isopackJSON['isopack-1'].tools, { arch: arch });

    if (!toolRecord) {
      return callback(new Error('cannot find tool for arch ' + arch));
    }

    db.close(function () {
      callback(null, path.join(packagePath, toolRecord.path));
    });
  });
}

{'fraction_non_alphanumeric': 0.15701943844492441, 'fraction_numerical': 0.004967602591792656, 'mean_word_length': 3.607960199004975, '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': '4596862', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1448, 'n_words': 352}