<commit_before>var cradle  = require('cradle');
var express = require('express');
var fs      = require('fs');
var spawner = require('spawner').create();
var util    = require('util');
var uuid    = require('node-uuid');

var app = express.createServer(
  express.logger(),
  express.cookieParser(),
  express.session({ secret: process.env.SECRET }),
  require('connect-form')({ keepExtensions: true })
);

function log_action(uuid, message) {
  console.log(uuid + ': ' + message);
}

function log_error(uuid, message) {
  log_action(uuid, 'ERROR: ' + message);
}

// connect to couchdb
var couchdb_url = require('url').parse(process.env.CLOUDANT_URL);
var couchdb_options = couchdb_url.auth ?
  { auth: { username: couchdb_url.auth.split(':')[0], password: couchdb_url.auth.split(':')[1] }  } :
  { }
var db = new(cradle.Connection)(couchdb_url.hostname, couchdb_url.port || 5984, couchdb_options).database('make');
db.create(function(){});

// POST /make starts a build
app.post('/make', function(request, response, next) {

  // require a form
  if (! request.form) {
    console.log('invalid form');
    response.write('invalid form');
    response.send(500);
  } else {

    // form handler
    request.form.complete(function(err, fields, files) {

      // if there's an error, pass through to the next handler
      if (err) { return next(err); }

      // match on the shared secret
      if (fields.secret != process.env.SECRET) {
        response.write('invalid secret');
        response.send(500);
      } else {

        var id      = uuid();
        var command = fields.command;
        var prefix  = fields.prefix;
        var deps    = JSON.parse(fields.deps);

        // create a couchdb documents for this build
        log_action(id, 'saving to couchdb');
        db.save(id, { command:command, prefix:prefix, deps:deps }, function(err, doc) {
          if (err) { log_error(id, util.inspect(err)); return next(err); }

          // save the input tarball as an attachment
          log_action(id, 'saving attachment - [id:' + doc.id + ', rev:' + doc.rev + ']')
          fs.createReadStream(files.code.path).pipe(db.saveAttachment(
            {id: doc.id,
             rev: doc.rev},
            {name:'input',
            'Content-Type': 'application/octet-stream'},
            function(err, data) {
              if (err) {
                // work around temporary problem with cloudant and document
                // conflicts
                if (err.error != 'conflict') {
                  log_error(id, err.reason);
                  return next(err.reason);
                }
              }

              // spawn bin/make with this build id<commit_msg>Deal with empty dependencies, fixes #18<commit_after>var cradle  = require('cradle');
var express = require('express');
var fs      = require('fs');
var spawner = require('spawner').create();
var util    = require('util');
var uuid    = require('node-uuid');

var app = express.createServer(
  express.logger(),
  express.cookieParser(),
  express.session({ secret: process.env.SECRET }),
  require('connect-form')({ keepExtensions: true })
);

function log_action(uuid, message) {
  console.log(uuid + ': ' + message);
}

function log_error(uuid, message) {
  log_action(uuid, 'ERROR: ' + message);
}

// connect to couchdb
var couchdb_url = require('url').parse(process.env.CLOUDANT_URL);
var couchdb_options = couchdb_url.auth ?
  { auth: { username: couchdb_url.auth.split(':')[0], password: couchdb_url.auth.split(':')[1] }  } :
  { }
var db = new(cradle.Connection)(couchdb_url.hostname, couchdb_url.port || 5984, couchdb_options).database('make');
db.create(function(){});

// POST /make starts a build
app.post('/make', function(request, response, next) {

  // require a form
  if (! request.form) {
    console.log('invalid form');
    response.write('invalid form');
    response.send(500);
  } else {

    // form handler
    request.form.complete(function(err, fields, files) {

      // if there's an error, pass through to the next handler
      if (err) { return next(err); }

      // match on the shared secret
      if (fields.secret != process.env.SECRET) {
        response.write('invalid secret');
        response.send(500);
      } else {

        var id      = uuid();
        var command = fields.command;
        var prefix  = fields.prefix;
        var deps    = [];
        if (fields.deps) {
          deps = JSON.parse(fields.deps);
        }

        // create a couchdb documents for this build
        log_action(id, 'saving to couchdb');
        db.save(id, { command:command, prefix:prefix, deps:deps }, function(err, doc) {
          if (err) { log_error(id, util.inspect(err)); return next(err); }

          // save the input tarball as an attachment
          log_action(id, 'saving attachment - [id:' + doc.id + ', rev:' + doc.rev + ']')
          fs.createReadStream(files.code.path).pipe(db.saveAttachment(
            {id: doc.id,
             rev: doc.rev},
            {name:'input',
            'Content-Type': 'application/octet-stream'},
            function(err, data) {
              if (err) {
                // work around temporary problem with cloudant and document
                // conflicts
                if (err.error != 'conflict') {
                  log_error(id, err.reason);
                  return next(err.reason);
                }
              }

              // spawn bin/make with this build id
{'fraction_non_alphanumeric': 0.14712348845731038, 'fraction_numerical': 0.004763649688530597, 'mean_word_length': 2.433333333333333, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1621935', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1629, 'n_words': 397}