<commit_before>import * as fs from 'fs';
import * as minimist from 'minimist';
import * as path from 'path';
import * as os from 'os';
import * as childProcess from 'child_process';
import * as http from 'http';

import {Opts} from './opts';
import * as Opt from './';
import {Config} from '../config';
import {FileManager} from '../files';
import {Logger, Options, Program} from '../cli';
import {BinaryMap, Binary, ChromeDriver, IEDriver, AndroidSDK, Appium, StandAlone} from
    '../binaries';

let logger = new Logger('start');
let prog = new Program()
    .command('start', 'start up the selenium server')
    .action(start)
    .addOption(Opts[Opt.OUT_DIR])
    .addOption(Opts[Opt.SELENIUM_PORT])
    .addOption(Opts[Opt.APPIUM_PORT])
    .addOption(Opts[Opt.AVD_PORT])
    .addOption(Opts[Opt.VERSIONS_STANDALONE])
    .addOption(Opts[Opt.VERSIONS_CHROME])
    .addOption(Opts[Opt.VERSIONS_ANDROID])
    .addOption(Opts[Opt.VERSIONS_APPIUM])
    .addOption(Opts[Opt.CHROME_LOGS])
    .addOption(Opts[Opt.ANDROID])
    .addOption(Opts[Opt.AVDS])
    .addOption(Opts[Opt.AVD_USE_SNAPSHOTS]);

if (os.type() === 'Darwin') {
  prog.addOption(Opts[Opt.IOS]);
}

if (os.type() === 'Windows_NT') {
  prog.addOption(Opts[Opt.VERSIONS_IE]);
  prog.addOption(Opts[Opt.EDGE]);
}

export var program = prog;

// stand alone runner
let argv = minimist(process.argv.slice(2), prog.getMinimistOptions());
if (argv._[0] === 'start-run') {
  prog.run(JSON.parse(JSON.stringify(argv)));
} else if (argv._[0] === 'start-help') {
  prog.printHelp();
}

/**
 * Parses the options and starts the selenium standalone server.
 * @param options
 */
