<commit_before>/**
 * @file
 * Functionality related to installing Kalabox.
 */


// Requires
var fs = require('fs'),
    url = require('url'),
    http = require('http'),
    https = require('https'),
    exec = require('child_process').exec,
    spawn = require('child_process').spawn,
    flow = require('nue').flow,
    as = require('nue').as,
    logger = require('../../logger');

// State variables:
var vboxVersion,
    vagrantVersion;

/**
 * Downloads a file from the Internet.
 *
 * @param  string file_url
 *   URL to download from.
 * @param  string destination
 *   Directory on the file system to save file to (ending with "/").
 * @param  function callback
 *   Callback to pass progress to.
 */
exports.downloadFile = flow('writeFile')(
  // Check if there is already a file at the destination location
  function writeFile0(file_url, destination, callback) {
    // Get URL details from the full URL.
    var parsedUrl = url.parse(file_url);
    this.data.options = {
      host : parsedUrl.host,
      port : 80,
      path : parsedUrl.pathname
    };
    this.data.file_name = parsedUrl.pathname.split('/').pop();

    // Determine if we're downloading over http or https.
    this.data.httpInterface = http;
    if (parsedUrl.protocol == 'https:') {
      this.data.httpInterface = https;
      this.data.options.port = 443;
    }
    this.data.destination = destination;
    this.data.callback = callback;

    fs.exists(this.data.destination + this.data.file_name, this.async(as(0)));
  },
  // Create stream and start the download.
  function writeFile1(exists){
    if(exists){
      console.log('File already downloaded');
      this.end();
      return;
    }

    this.data.file = fs.createWriteStream(this.data.destination + this.data.file_name);
    this.data.httpInterface.get(this.data.options, this.async(as(0))).on('error', function(error) {
      this.data.callback(error, 100);
    });
  },
  // Report on the downloading progress
  function writeFile2(res) {
    var filesize = res.headers['content-length'];
    var downloaded = 0;
    var done = 0;
    var that = this;
    res.on('data', function(data) {
      that.data.file.write(data, function() {
        downloaded = downloaded + data.length;
        done = (downloaded / filesize) * 100;
        that.data.callback(null, done);
      });
    }).on('end', this.async(as(0)));
  },
  function writeFile3(data) {
    this.data.file.end(this.async());
  },
  function writeFileEnd() {
    if (this.err) {
      this.data.callback({message: this.err.message});
      this.err = null;
      return;
    }

    console.log(this.data.file_name + ' downloaded to ' + this.data.destination);
    this.data.callback(null, 100);
  }
);

exports.downloadKalastack = flow('downloadKalastack')(
  function downloadKalastack0(kalabox_dir, kalastack_filename, kalastack_url, kalastackDir, callback) {
    this.data.kalastack_url = kalastack_url;
    this.data.kalabox_dir = kalabox_dir;
    this.data.kalastack_filename = kalastack_filename;
    this.data.kalastackDir = kalastackDir;
    this.data.callback = callback;

    fs.exists(kalastackDir, this.async(as(0)));
  },
  // See if Kalastack is already downloaded
  function downloadKalastack1(exists) {
    if (exists) {
      console.log('Kalastack already downloaded');<commit_msg>Download dmgs and other files to filename.incomplete, then move them to filename when download has completed<commit_after>/**
 * @file
 * Functionality related to installing Kalabox.
 */


// Requires
var fs = require('fs'),
    url = require('url'),
    http = require('http'),
    https = require('https'),
    exec = require('child_process').exec,
    spawn = require('child_process').spawn,
    flow = require('nue').flow,
    as = require('nue').as,
    logger = require('../../logger');

// State variables:
var vboxVersion,
    vagrantVersion;

/**
 * Downloads a file from the Internet.
 *
 * @param  string file_url
 *   URL to download from.
 * @param  string destination
 *   Directory on the file system to save file to (ending with "/").
 * @param  function callback
 *   Callback to pass progress to.
 */
exports.downloadFile = flow('writeFile')(
  // Check if there is already a file at the destination location
  function writeFile0(file_url, destination, callback) {
    // Get URL details from the full URL.
    var parsedUrl = url.parse(file_url);
    this.data.options = {
      host : parsedUrl.host,
      port : 80,
      path : parsedUrl.pathname
    };
    this.data.file_name = parsedUrl.pathname.split('/').pop();

    // Determine if we're downloading over http or https.
    this.data.httpInterface = http;
    if (parsedUrl.protocol == 'https:') {
      this.data.httpInterface = https;
      this.data.options.port = 443;
    }
    this.data.destination = destination;
    this.data.callback = callback;

    fs.exists(this.data.destination + this.data.file_name, this.async(as(0)));
  },
  // Create stream and start the download.
  function writeFile1(exists){
    if(exists){
      console.log('File already downloaded');
      this.end();
      return;
    }
    // Download as an "incomplete" file.
    this.data.file = fs.createWriteStream(this.data.destination + this.data.file_name + '.incomplete');
    this.data.httpInterface.get(this.data.options, this.async(as(0))).on('error', function(error) {
      this.data.callback(error, 100);
    });
  },
  // Report on the downloading progress
  function writeFile2(res) {
    var filesize = res.headers['content-length'];
    var downloaded = 0;
    var done = 0;
    var that = this;
    res.on('data', function(data) {
      that.data.file.write(data, function() {
        downloaded = downloaded + data.length;
        done = (downloaded / filesize) * 100;
        that.data.callback(null, done);
      });
    }).on('end', this.async(as(0)));
  },
  function writeFile3(data) {
    this.data.file.end(this.async());
  },
  // Move the completed file download to a permenant path.
  function writeFile4(data) {
    exec('mv ' + this.data.destination + this.data.file_name + '.incomplete ' +
      this.data.destination + this.data.file_name, this.async());
  },
  function writeFileEnd() {
    if (this.err) {
      this.data.callback({message: this.err.message});
      this.err = null;
      return;
    }

    console.log(this.data.file_name + ' downloaded to ' + this.data.destination);
    this.data.callback(null, 100);
  }
);

exports.downloadKalastack = flow('downloadKalastack')(
  function downloadKalastack0(kalabox_dir, kalastack_filename, kalastack_url, kalastackDir, callback) {
    this.data.kalastack_url = kalastack_url;
    this.data.kalabox_dir = kalabox_dir;
    this.data.kalastack_filename = kalastack_filename;
    this.data.kalastackDir = kalastackDir;
    this.data.callback = callback;

    fs.exists(kalastackDir, this.async(as(0)));
  },
  // See if Kalastack is already downloaded
  function downloadKalastack1(exists) {
    if (exists) {
      console.log('Kalastack already downloaded');
{'fraction_non_alphanumeric': 0.13750889679715303, 'fraction_numerical': 0.007544483985765124, 'mean_word_length': 3.1063705435417885, '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': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4654546', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2190, 'n_words': 506}