<commit_before>// @file Executable script to start a top server.

'use strict';

// Third party libs.
var _ = require('lodash');
var debug = require('debug')('cee');

// Locally defined libs.
var common = require('./server/common');
var createTop = require('./server/createtop').createTop;

var argv = require('commander')
  .usage('[options]')
  .option('-p, --port <n>', 'Port to listen on.', parseInt)
  .option('-b, --hostname <a>', 'Address to bind to. (eg. "0.0.0.0")')
  .option(
    '-i, --pidfile <filename>',
    'File to store child process identifiers',
    '.child-pids'
  )
  .option(
    '-a, --activities <a>',
    'Comma separated list of activities to start.'
  )
  .parse(process.argv);

var checkArgType = function(arg, type) {
  return typeof arg !== type && arg !== undefined;
};

var logArgError = function() {
  console.error.apply(console, arguments);
  argv.help();
};

// Do not allow unknown options to be given. Show help and quit.
if (argv.args.length > 0) {
  logArgError('Unrecognized options:', argv.args.join(' '));
// Make sure hostname is a string. Print a helpful message if it isn't.
} else if (checkArgType(argv.hostname, 'string')) {
  logArgError('--hostname value must be a string. Given:', argv.hostname);
// Make sure activities is a string. Print a helpful message if it isn't.
} else if (checkArgType(argv.activities, 'string')) {
  logArgError('--activities value must be a string. Given:', argv.activities);
}

// Build a scriptFilter function from the activities argument.
//
// Comma separated values can be name, !name, or *.
// name: Include the activity with that name.
// !name: Exclude the activity with that name.
//    If all arguments are !name style, all remaining activities are included.
// *: Include all other activities.
//    Need if name and !name style are used.
if (argv.activities) {
  var activities = argv.activities.split(',');
  var allRemainingActivities = _.contains(activities, '*');
  if (
    !allRemainingActivities &&
    _.all(activities, function(activity) {
      return activity[0] === '!';
    })
  ) {
    allRemainingActivities = true;
  }

  argv.scriptFilter = function(scriptPath) {
    var scriptName = /src\/activities\/([-\w]+)\/index.js/.exec(scriptPath)[1];
    // User can include a !name argument along with * to exclude activities.
    if (_.contains(activities, '!' + scriptName)) {
      return false;
    } else if (allRemainingActivities || _.contains(activities, scriptName)) {
      return true;
    }
    return false;
  };
}

createTop(argv, debug)
  .then(function(server) {
    common.atTermination(server.close.bind(server), debug);
  })
  .catch(function(e) {
    // Log a more descriptive message for common listen errors.
    if (e.code === 'EACCES' && e.syscall === 'listen') {
      console.error('Unable to bind to port:', argv.port);
    } else if (e.code === 'EADDRNOTAVAIL' && e.syscall === 'listen' ) {
      console.error('Unable to bind to hostname:', argv.hostname);
    }

    // Print the error
    console.error(e);
  });<commit_msg>Print port on command line<commit_after>// @file Executable script to start a top server.

'use strict';

// Third party libs.
var _ = require('lodash');
var debug = require('debug')('cee');

// Locally defined libs.
var common = require('./server/common');
var createTop = require('./server/createtop').createTop;

var argv = require('commander')
  .usage('[options]')
  .option('-p, --port <n>', 'Port to listen on.', parseInt)
  .option('-b, --hostname <a>', 'Address to bind to. (eg. "0.0.0.0")')
  .option(
    '-i, --pidfile <filename>',
    'File to store child process identifiers',
    '.child-pids'
  )
  .option(
    '-a, --activities <a>',
    'Comma separated list of activities to start.'
  )
  .parse(process.argv);

var checkArgType = function(arg, type) {
  return typeof arg !== type && arg !== undefined;
};

var logArgError = function() {
  console.error.apply(console, arguments);
  argv.help();
};

// Do not allow unknown options to be given. Show help and quit.
if (argv.args.length > 0) {
  logArgError('Unrecognized options:', argv.args.join(' '));
// Make sure hostname is a string. Print a helpful message if it isn't.
} else if (checkArgType(argv.hostname, 'string')) {
  logArgError('--hostname value must be a string. Given:', argv.hostname);
// Make sure activities is a string. Print a helpful message if it isn't.
} else if (checkArgType(argv.activities, 'string')) {
  logArgError('--activities value must be a string. Given:', argv.activities);
}

// Build a scriptFilter function from the activities argument.
//
// Comma separated values can be name, !name, or *.
// name: Include the activity with that name.
// !name: Exclude the activity with that name.
//    If all arguments are !name style, all remaining activities are included.
// *: Include all other activities.
//    Need if name and !name style are used.
if (argv.activities) {
  var activities = argv.activities.split(',');
  var allRemainingActivities = _.contains(activities, '*');
  if (
    !allRemainingActivities &&
    _.all(activities, function(activity) {
      return activity[0] === '!';
    })
  ) {
    allRemainingActivities = true;
  }

  argv.scriptFilter = function(scriptPath) {
    var scriptName = /src\/activities\/([-\w]+)\/index.js/.exec(scriptPath)[1];
    // User can include a !name argument along with * to exclude activities.
    if (_.contains(activities, '!' + scriptName)) {
      return false;
    } else if (allRemainingActivities || _.contains(activities, scriptName)) {
      return true;
    }
    return false;
  };
}

createTop(argv, debug)
  .then(function(server) {
    console.log('Server running on port ' + server.address().port);
    common.atTermination(server.close.bind(server), debug);
  })
  .catch(function(e) {
    // Log a more descriptive message for common listen errors.
    if (e.code === 'EACCES' && e.syscall === 'listen') {
      console.error('Unable to bind to port:', argv.port);
    } else if (e.code === 'EADDRNOTAVAIL' && e.syscall === 'listen' ) {
      console.error('Unable to bind to hostname:', argv.hostname);
    }

    // Print the error
    console.error(e);
  });
{'fraction_non_alphanumeric': 0.15765693077295465, 'fraction_numerical': 0.002259157656930773, 'mean_word_length': 3.3433777154870357, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '7606854', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1926, 'n_words': 603}