function start(options: Options) {
  let osType = os.type();
  let binaries = FileManager.setupBinaries();
  let seleniumPort = options[Opt.SELENIUM_PORT].getString();
  let outputDir = Config.getSeleniumDir();
  if (options[Opt.OUT_DIR].getString()) {
    if (path.isAbsolute(options[Opt.OUT_DIR].getString())) {
      outputDir = options[Opt.OUT_DIR].getString();
    } else {
      outputDir = path.resolve(Config.getBaseDir(), options[Opt.OUT_DIR].getString());
    }
  }

  try {
    // check if folder exists
    fs.statSync(outputDir).isDirectory();
  } catch (e) {
    // if the folder does not exist, quit early.
    logger.warn('the out_dir path ' + outputDir + ' does not exist, run webdriver-manager update');
    return;
  }

  let chromeLogs: string = null;
  if (options[Opt.CHROME_LOGS].getString()) {
    if (path.isAbsolute(options[Opt.CHROME_LOGS].getString())) {
      chromeLogs = options[Opt.CHROME_LOGS].getString();
    } else {
      chromeLogs = path.resolve(Config.getBaseDir(), options[Opt.CHROME_LOGS].getString());
    }
  }
  binaries[StandAlone.id].versionCustom = options[Opt.VERSIONS_STANDALONE].getString();
  binaries[ChromeDriver.id].versionCustom = options[Opt.VERSIONS_CHROME].getString();
  if (options[Opt.VERSIONS_IE]) {
    binaries[IEDriver.id].versionCustom = options[Opt.VERSIONS_IE].getString();
  }
  binaries[AndroidSDK.id].versionCustom = options[Opt.VERSIONS_ANDROID].getString();
  binaries[Appium.id].versionCustom = options[Opt.VERSIONS_APPIUM].getString();
  let downloadedBinaries = FileManager.downloadedBinaries(outputDir);

  if (downloadedBinaries[StandAlone.id] == null) {
    logger.error(
        'Selenium Standalone is not present. Install with ' +
        'webdriver-manager update --standalone');
    process.exit(1);
  }
  let args: string[] = ['-jar', path.join(outputDir, binaries[StandAlone.id].filename())];<commit_msg>chore(selenium): add dev/urandom to selenium start args to prevent startup delays in linux<commit_after>import * as fs from 'fs';
import * as minimist from 'minimist';
import * as path from 'path';
import * as os from 'os';
import * as childProcess from 'child_process';
import * as http from 'http';

import {Opts} from './opts';
import * as Opt from './';
import {Config} from '../config';
import {FileManager} from '../files';
import {Logger, Options, Program} from '../cli';
import {BinaryMap, Binary, ChromeDriver, IEDriver, AndroidSDK, Appium, StandAlone} from
    '../binaries';

let logger = new Logger('start');
let prog = new Program()
    .command('start', 'start up the selenium server')
    .action(start)
    .addOption(Opts[Opt.OUT_DIR])
    .addOption(Opts[Opt.SELENIUM_PORT])
    .addOption(Opts[Opt.APPIUM_PORT])
    .addOption(Opts[Opt.AVD_PORT])
    .addOption(Opts[Opt.VERSIONS_STANDALONE])
    .addOption(Opts[Opt.VERSIONS_CHROME])
    .addOption(Opts[Opt.VERSIONS_ANDROID])
    .addOption(Opts[Opt.VERSIONS_APPIUM])
    .addOption(Opts[Opt.CHROME_LOGS])
    .addOption(Opts[Opt.ANDROID])
    .addOption(Opts[Opt.AVDS])
    .addOption(Opts[Opt.AVD_USE_SNAPSHOTS]);

if (os.type() === 'Darwin') {
  prog.addOption(Opts[Opt.IOS]);
}

if (os.type() === 'Windows_NT') {
  prog.addOption(Opts[Opt.VERSIONS_IE]);
  prog.addOption(Opts[Opt.EDGE]);
}

export var program = prog;

// stand alone runner
let argv = minimist(process.argv.slice(2), prog.getMinimistOptions());
if (argv._[0] === 'start-run') {
  prog.run(JSON.parse(JSON.stringify(argv)));
} else if (argv._[0] === 'start-help') {
  prog.printHelp();
}

/**
 * Parses the options and starts the selenium standalone server.
 * @param options
 */
function start(options: Options) {
  let osType = os.type();
  let binaries = FileManager.setupBinaries();
  let seleniumPort = options[Opt.SELENIUM_PORT].getString();
  let outputDir = Config.getSeleniumDir();
  if (options[Opt.OUT_DIR].getString()) {
    if (path.isAbsolute(options[Opt.OUT_DIR].getString())) {
      outputDir = options[Opt.OUT_DIR].getString();
    } else {
      outputDir = path.resolve(Config.getBaseDir(), options[Opt.OUT_DIR].getString());
    }
  }

  try {
    // check if folder exists
    fs.statSync(outputDir).isDirectory();
  } catch (e) {
    // if the folder does not exist, quit early.
    logger.warn('the out_dir path ' + outputDir + ' does not exist, run webdriver-manager update');
    return;
  }

  let chromeLogs: string = null;
  if (options[Opt.CHROME_LOGS].getString()) {
    if (path.isAbsolute(options[Opt.CHROME_LOGS].getString())) {
      chromeLogs = options[Opt.CHROME_LOGS].getString();
    } else {
      chromeLogs = path.resolve(Config.getBaseDir(), options[Opt.CHROME_LOGS].getString());
    }
  }
  binaries[StandAlone.id].versionCustom = options[Opt.VERSIONS_STANDALONE].getString();
  binaries[ChromeDriver.id].versionCustom = options[Opt.VERSIONS_CHROME].getString();
  if (options[Opt.VERSIONS_IE]) {
    binaries[IEDriver.id].versionCustom = options[Opt.VERSIONS_IE].getString();
  }
  binaries[AndroidSDK.id].versionCustom = options[Opt.VERSIONS_ANDROID].getString();
  binaries[Appium.id].versionCustom = options[Opt.VERSIONS_APPIUM].getString();
  let downloadedBinaries = FileManager.downloadedBinaries(outputDir);

  if (downloadedBinaries[StandAlone.id] == null) {
    logger.error(
        'Selenium Standalone is not present. Install with ' +
        'webdriver-manager update --standalone');
    process.exit(1);
  }
  let args: string[] = ['-jar', path.join(outputDir, binaries[StandAlone.id].filename())];
  if (osType === 'Linux') {
    // selenium server may take a long time to start because /dev/random is BLOCKING if there is not enough entropy
    // the solution is to use /dev/urandom, which is NON-BLOCKING (use /dev/./urandom because of a java bug)
    // https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1301
    // https://bugs.openjdk.java.net/browse/JDK-6202721
    args.push('-Djava.security.egd=file:///dev/./urandom');
  }
{'fraction_non_alphanumeric': 0.1571240105540897, 'fraction_numerical': 0.0025065963060686017, 'mean_word_length': 4.199588477366255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2437493', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2558, 'n_words': 514}