-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdcs-get-node
More file actions
164 lines (152 loc) · 3.93 KB
/
dcs-get-node
File metadata and controls
164 lines (152 loc) · 3.93 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/var/tmp/dcs-get/bin/node-0.4.2
// Useful settings
var settings = {
'install_dir': '/var/tmp/dcs-get',
'base_host': 'backus.uwcs.co.uk',
'base_path': '/dcs-get/',
'debug': true
};
// Global variable for packages
var packages;
// Node modules
var http = require('http'),
fs = require('fs'),
util = require('util'),
spawn = require('child_process').spawn,
exec = require('child_process').exec;
// Grab the package list from backus
http.get({
host: settings['base_host'],
port: 80,
path: settings['base_path']+'packages.json'
}, function(res) {
res.setEncoding('utf8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
try {
packages = JSON.parse(data);
packages = sortObject(packages);
} catch (e) {
console.log("Error: Problem with package data");
if (settings.debug) console.log(e);
return;
}
// We have the package data and are ready to go
handleCLI();
});
}).on('error', function(e) {
console.log("Error: Problem downloading package list from backus");
if (settings.debug) console.log(e);
});
var handleCLI = function () {
// Deal with command line input
if (process.argv[2] === undefined) {
console.log("Try dcs-get help");
return;
}
switch (process.argv[2]) {
case "list":
case "l":
var package;
for (packageName in packages) {
package = packages[packageName];
if (package.type === undefined || package.type != "dev") {
console.log(packageName+" - "+package.description+" - Versions: "+package.version.join(", "));
}
}
break;
case "list-dev":
case "ld":
var package;
for (packageName in packages) {
package = packages[packageName];
console.log(packageName+" - "+package.description+" - Versions: "+package.version.join(", "));
}
break;
case "clean":
case "c":
exec('rm -rf '+settings['install_dir']);
console.log("dcs-get completely removed");
break;
case "search":
case "s":
case "install":
case "i":
case "reinstall":
case "r":
case "list-installed":
case "li":
case "gensymlinks":
case "g":
case "package":
case "p":
case "upload":
case "u":
case "help":
console.log("TODO");
break;
case "commands":
console.log("commands search install reinstall list list-dev clean gensymlinks package help upload");
break;
default:
console.log("Try dcs-get help");
}
}
// Function to download a file, while extracting it
// TODO: Add callbacks (progress and complete)
var downloadPackage = function (package) {
http.get({
host: settings['base_host'],
port: 80,
path: settings['base_path']+'packages/'+package+'.tar.gz'
}, function(res) {
var downloaded = 0;
res.on('data', function (chunk) {
downloaded += chunk.length;
});
var progress = setInterval(function() {
console.log(Math.round((downloaded/res.headers['content-length'])*100)+'%');
}, 50);
res.on('end', function () {
clearInterval(progress);
console.log("Package downloaded");
});
// Save file
util.pump(res, fs.createWriteStream(settings['install_dir']+'/downloads/'+package+'.tar.gz'));
// Extract contents
var tar = spawn('tar', ['zx','-C', settings['install_dir']]);
util.pump(res, tar.stdin);
// Calculate md5sum
var md5sum = spawn('md5sum');
util.pump(res, md5sum.stdin);
var md5hash = '';
md5sum.stdout.on('data', function (chunk) {
md5hash += chunk;
})
md5sum.stdout.on('end', function () {
console.log("File hash: "+md5hash);
});
}).on('error', function(e) {
console.log("Error: Problem downloading package.");
if (settings.debug) console.log(e);
});
};
downloadPackage('git-1.7.3.2');
// Useful functions (not written by me)
function sortObject(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}