<commit_before>// Generated by CoffeeScript 1.8.0
var Application, Manifest, autostop, exec, fs, log, manager, markBroken, randomString, request, saveIcon, sendError, sendErrorSocket, slugify, startedApplications, updateApp;

request = require('request-json');

fs = require('fs');

slugify = require('cozy-slug');

exec = require('child_process').exec;

log = require('printit')({
  prefix: "applications"
});

Application = require('../models/application');

manager = require('../lib/paas').get();

Manifest = require('../lib/manifest').Manifest;

autostop = require('../lib/autostop');

saveIcon = require('../lib/icon').save;

startedApplications = {};

sendError = function(res, err, code) {
  if (code == null) {
    code = 500;
  }
  if (err == null) {
    err = {
      stack: null,
      message: "Server error occured"
    };
  }
  console.log("Sending error to client :");
  console.log(err.stack);
  return res.send(code, {
    error: true,
    success: false,
    message: err.message,
    stack: err.stack
  });
};

sendErrorSocket = function(err) {
  console.log("Sending error through socket");
  return console.log(err.stack);
};

markBroken = function(res, app, err) {
  console.log("Marking app " + app.name + " as broken because");
  console.log(err.stack);
  app.state = "broken";
  app.password = <PASSWORD>;
  if (err.result != null) {
    app.errormsg = err.message + ' :\n' + err.result;
  } else if (err.message != null) {
    app.errormsg = err.message + ' :\n' + err.stack;
  } else {
    app.errormsg = err;
  }
  return app.save(function(saveErr) {
    if (saveErr) {
      return sendError(res, saveErr);
    }
    return res.send({
      app: app,
      error: true,
      success: false,
      message: err.message,
      stack: err.stack
    }, 500);
  });
};

randomString = function(length) {
  var string;
  string = "";
  while (string.length < length) {
    string = string + Math.random().toString(36).substr(2);
  }
  return string.substr(0, length);
};

updateApp = function(app, callback) {
  var data;
  data = {};
  if (app.password == null) {
    data.password = <PASSWORD>(32);
  }
  return manager.updateApp(app, function(err, result) {
    var manifest;
    if (err != null) {
      callback(err);
    }
    if (app.state !== "stopped") {
      data.state = "installed";
    }
    manifest = new Manifest();
    return manifest.download(app, (function(_this) {
      return function(err) {
        if (err != null) {
          return callback(err);
        } else {
          data.permissions = manifest.getPermissions();
          data.widget = manifest.getWidget();
          data.version = manifest.getVersion();
          data.iconPath = manifest.getIconPath();
          data.color = manifest.getColor();
          data.needsUpdate = false;
          return app.updateAttributes(data, function(err) {
            saveIcon(app, function(err) {
              if (err) {
                return console.log(err.stack);
              } else {
                return console.info('icon attached');
              }
            });
            if (err != null) {
              callback(err);
            }
            return manager.resetProxy(callback);
          });
        }
      };
    })(this));
  });
};

module.exports = {
  loadApplication: function(req, res, next, slug) {
    return Application.all({<commit_msg>Built<commit_after>// Generated by CoffeeScript 1.8.0
var Application, Manifest, autostop, exec, fs, log, manager, markBroken, randomString, request, saveIcon, sendError, sendErrorSocket, slugify, startedApplications, updateApp;

request = require('request-json');

fs = require('fs');

slugify = require('cozy-slug');

exec = require('child_process').exec;

log = require('printit')({
  prefix: "applications"
});

Application = require('../models/application');

manager = require('../lib/paas').get();

Manifest = require('../lib/manifest').Manifest;

autostop = require('../lib/autostop');

saveIcon = require('../lib/icon').save;

startedApplications = {};

sendError = function(res, err, code) {
  if (code == null) {
    code = 500;
  }
  if (err == null) {
    err = {
      stack: null,
      message: "Server error occured"
    };
  }
  console.log("Sending error to client :");
  console.log(err.stack);
  return res.send(code, {
    error: true,
    success: false,
    message: err.message,
    stack: err.stack
  });
};

sendErrorSocket = function(err) {
  console.log("Sending error through socket");
  return console.log(err.stack);
};

markBroken = function(res, app, err) {
  console.log("Marking app " + app.name + " as broken because");
  console.log(err.stack);
  app.state = "broken";
  app.password = <PASSWORD>;
  if (err.result != null) {
    app.errormsg = err.message + ' :\n' + err.result;
  } else if (err.message != null) {
    app.errormsg = err.message + ' :\n' + err.stack;
  } else {
    app.errormsg = err;
  }
  return app.save(function(saveErr) {
    if (saveErr) {
      return sendError(res, saveErr);
    }
    return res.send({
      app: app,
      error: true,
      success: false,
      message: err.message,
      stack: err.stack
    }, 500);
  });
};

randomString = function(length) {
  var string;
  string = "";
  while (string.length < length) {
    string = string + Math.random().toString(36).substr(2);
  }
  return string.substr(0, length);
};

updateApp = function(app, callback) {
  var data;
  data = {};
  if (app.password == null) {
    data.password = <PASSWORD>(32);
  }
  return manager.updateApp(app, function(err, result) {
    var manifest;
    if (err != null) {
      callback(err);
    }
    if (app.state !== "stopped") {
      data.state = "installed";
    }
    manifest = new Manifest();
    return manifest.download(app, (function(_this) {
      return function(err) {
        if (err != null) {
          return callback(err);
        } else {
          data.permissions = manifest.getPermissions();
          data.widget = manifest.getWidget();
          data.version = manifest.getVersion();
          data.iconPath = manifest.getIconPath();
          data.color = manifest.getColor();
          data.needsUpdate = false;
          return app.updateAttributes(data, function(err) {
            if (err != null) {
              callback(err);
            }
            return saveIcon(app, function(err) {
              if (err) {
                console.log(err.stack);
              } else {
                console.info('icon attached');
              }
              return manager.resetProxy(callback);
            });
          });
        }
      };
    })(this));
  });
};

module.exports = {
  loadApplication: function(req, res, next, slug) {
    return Application.all({
{'fraction_non_alphanumeric': 0.15693267944716896, 'fraction_numerical': 0.004458314757021846, 'mean_word_length': 2.5052083333333335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6946886', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2099, 'n_words': 466}