<commit_before>#! /usr/bin/env node

'use strict';

var http = require('http');
var path = require('path');
var fs = require('fs');
var childProcess = require('child_process');
var JSFtp = require('jsftp');
var q = require('q');
var temp = require('temp');
var mkdirp = require('mkdirp');
var moment = require('moment');
var yargs = require('yargs');
var colors = require('colors');

// Command-line arguments
var yarg = yargs.usage('Shallow-flash Gecko and Gaia on Firefox OS devices from Mozilla\'s public build server (http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/).\nUsage: flash-b2g [device] [channel]')
	.example('$0 flame 1.4', 'Flash a flame with 1.4 build.')
	.example('$0 flame --folder ~/', 'Flash a flame with a nightly build (downloaded to ~/)')
	.example('$0 flame --folder ~/ --local', 'Flash a Flame device with a previously downloaded build in ~/.')
	.example('$0 hamachi aurora --eng', 'Flash an Hamachi device with an aurora engineering build.')
	.string([1, 'device', 'channel', 'date'])
	.alias({
		'dir': 'd',
		'channel': 'c',
		'device': 'i',
		'date': 't',
		'local': 'l',
		'eng': 'e',
		'profile': 'p',
		'remotify': 'r'
	})
	.boolean(['eng', 'local', 'profile', 'help', 'remotify', 'help'])
	.default({
		device: 'flame',
		channel: 'central',
		date: 'latest'
	})
	.describe({
		device: 'Device (flame, helix, hamachi, …)',
		channel: 'Channel (central, aurora, 1.4, …)',
		date: 'Build date (regression window testing)',
		eng: 'Engineering build (marionette testing)',
		local: 'Use local files, skipping FTP',
		profile: 'Keep profile (no promises)',
		remotify: 'Set device into development mode',
		help: 'Show this help'
	});
var argv = yarg.argv;
if (argv.help) {
	console.log(yarg.help());
	process.exit();
}
argv.device = (argv._[0] || argv.device).toLowerCase();
argv.channel = String(argv._[1] || argv.channel);
if (/^\d+$/.test(argv.channel)) {
	argv.channel += '.0';
}
if (argv.date && argv.date != 'latest') {
	argv.date = moment(argv.date);
	if (!argv.date.isValid()) {
		throw new Error('Invalid `date` argument');
	}
} else {
	argv.date = null;
}

// Configuration
var FTP_HOST = 'ftp.mozilla.org';
var FTP_URL = 'http://' + FTP_HOST;
var FTP_PATH = '/pub/mozilla.org/b2g/nightly/';
var SCRIPT_PATH = path.join(__dirname, 'scripts');
var FLASH_SCRIPT_PATH = path.join(SCRIPT_PATH, 'shallow_flash.sh');
var TIMEOUT = 60 * 60 * 1000; // 1min

// Set up dirs
temp.track();
var tempDir = temp.mkdirSync('flash-b2g');
var dir = argv.dir;
if (!dir) {
	dir = tempDir;
} else {
	dir = path.resolve(dir)
	if (!fs.existsSync(dir)) {
		mkdirp.sync(dir);
		console.log('Created directory: %s', dir)
	}
}

if (argv['only-remotify']) {
	setDeveloperPrefs().done(function() {
		process.exit();
	});
	return;
}

// FTP connection
// TODO: Allow login
var ftp = new JSFtp({
	host: FTP_HOST
});
ftp.useList = true;
ftp.socket.setTimeout(TIMEOUT);
ftp.socket.once('timeout', function(data) {
	ftp.socket.destroy(); // should call the "ls" callback "err"
});

var dateBit = (argv.date) ? argv.date.format('YYYY-MM-DD') : 'latest';
var channelBit = argv.channel;
if (/\d/.test(channelBit)) {
	channelBit = '.*v' + channelBit.replace(/\./, '_');
}
var pathBits = [dateBit, '.*', channelBit, argv.device];
var nameBits = [argv.device, channelBit];
if (argv.eng) {
	pathBits.push('eng');
	nameBits.push('eng');
}
nameBits.push(dateBit);
var pathMatch = new RegExp('^' + pathBits.join('-') + '$', 'i');
var ftpPath = FTP_PATH;

var b2gFilePath = null;
var b2gFile = null;
var gaiaFile = null;

var localGaiaPath = path.join(dir, 'gaia-' + nameBits.join('-') + '.zip');
var localB2gPath = path.join(dir, 'b2g-' + nameBits.join('-') + '.tar.gz');

