Skip to content

Commit

Permalink
Replace external binaries with js code
Browse files Browse the repository at this point in the history
  • Loading branch information
tstarck committed Nov 26, 2014
1 parent 1055997 commit d9cf00c
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 31 deletions.
50 changes: 19 additions & 31 deletions lib/adapters/fs.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
var _ = require('underscore');
var async = require('async');
var fs = require('fs');
var path = require('path');
var os = require('os');
var child_process = require('child_process');
var _ = require('underscore');
var async = require('async');
var fs = require('fs');
var log = require('../log');
var mkdirp = require('mkdirp');
var os = require('os');
var path = require('path');
var rimfar = require('rimraf');

function initialize(config, callback) {
var adapter = new FsAdapter(config);

async.series([
_.partial(adapter.mkdirp, adapter.path),
_.partial(adapter.mkdirp, path.join(adapter.path, 'lock')),
_.partial(adapter.mkdirp, path.join(adapter.path, 'task')),
_.partial(adapter.mkdirp, path.join(adapter.path, 'disabled')),
_.partial(adapter.mkdirp, path.join(adapter.path, 'due'))
_.partial(mkdirp, path.join(adapter.path, 'lock')),
_.partial(mkdirp, path.join(adapter.path, 'task')),
_.partial(mkdirp, path.join(adapter.path, 'disabled')),
_.partial(mkdirp, path.join(adapter.path, 'due'))
], function(error) {
if (error) {
callback(new Error('Error while ensuring destination directories: ' + error));
Expand Down Expand Up @@ -118,7 +119,7 @@ FsAdapter.prototype.completeTask = function(task, callback) {
}.bind(this));
};

/**
/*
* Helper functions
*/

Expand All @@ -128,12 +129,6 @@ FsAdapter.prototype.generate_id = function() {
return id.join('');
};

FsAdapter.prototype.mkdirp = function(dir, callback) {
// TODO make this.. umm.. windows compatible? :D

child_process.execFile('/bin/mkdir', ['-p', dir], { timeout: 100 }, callback);
};

FsAdapter.prototype.lock_task_with_new_id = function(task, callback, failure_count) {
failure_count = failure_count || 0;

Expand Down Expand Up @@ -276,7 +271,7 @@ FsAdapter.prototype.store_disabled_task_string_for_task = function(task, string,

FsAdapter.prototype.store_task_string_to_file = function(file, string, callback) {
async.series([
_.partial(FsAdapter.prototype.mkdirp, path.dirname(file)),
_.partial(mkdirp, path.dirname(file)),
_.partial(fs.writeFile, file, string, { encoding: 'utf8' })
], this.generate_result_callback_that_returns_nothing(callback));
};
Expand All @@ -285,7 +280,7 @@ FsAdapter.prototype.create_due_file_for_task = function(task, callback) {
var file = this.return_due_file_for_task(task);

async.series([
_.partial(FsAdapter.prototype.mkdirp, path.dirname(file)),
_.partial(mkdirp, path.dirname(file)),
_.partial(fs.writeFile, file, '', { encoding: 'utf8' })
], this.generate_result_callback_that_returns_nothing(callback));
};
Expand Down Expand Up @@ -413,19 +408,12 @@ FsAdapter.prototype.generate_result_callback_that_returns_nothing = function(cal
}
};

// Test helpers
/*
* Test helpers
*/

FsAdapter.prototype.testInterfaceWipeDatastore = function(callback) {
var path = this.path;

fs.exists(path, function(exists) {
if (exists) {
child_process.execFile('/bin/rm', ['-Rf', path], { timeout: 5000 }, callback);
}
else {
callback();
}
});
rimraf(this.path, callback);
};

FsAdapter.prototype.testInterfaceGatherEnabledJobMetaList = function(callback) {
Expand Down
21 changes: 21 additions & 0 deletions lib/adapters/mkdirp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright 2010 James Halliday ([email protected])

This project is free software released under the MIT/X11 license:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
97 changes: 97 additions & 0 deletions lib/adapters/mkdirp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var path = require('path');
var fs = require('fs');

module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;

function mkdirP (p, opts, f, made) {
if (typeof opts === 'function') {
f = opts;
opts = {};
}
else if (!opts || typeof opts !== 'object') {
opts = { mode: opts };
}

var mode = opts.mode;
var xfs = opts.fs || fs;

if (mode === undefined) {
mode = 0777 & (~process.umask());
}
if (!made) made = null;

var cb = f || function () {};
p = path.resolve(p);

xfs.mkdir(p, mode, function (er) {
if (!er) {
made = made || p;
return cb(null, made);
}
switch (er.code) {
case 'ENOENT':
mkdirP(path.dirname(p), opts, function (er, made) {
if (er) cb(er, made);
else mkdirP(p, opts, cb, made);
});
break;

// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
xfs.stat(p, function (er2, stat) {
// if the stat fails, then that's super weird.
// let the original error be the failure reason.
if (er2 || !stat.isDirectory()) cb(er, made)
else cb(null, made);
});
break;
}
});
}

mkdirP.sync = function sync (p, opts, made) {
if (!opts || typeof opts !== 'object') {
opts = { mode: opts };
}

var mode = opts.mode;
var xfs = opts.fs || fs;

if (mode === undefined) {
mode = 0777 & (~process.umask());
}
if (!made) made = null;

p = path.resolve(p);

try {
xfs.mkdirSync(p, mode);
made = made || p;
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
made = sync(path.dirname(p), opts, made);
sync(p, opts, made);
break;

// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
var stat;
try {
stat = xfs.statSync(p);
}
catch (err1) {
throw err0;
}
if (!stat.isDirectory()) throw err0;
break;
}
}

return made;
};
6 changes: 6 additions & 0 deletions lib/adapters/rimraf/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Authors sorted by whether or not they're me.
Isaac Z. Schlueter <[email protected]> (http://blog.izs.me)
Wayne Larsen <[email protected]> (http://github.com/wvl)
ritch <[email protected]>
Marcel Laverdet
Yosef Dinerstein <[email protected]>
23 changes: 23 additions & 0 deletions lib/adapters/rimraf/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright 2009, 2010, 2011 Isaac Z. Schlueter.
All rights reserved.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit d9cf00c

Please sign in to comment.