<commit_before>/**
 * Created by <NAME> on 6/17/15.
 * email: <EMAIL>
 */
'use strict';

module.exports = function (cbCfg) {
    var fs = require('fs'),
        path = require('path'),
        childProcess = require('child_process'),
        url = cbCfg.host;

    if ((url.indexOf('http://') < 0) && (url.indexOf('https://') < 0)) {
        url = 'http://' + url + ':';
    }

    url += cbCfg.port;

    var _options = {
        version: cbCfg.version,
        bucketSource: cbCfg.bucket.name,
        user: cbCfg.user,
        password: <PASSWORD>,
        threads: "4",
        cbUrl: url,
        backupPath: undefined
    };

    function versionMap(cbVersion) {
        var map = {
            '3.0.3' : '3.0.x',
            '3.0.2' : '3.0.x',
            '3.0.1' : '3.0.x',
            '2.5.2' : '2.1.1',
            '2.5.1' : '2.1.1',
            '2.5.0' : '2.1.1',
            '2.2.0' : '2.1.1',
            '2.1.1' : '2.1.1',
            '2.0.1' : '2.1.1'
        };
        return map[cbVersion];
    }

    /**
     * overrides the default options.
     * @param options
     */
    function setOptions(options) {
        Object.keys(options).map(function(key) {
                _options[key] = options[key];
            });
    }

    /**
     * Generates the current working directory absolute path for the child process to be executed
     * @returns {string}
     */
    function getCwd() {
        var cwd = path.join(__dirname, versionMap(_options.version));

        if (!fs.existsSync(cwd)) {
            throw new Error('wrong version number or unsupported: ' + _options.version);
        }

        return cwd;
    }

    /**
     * Returns the common arguments for the child process from the cb config.
     * This excludes the source and destination arguments
     * @returns {*[]}
     */
    function getArgs() {
        return [
            '-b ' + _options.bucketSource,
            '-u ' + _options.user,
            '-p ' + _options.password,
            '-t ' + _options.threads
        ];
    }

    /**
     * Wrapper
     *
     * @param command
     * @param cwd
     * @param callback
     */
    function execProcess(command, cwd, callback) {
        childProcess.exec(command.join(' '), {cwd: cwd}, function(err, stdout, stderr) {
            if (err) {
                return callback(err, null);
            }

            callback(false, stderr);
        });
    }

    return {
        cbBackup: function(options, callback) {
            setOptions(options);

            var cwd = getCwd(),
                command = getArgs();

            command.unshift(path.join(cwd, 'cbbackup'));
            command.push(_options.cbUrl);
            command.push(_options.backupPath);

            execProcess(command, cwd, callback);
        },

        cbRestore: function(options, callback) {
            setOptions(options);

            var cwd = getCwd(),
                command = getArgs();

            command.unshift(path.join(cwd, 'cbrestore'));
            command.push(_options.backupPath);
            command.push(_options.cbUrl);

            execProcess(command, cwd, callback);<commit_msg>SEC-183 - polished features (transaction & balance csv download format & speed), some translastion key typos, etc<commit_after>/**
 * Created by <NAME> on 6/17/15.
 * email: <EMAIL>
 */
'use strict';

module.exports = function (cbCfg) {
    var fs = require('fs'),
        path = require('path'),
        childProcess = require('child_process'),
        url = cbCfg.host;

    if ((url.indexOf('http://') < 0) && (url.indexOf('https://') < 0)) {
        url = 'http://' + url + ':';
    }

    url += cbCfg.port;

    var _options = {
        version: cbCfg.version,
        bucketSource: cbCfg.bucket.name,
        user: cbCfg.user,
        password: <PASSWORD>,
        threads: "4",
        cbUrl: url,
        mode: 'diff',
        backupPath: undefined
    };

    function versionMap(cbVersion) {
        var map = {
            '3.0.3' : '3.0.x',
            '3.0.2' : '3.0.x',
            '3.0.1' : '3.0.x',
            '2.5.2' : '2.1.1',
            '2.5.1' : '2.1.1',
            '2.5.0' : '2.1.1',
            '2.2.0' : '2.1.1',
            '2.1.1' : '2.1.1',
            '2.0.1' : '2.1.1'
        };
        return map[cbVersion];
    }

    /**
     * overrides the default options.
     * @param options
     */
    function setOptions(options) {
        Object.keys(options).map(function(key) {
                _options[key] = options[key];
            });
    }

    /**
     * Generates the current working directory absolute path for the child process to be executed
     * @returns {string}
     */
    function getCwd() {
        var cwd = path.join(__dirname, versionMap(_options.version));

        if (!fs.existsSync(cwd)) {
            throw new Error('wrong version number or unsupported: ' + _options.version);
        }

        return cwd;
    }

    /**
     * Returns the common arguments for the child process from the cb config.
     * This excludes the source and destination arguments
     * @returns {*[]}
     */
    function getArgs(isBackup) {
        var ret = [
            '-b ' + _options.bucketSource,
            '-u ' + _options.user,
            '-p ' + _options.password,
            '-t ' + _options.threads
        ];

        if (isBackup) {
            ret.push('-m ' + _options.mode);
        }

        return ret;
    }

    /**
     * Wrapper
     *
     * @param command
     * @param cwd
     * @param callback
     */
    function execProcess(command, cwd, callback) {
        childProcess.exec(command.join(' '), {cwd: cwd}, function(err, stdout, stderr) {
            if (err) {
                return callback(err, null);
            }

            callback(false, stderr);
        });
    }

    return {
        cbBackup: function(options, callback) {
            setOptions(options);

            var cwd = getCwd(),
                command = getArgs(true);

            command.unshift(path.join(cwd, 'cbbackup'));
            command.push(_options.cbUrl);
            command.push(_options.backupPath);

            execProcess(command, cwd, callback);
        },

        cbRestore: function(options, callback) {
            setOptions(options);

            var cwd = getCwd(),
                command = getArgs(false);

            command.unshift(path.join(cwd, 'cbrestore'));
            command.push(_options.backupPath);
            command.push(_options.cbUrl);

            execProcess(command, cwd, callback);
{'fraction_non_alphanumeric': 0.14723456411827793, 'fraction_numerical': 0.018538379040907, 'mean_word_length': 1.6028708133971292, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4402756', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1983, 'n_words': 431}