function setDeveloperPrefs() {
	var prefs = {
		'devtools.console.logger.forbid-certified-apps': false,
		'devtools.console.logger.prompt-connection': false,
		'b2g.adb.timeout': 0,
		// Bug 1001348: This optimization prevents debugger to fetch script sources
		// of certified apps as well as chrome code:
		'javascript.options.discardSystemSource': false,
		// Reduce noise in logs: http://kb.mozillazine.org/Layout.css.report_errors
		'layout.css.report_errors': false
	};
	var settings = {
		'developer.menu.enabled': true,
		'ftu.manifestURL': null,
		'debugger.remote-mode': 'adb-devtools',
		'devtools.debugger.remote-enabled': true,
		'screen.timeout': 0,
		'lockscreen.locked': false,
		'lockscreen.enabled': false,
	};
<commit_msg>Clean up DevTools prefs / settings<commit_after>#! /usr/bin/env node

'use strict';

var http = require('http');
var path = require('path');
var fs = require('fs');
var childProcess = require('child_process');
var JSFtp = require('jsftp');
var q = require('q');
var temp = require('temp');
var mkdirp = require('mkdirp');
var moment = require('moment');
var yargs = require('yargs');
var colors = require('colors');

// Command-line arguments
var yarg = yargs.usage('Shallow-flash Gecko and Gaia on Firefox OS devices from Mozilla\'s public build server (http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/).\nUsage: flash-b2g [device] [channel]')
	.example('$0 flame 1.4', 'Flash a flame with 1.4 build.')
	.example('$0 flame --folder ~/', 'Flash a flame with a nightly build (downloaded to ~/)')
	.example('$0 flame --folder ~/ --local', 'Flash a Flame device with a previously downloaded build in ~/.')
	.example('$0 hamachi aurora --eng', 'Flash an Hamachi device with an aurora engineering build.')
	.string([1, 'device', 'channel', 'date'])
	.alias({
		'dir': 'd',
		'channel': 'c',
		'device': 'i',
		'date': 't',
		'local': 'l',
		'eng': 'e',
		'profile': 'p',
		'remotify': 'r'
	})
	.boolean(['eng', 'local', 'profile', 'help', 'remotify', 'help'])
	.default({
		device: 'flame',
		channel: 'central',
		date: 'latest'
	})
	.describe({
		device: 'Device (flame, helix, hamachi, …)',
		channel: 'Channel (central, aurora, 1.4, …)',
		date: 'Build date (regression window testing)',
		eng: 'Engineering build (marionette testing)',
		local: 'Use local files, skipping FTP',
		profile: 'Keep profile (no promises)',
		remotify: 'Set device into development mode',
		help: 'Show this help'
	});
var argv = yarg.argv;
if (argv.help) {
	console.log(yarg.help());
	process.exit();
}
argv.device = (argv._[0] || argv.device).toLowerCase();
argv.channel = String(argv._[1] || argv.channel);
if (/^\d+$/.test(argv.channel)) {
	argv.channel += '.0';
}
if (argv.date && argv.date != 'latest') {
	argv.date = moment(argv.date);
	if (!argv.date.isValid()) {
		throw new Error('Invalid `date` argument');
	}
} else {
	argv.date = null;
}

// Configuration
var FTP_HOST = 'ftp.mozilla.org';
var FTP_URL = 'http://' + FTP_HOST;
var FTP_PATH = '/pub/mozilla.org/b2g/nightly/';
var SCRIPT_PATH = path.join(__dirname, 'scripts');
var FLASH_SCRIPT_PATH = path.join(SCRIPT_PATH, 'shallow_flash.sh');
var TIMEOUT = 60 * 60 * 1000; // 1min

// Set up dirs
temp.track();
var tempDir = temp.mkdirSync('flash-b2g');
var dir = argv.dir;
if (!dir) {
	dir = tempDir;
} else {
	dir = path.resolve(dir)
	if (!fs.existsSync(dir)) {
		mkdirp.sync(dir);
		console.log('Created directory: %s', dir)
	}
}

if (argv['only-remotify']) {
	setDeveloperPrefs().done(function() {
		process.exit();
	});
	return;
}

// FTP connection
// TODO: Allow login
var ftp = new JSFtp({
	host: FTP_HOST
});
ftp.useList = true;
ftp.socket.setTimeout(TIMEOUT);
ftp.socket.once('timeout', function(data) {
	ftp.socket.destroy(); // should call the "ls" callback "err"
});

var dateBit = (argv.date) ? argv.date.format('YYYY-MM-DD') : 'latest';
var channelBit = argv.channel;
if (/\d/.test(channelBit)) {
	channelBit = '.*v' + channelBit.replace(/\./, '_');
}
var pathBits = [dateBit, '.*', channelBit, argv.device];
var nameBits = [argv.device, channelBit];
if (argv.eng) {
	pathBits.push('eng');
	nameBits.push('eng');
}
nameBits.push(dateBit);
var pathMatch = new RegExp('^' + pathBits.join('-') + '$', 'i');
var ftpPath = FTP_PATH;

var b2gFilePath = null;
var b2gFile = null;
var gaiaFile = null;

var localGaiaPath = path.join(dir, 'gaia-' + nameBits.join('-') + '.zip');
var localB2gPath = path.join(dir, 'b2g-' + nameBits.join('-') + '.tar.gz');

function setDeveloperPrefs() {
	var prefs = {
		'devtools.debugger.forbid-certified-apps': false,
		'devtools.debugger.prompt-connection': false,
		'b2g.adb.timeout': 0,
		// Reduce noise in logs: http://kb.mozillazine.org/Layout.css.report_errors
		'layout.css.report_errors': false
	};
	var settings = {
		'developer.menu.enabled': true,
		'ftu.manifestURL': null,
		'debugger.remote-mode': 'adb-devtools',
		'screen.timeout': 0,
		'lockscreen.locked': false,
		'lockscreen.enabled': false,
	};

{'fraction_non_alphanumeric': 0.19728443773935245, 'fraction_numerical': 0.008703725194383196, 'mean_word_length': 4.2806372549019605, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '992492', 'n_tokens_mistral': 3306, 'n_tokens_neox': 3129, 'n_words': 794}