<commit_before>//Load necessary modules
var fs     = require( 'fs'         );
var Rsync  = require( 'rsync'      );
var bunyan = require( 'bunyan'     );
var color  = require( 'ansi-color' ).set;

//Function to set all global variables
function setGlobalVariables() {
    //Define global variables
    GLOBAL.handler           = null;
    GLOBAL.errors_log        = null;
    GLOBAL.rsync             = null;
    GLOBAL.user              = null;
    GLOBAL.origin            = null;
    GLOBAL.target            = null;
    GLOBAL.machine           = null;
    GLOBAL.key               = null;
    GLOBAL.password          = null;
    GLOBAL.OptionsForSFTP    = null;
    GLOBAL.argv              = null;
    GLOBAL.connection        = null;
    GLOBAL.config            = null;
    GLOBAL.active_connection = false;
    GLOBAL.active_sftp       = null;

    //Set files queues
    GLOBAL.fileActionsQueue = [];
}

//Function to check script arguments
function checkScriptArgs() {
    //Initialize vars
    var re, tmp;

    //Check if valid args
    if (argv.length < 5) {
        console.log("\n" + color('Usage:', 'yellow') +
            ' ./syncmaster ' + color('USER@MACHINE', 'green') +
            ' ' + color('ORIGIN_PATH', 'red') + ' ' +
            color('DESTINATION_PATH', 'blue') + "\n"
        );
        //End process
        return;
    }

    //Retrieve args data
    origin = argv[3];
    target = argv[4];

    //Parse user/machine
    re = /(.+)@(.+)/;
    tmp = argv[2].match(re);

    //Check valid user and machine
    if (tmp && tmp.length === 3) {
        user = tmp[1];
        machine = tmp[2];
    }
    else {
        //Log error
        errors_log.error('Invalid user@machine data: ' + argv[2]);
        //Exit with error
        console.log("\n" + color('Invalid user@machine data: ', 'red') + ' ' + color(argv[2], 'blue') + "\n");
        //End process
        return;
    }
    //Verify if key/password is configured for user/machine
    if (config[user] && config[user][machine] && config[user][machine].key && config[user][machine].password) {
        key = config[user][machine].key;
        password = config[user][machine].password;
    }
    else {
        //Log error
        errors_log.error('Failed to retrieve ssh key/password for user ' + user + ' to machine ' + machine);
        //Exit with error
        console.log("\n" + color('Failed to retrieve ssh key/password for user' + user + ' to machine ' + machine, 'red') + "\n");
        //End process
        return;
    }
}

//Lib with auxiliary functions to load start env
function loadSyncEnv(machine, origin, target, user, password, key) {
    //Setup options for sending files through connection
    OptionsForSFTP = {
        host: machine,
        port: 22,
        username: user,
        passphrase: password,
        privateKey: fs.readFileSync(key)
    };
    //Sync origin with target destination first
    rsync = new Rsync()
        .shell('ssh')
        .flags('arz')
        .delete()
        .quiet()
        .source(origin)
        .destination(user + '@' + machine + ':' + target)
        .debug(false);
}

function initializeLogs() {
    //Initialize logs
    errors_log = bunyan.createLogger({
        name: 'sycmaster',
        streams: [{
            type: 'rotating-file',
            path: 'logs/syncmaster.log',
            period: '1d', // daily rotation
            count: 30, // keep 30 back copies
            level: 'debug'
        }]
    });
}

//Expose public API
module.exports = {
    loadSyncEnv: loadSyncEnv,
    checkScriptArgs: checkScriptArgs,
    initializeLogs: initializeLogs,
    setGlobalVariables: setGlobalVariables
};<commit_msg>Added rsync from target to origin<commit_after>//Load necessary modules
var fs     = require( 'fs'         );
var Rsync  = require( 'rsync'      );
var bunyan = require( 'bunyan'     );
var color  = require( 'ansi-color' ).set;

