-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstatic_code_analysis.js
More file actions
91 lines (84 loc) · 2.18 KB
/
Copy pathstatic_code_analysis.js
File metadata and controls
91 lines (84 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const fs = require('fs');
const nrc = require('node-run-cmd');
const natural = require('natural');
const mailer = require('./mailer.js');
// Connect to S3
const Aws = require('aws-sdk');
const s3 = new Aws.S3();
const bucket = 'sourcecodestore';
var exports = {};
function finalScore(count) {
var badness = 0;
for(var type in count) badness += count[type];
return badness;
}
function getScore(data, done) {
var count = {
"error" : 0,
"warning" : 0,
"style" : 0,
"performance" : 0,
"portability" : 0,
"information" :0
};
var sentences = data.split("\n");
var wordTokenizer = new natural.WordTokenizer();
var types = ["[(]error[)]", "[(]warning[)]", "[(]style[)]", "[(]performance[)]", "[(]portability[)]", "[(]information[)]"];
for (var i = 0; i < sentences.length - 1; i++) {
for (var j = 0; j < types.length; j++) {
if (sentences[i].match(types[j]) != null) {
count[types[j].slice(3, -3)] += 1;
break;
}
}
}
console.log(count);
var badness = finalScore(count);
return done(badness);
}
function readMyFile(path, scapath, email) {
let content;
try {
content = fs.readFileSync(path, 'utf-8');
} catch(ex) {
console.log(ex);
}
getScore(content, function(badness) {
console.log('getScore:' + badness);
console.log(scapath);
s3.putObject({
Bucket : bucket,
Key : scapath,
Body : "Badness score : " + badness,
}, function(err, data) {
if(err) throw err;
else console.log('Badness pushed');
});
// Send email to student
mailer.sendMail({
from : '"Automated Lab Assessment" <automatedlabassessment@gmail.com>',
to : email,
subject : 'Your Lab Assessment Score',
html : "<p>Your score is " + badness
}, function(data) {
res.sendStatus(200);
});
});
}
exports.analyseFile = function(codeFile, errorFile, scapath, email) {
s3.getObject({
Bucket : bucket,
Key : 'file1.cpp'
}, function(err, data) {
if(err) console.error(err);
else {
fs.writeFile("./temp/codeFile.cpp", data.Body.toString(), function(err) {
if(err) throw err;
nrc.run('cppcheck ./temp/codeFile.cpp' + ' 2> ' + errorFile).then(function(exitCodes) {
readMyFile(errorFile, scapath, email);
});
});
}
});
};
module.exports = exports;