Skip to content

Commit

Permalink
Fix pi chart.
Browse files Browse the repository at this point in the history
Add build/package.json for npm install.
Refactoring.
  • Loading branch information
ArnonEilat committed May 17, 2014
1 parent 1f76a88 commit f465537
Show file tree
Hide file tree
Showing 45 changed files with 838 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/repositories/
build/node_modules/

# ignore backups
*~
Expand Down
17 changes: 17 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Test and backup files
test*
*~
*.bak
*.rar

# Credentials
wiki_credentials.txt
wiki_botconf.txt
.mediawiki*

# Various project files
.settings
.project

build/node_modules

15 changes: 7 additions & 8 deletions build/commitsToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var spawn = require('child_process').spawn;
var colors = require('colors');

/**
* @description Get commits for path in array.
* @param {string} path of file to get its log.
* @description Get JSON array of commits for path in array.
* @param {Repository} repo of file to get its log.
* @param {Function} callBack to run with the result.
* The result look like this:
* [{
Expand All @@ -15,13 +15,12 @@ var colors = require('colors');
* ]
* @returns {void}
*/
function commitsArray(path, callBack) {
if (path === '')
throw new Error('\ngit-dir must be set.\nUse setGitDir to set it.'.red);
function commitsArray(repo, callBack) {

var result = '';
var separator = ' qqqqqqqq ';
var args = [
'--git-dir=' + path
'--git-dir=' + repo.getDotGitFolder()
, "log"
, "--pretty=format:%H" // commit hash
+ separator + "%an" // author name
Expand Down Expand Up @@ -68,14 +67,14 @@ function commitsArray(path, callBack) {
/**
* @type {Array}
*/
var json = JSON.parse(jsonStr);
var jsonArray = JSON.parse(jsonStr);
}
catch (e) {
// debugger;
console.log(e.toString().red);
console.log(jsonStr.red);
}
callBack(json);
callBack(jsonArray, repo);
});
process.stderr.on('data', function(data) {
var buff = new Buffer(data);
Expand Down
89 changes: 21 additions & 68 deletions build/getCommitsData.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,22 @@
var fs = require('fs');
var path = require('path');
var util = require('util');
var jsonSanitizer = require('./jsonSanitizer');
var spawn = require('child_process').spawn;
var jsonSanitizer = require('./jsonSanitizer');
var commitsToJson = require('./commitsToJson');

var eKnights = require('./repositories').repositories;
var numStat = require('./numStat');
var colors = require('colors');
//var repositoriesData = require('../eKnightsData').eKnightsData;



/***
*
* Column1 = inserted
* Column2 = removed
* columns are separeted by tab (\t)
* @param {string} lineStat
* @returns {Object}
*/
var parseLineStat = function(lineStat) {
var line = lineStat.split('\t');
var stats = new Object();

stats.path = line[2];

var additions = parseInt(line[0], 10);
var deletions = parseInt(line[1], 10);
if (!isNaN(additions)) {
stats.total = additions;
stats.additions = additions;
if (!isNaN(deletions)) {
stats.deletions = deletions;
stats.total += deletions;
}
} else if (!isNaN(deletions)) {
stats.total += deletions;
stats.deletions = deletions;
}
return stats;
};

/**
* @param {string} numstat
* @returns {undefined}
* @description write File
* @param {type} commits
* @param {Repository} repo
* @returns {void}
*/
var parseNumStat = function(numstat) {
var lines = numstat.split('\n');
var statArr = new Array();
for (var i = 0; i < lines.length; i++) {
var s = parseLineStat(lines[i]);
statArr.push(s);
}
var totalSum = 0;
for (var i = 0; i < statArr.length; i++) {
if (statArr[i].total) {
totalSum += statArr[i].total;
}
}

return {
'total': totalSum,
'files': statArr
};
};


//var dotGitPath = 'repositories/Open-Knesset/.git';
var dotGitPath = '/home/arnon/apache_public_html/hasadna.github.io/.git';
commitsToJson.commitsArray(dotGitPath, function(commits) {
function writeFile(commits, repo) {
var data = new Array();

function addSt(i) {
Expand All @@ -76,14 +25,13 @@ commitsToJson.commitsArray(dotGitPath, function(commits) {
// Turn strings into dates, and then subtract them to get a value that is either negative, positive, or zero.
return new Date(b.date) - new Date(a.date);
});
// console.log(JSON.stringify(data));
fs.writeFile("testData.json", JSON.stringify(data), function(err) {
fs.writeFile(outputPath + repo.getFolderName() + ".json", JSON.stringify(data), function(err) {
});
return;
}
var result = '';
var args = new Array(
'--git-dir=' + dotGitPath
'--git-dir=' + repo.getDotGitFolder()
, 'diff'
, '--numstat'
, commits[i].commit
Expand All @@ -95,9 +43,8 @@ commitsToJson.commitsArray(dotGitPath, function(commits) {
});
process.on('exit', function(code) { // When exit run the callback with the results.
// console.log(commits[i - 1].commit + ' ' + commits[i].commit);
// console.log(result);

var ns = parseNumStat(result);
var ns = numStat.parseNumStat(result);
ns.sha = commits[i].commit;
ns.body = commits[i].body;
ns.date = commits[i].date;
Expand All @@ -106,12 +53,18 @@ commitsToJson.commitsArray(dotGitPath, function(commits) {
addSt(--i);
});
process.stderr.on('data', function(data) {

var buff = new Buffer(data);
// console.log(buff.toString('utf8').red);
// console.log(JSON.stringify(args));
//console.log(buff.toString('utf8').red);
});
}

addSt(commits.length - 1);
});
}



var outputPath = './';

for (var i = 0; i < 1; i++) {
commitsToJson.commitsArray(eKnights[i].getMainRepository(), writeFile);
}
25 changes: 12 additions & 13 deletions build/getPiData.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var fs = require('fs');
var path = require('path');
//var util = require('util');
var posix = require('posix');
//var colors = require('colors');
var rootChecker = require('rootChecker');
var lnCounter = require('lineCounter').createLineCounter();
var github = require('github');
var repositoriesData = require('../eKnightsData').eKnightsData;
var index = repositoriesData.length - 1;
var rootChecker = require('./rootChecker');
var lnCounter = require('./lineCounter').createLineCounter();
var github = require('./github');
var eKnights = require('./repositories').repositories;
var index = eKnights.length - 1;

rootChecker.isRootUser(function() {
/**
Expand All @@ -19,6 +18,7 @@ rootChecker.isRootUser(function() {
*/
posix.setrlimit('nofile', {soft: 100000, hard: 100000});
});

/**
* @param {string} filename Full path of file
* @returns
Expand Down Expand Up @@ -77,7 +77,7 @@ var mapData = function(folderToMap, cb) {

var obj = dirTree(__dirname + "/repositories/" + folderToMap);
var fileWriter = function() {
fs.writeFile("../data/" + repositoriesData[index].slug + "-pi.json", JSON.stringify(obj), function(err) {
fs.writeFile("../data/" + eKnights[index].slug + "-pi.json", JSON.stringify(obj), function(err) {
cb(--index);
if (err)
console.log(err);
Expand All @@ -94,18 +94,17 @@ var mapData = function(folderToMap, cb) {
var repositoriesPath = __dirname + "/repositories/";



function main() {
if (index !== -1) {
// console.log('i: ' + index);
var folderName = github.getFolderName(repositoriesData[index].github_repo);
github.setDotGitPath(repositoriesPath + folderName + '/.git');
github.cloneOrPull(repositoriesData[index].github_repo + '.git', function() {
var folderName = eKnights[index].getMainRepository().getFolderName();
var dotGitFolder = eKnights[index].getMainRepository().getDotGitFolder();
github.setDotGitPath(dotGitFolder);

github.cloneOrPull(repositoriesPath + folderName, function() {
mapData(folderName, main);
}, true);
}
}

main();


27 changes: 20 additions & 7 deletions build/node_modules/github.js → build/github.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use strict";
var spawn = require('child_process').spawn;
var colors = require('colors');
var fs = require('fs');
var path = require('path');
var jsonSanitizer = require('jsonSanitizer');
var jsonSanitizer = require('./jsonSanitizer');


/**
Expand Down Expand Up @@ -75,7 +76,7 @@ parentPath = findParentPath(parentPath);
*/
var getFolderName = function(str) {
var lastSlash = str.lastIndexOf("/");
var end = str.length - 4;
var end = str.length;
if (str.indexOf('.git') === -1)
end = str.length;

Expand All @@ -95,15 +96,18 @@ var cloneOrPull = function(repoURL, callBack, verbose) {
var clone = false;
var gitFolder = '--git-dir=' + parentPath + '/repositories/' + containingFolder + '/.git';

if (path.existsSync(parentPath + '/repositories/' + containingFolder)) // If repository exist pull it, otherwise clone it.
if (fs.existsSync(parentPath + '/repositories/' + containingFolder)) // If repository exist pull it, otherwise clone it.
var process = spawn('git', [gitFolder, 'pull']);
else {
var process = spawn('git', ['clone', repoURL]);
clone = true;
}

process.on('exit', function(code) { // When exit run the callback.
if (clone === true) {
fs.renameSync(parentPath + '/' + containingFolder, parentPath + '/repositories/' + containingFolder);
var src = parentPath + '/' + containingFolder;
var des = parentPath + '/repositories/' + containingFolder;
fs.renameSync(src, des);
console.log(containingFolder + ' moved from: \n' + parentPath + '\nTo:\n' + parentPath + '/repositories/');
}
callBack(code, containingFolder);
Expand Down Expand Up @@ -158,6 +162,11 @@ var getLogForFile = function(fullPath, callBack) {
var result = '';
var separator = ' qqqqqqqq ';

var relativePath = getRelativePath(fullPath);
if (relativePath === "")
relativePath = containingFolder + '/';
// console.log("fullPath: " + fullPath);

var args = new Array(
'--git-dir=' + dotGitPath,
"log",
Expand All @@ -168,7 +177,7 @@ var getLogForFile = function(fullPath, callBack) {
+ "%f" + separator // sanitized subject line, suitable for a filename
+ "%b", // body
"--",
getRelativePath(fullPath)
fullPath
);

lock++;
Expand Down Expand Up @@ -201,10 +210,13 @@ var getLogForFile = function(fullPath, callBack) {
+ '"},';
}

jsonStr = jsonStr.substring(0, jsonStr.length - 1) + ']'; // Remove the last comma
jsonStr = jsonStr.substring(0, jsonStr.length - 1) // Remove the last comma
+ "]";

var json = JSON.parse(jsonStr);
if (jsonStr === "]") // For no data
jsonStr = "[]";

var json = JSON.parse(jsonStr);

callBack(json);
lock--;
Expand All @@ -224,3 +236,4 @@ module.exports.setDotGitPath = setDotGitPath;
module.exports.cloneOrPull = cloneOrPull;
module.exports.getLogForFile = getLogForFile;
module.exports.getFolderName = getFolderName;

File renamed without changes.
1 change: 0 additions & 1 deletion build/node_modules/.bin/rimraf

This file was deleted.

18 changes: 5 additions & 13 deletions build/node_modules/colors/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f465537

Please sign in to comment.