//Function to set all global variables
function setGlobalVariables() {
    //Define global variables
    GLOBAL.handler           = null;
    GLOBAL.errors_log        = null;
    GLOBAL.rsync             = null;
    GLOBAL.rsync2            = null;
    GLOBAL.user              = null;
    GLOBAL.origin            = null;
    GLOBAL.target            = null;
    GLOBAL.machine           = null;
    GLOBAL.key               = null;
    GLOBAL.password          = null;
    GLOBAL.OptionsForSFTP    = null;
    GLOBAL.argv              = null;
    GLOBAL.connection        = null;
    GLOBAL.config            = null;
    GLOBAL.active_connection = false;
    GLOBAL.active_sftp       = null;

    //Set files queues
    GLOBAL.fileActionsQueue = [];
}

//Function to check script arguments
function checkScriptArgs() {
    //Initialize vars
    var re, tmp;

    //Check if valid args
    if (argv.length < 5) {
        console.log("\n" + color('Usage:', 'yellow') +
            ' ./syncmaster ' + color('USER@MACHINE', 'green') +
            ' ' + color('ORIGIN_PATH', 'red') + ' ' +
            color('DESTINATION_PATH', 'blue') + "\n"
        );
        //End process
        return;
    }

    //Retrieve args data
    origin = argv[3];
    target = argv[4];

    //Parse user/machine
    re = /(.+)@(.+)/;
    tmp = argv[2].match(re);

    //Check valid user and machine
    if (tmp && tmp.length === 3) {
        user = tmp[1];
        machine = tmp[2];
    }
    else {
        //Log error
        errors_log.error('Invalid user@machine data: ' + argv[2]);
        //Exit with error
        console.log("\n" + color('Invalid user@machine data: ', 'red') + ' ' + color(argv[2], 'blue') + "\n");
        //End process
        return;
    }
    //Verify if key/password is configured for user/machine
    if (config[user] && config[user][machine] && config[user][machine].key && config[user][machine].password) {
        key = config[user][machine].key;
        password = config[user][machine].password;
    }
    else {
        //Log error
        errors_log.error('Failed to retrieve ssh key/password for user ' + user + ' to machine ' + machine);
        //Exit with error
        console.log("\n" + color('Failed to retrieve ssh key/password for user' + user + ' to machine ' + machine, 'red') + "\n");
        //End process
        return;
    }
}

//Lib with auxiliary functions to load start env
function loadSyncEnv(machine, origin, target, user, password, key) {
    //Setup options for sending files through connection
    OptionsForSFTP = {
        host: machine,
        port: 22,
        username: user,
        passphrase: password,
        privateKey: fs.readFileSync(key)
    };

    /*console.log( origin );
    console.log( target );

    process.exit(1);*/

    //Sync target with origin
    rsync = new Rsync()
        .shell('ssh')
        .flags('avz')
        .delete()
        .quiet()
        .source(origin)
        .destination(user + '@' + machine + ':' + target)
        .debug(false);

    //Sync origin with target
    rsync2 = new Rsync()
        .shell('ssh')
        .flags('avz')
        .delete()
        .quiet()
        .source(user + '@' + machine + ':' + target)
        .destination(origin)
        .debug(false);
}

function initializeLogs() {
    //Initialize logs
    errors_log = bunyan.createLogger({
        name: 'sycmaster',
        streams: [{
            type: 'rotating-file',
            path: 'logs/syncmaster.log',
            period: '1d', // daily rotation
            count: 30, // keep 30 back copies
            level: 'debug'
        }]
    });
}

//Expose public API
module.exports = {
    loadSyncEnv: loadSyncEnv,
    checkScriptArgs: checkScriptArgs,
    initializeLogs: initializeLogs,
    setGlobalVariables: setGlobalVariables
};
{'fraction_non_alphanumeric': 0.13575600259571707, 'fraction_numerical': 0.004542504866969501, 'mean_word_length': 1.9024482109227872, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6131460', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2271, 'n_words': 590}