-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathgenerate_nightly_page.js
214 lines (173 loc) · 6.91 KB
/
generate_nightly_page.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Usage: node generate_nightly_page.js [<keyfile> [<outputfile> [<templatefile>]]]
*
* keyfile:
* The key.json file to use for authentication
* outputfile:
* The file where to write the output to
* templatefile:
* The HTML template to use, supports the following placeholders:
* - "{{ title }}" - will be replaced with page title
* - "{{ description }}" - will be replaced with page description
* - "{{ content }}" - will be replaced with page content
*
* Setup:
* npm install pkgcloud
* For NodeJS < 0.10 also
* npm install readable-stream
*/
//~~ setup
// imports
var pkgcloud = require('pkgcloud'),
fs = require('fs'),
path = require('path'),
stream = require('stream'),
util = require('util');
// polyfills
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (str) {
return !this.indexOf(str);
}
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
//~~ argument parsing
// key file to use => ./key.json or first command line argument
var keyfile = path.join(__dirname, 'key.json');
if (process.argv.length >= 3) {
keyfile = process.argv[2];
}
// output file => ./index.html or second command line argument
var outputfile = path.join(__dirname, 'index.html');
if (process.argv.length >= 4) {
outputfile = process.argv[3];
}
// template file ==> ./template.html or third command line argument
var templatefile = path.join(__dirname, 'template.html');
if (process.argv.length >= 5) {
templatefile = process.argv[4];
}
//~~ helpers
var filterByExtension = function(fileObjs, extensions) {
return fileObjs.filter(function (obj) {
var name = obj.name;
return extensions.some(function (extension) { return name.endsWith(extension); })
});
}
var filterByName = function(fileObjs, name) {
return fileObjs.filter(function (obj) { return obj.name.startsWith(name) });
}
var filterNameByRegex = function(fileObjs, regex) {
return fileObjs.filter(function (obj) { return regex.test(obj.name) });
}
var stripLeading = function(name, toStrip) {
return name.substring(toStrip.length);
}
var sortByDate = function(a, b) {
if (a.timeCreated < b.timeCreated) return 1;
if (a.timeCreated > b.timeCreated) return -1;
return 0;
}
var formatDate = function(date) {
return date.replace(/T/, ' ').replace(/\..+/, '') + " UTC";
}
var formatSize = function(bytes) {
// Formats the given file size in bytes
if (!bytes) return "-";
var units = ["bytes", "KB", "MB"];
for (var i = 0; i < units.length; i++) {
if (bytes < 1024) {
return bytes.toFixed(1) + units[i];
}
bytes /= 1024;
}
return bytes.toFixed(1) + "GB";
}
var convertHash = function(hash) {
// Converts a hash from base64 to hex
return new Buffer(hash, 'base64').toString('hex');
}
var outputTable = function(fileObjs, s, nameProcessor, limit) {
// Outputs an HTML table to <s> for the provided <fileObjs>, limiting them to <limit>
// and preprocessing the filename with <nameProcessor>
limit = limit || 20;
s.write('<table class="table table-hover table-bordered">\n');
s.write('<tr><th class="name">Name</th><th class="date">Creation Date</th><th class="size">Size</th><th class="md5sum">MD5 Hash</th></tr>\n');
// sort by date and limit
fileObjs.sort(sortByDate).slice(0, limit).forEach(function(fileObj) {
console.log("Processing file object: %j", fileObj);
var url = "https://storage.googleapis.com/octoprint/" + fileObj.name;
var name = nameProcessor(fileObj.name);
s.write('<tr>');
s.write('<td class="name"><a href="' + url + '">' + name + "</a></td>");
s.write('<td class="date">' + formatDate(fileObj.timeCreated) + "</td>");
s.write("<td class='size'>" + formatSize(fileObj.size) + "</td>");
s.write("<td class='md5sum'><code>" + convertHash(fileObj.md5Hash) + "</code></td>");
s.write("</tr>\n");
});
s.write('</table>\n');
}
var outputPage = function(files, s) {
// Outputs the page for <files> to stream <s>, using the template.
var title = "OctoPi Downloads";
var description = "OctoPi Downloads";
var Writable = stream.Writable || require('readable-stream').Writable;
function StringStream(options) {
Writable.call(this, options);
this.buffer = "";
}
util.inherits(StringStream, Writable);
StringStream.prototype._write = function (chunk, enc, cb) {
this.buffer += chunk;
cb();
};
var output = new StringStream();
output.write("<ul><li><a href='#rpi'>Raspberry Pi</a><ul><li><a href='#rpi-stable'>Stable Builds</li><li><a href='#rpi-nightly'>Nightly Builds</a></li></ul></li><li><a href='#banana'>Banana Pi M1</a><ul><li><a href='#banana-nightly'>Nightly Builds</a></li></ul></li></ul>");
output.write("<h2 id='rpi'>Raspberry Pi</h2>\n");
output.write("<h3 id='rpi-stable'>Stable Builds</h3>\n")
outputTable(filterNameByRegex(files, /^stable\/.*octopi-(wheezy|jessie)-.*/),
output,
function(name) { return stripLeading(name, "stable/") },
3);
output.write("<h3 id='rpi-nightly'>Nightly Builds</h3>\n");
output.write("<small>Warning: These builds are untested and can be unstable and/or broken. If in doubt use a stable build.</small>");
outputTable(filterNameByRegex(files.filter(function (obj) { return !obj.name.startsWith("stable/") && !obj.name.startsWith("bananapi-m1/") }), /octopi-(wheezy|jessie)-/),
output,
function(name) { return name },
14);
output.write("<h2 id='banana'>Banana Pi M1</h2>\n")
output.write("<h3 id='banana-nightly'>Nightly Builds</h3>\n");
output.write("<small>Warning: These builds are untested and can be unstable and/or broken.</small>");
outputTable(filterNameByRegex(files, /^bananapi-m1\//),
output,
function(name) { return stripLeading(name, "bananapi-m1/") },
14);
var content = output.buffer;
fs.readFile(templatefile, "utf8", function (err, template) {
var result = template.replace(/{{ content }}/g, content)
.replace(/{{ title }}/g, title)
.replace(/{{ description }}/g, description);
s.write(result);
})
}
//~~ action and go
// construct client
var client = require('pkgcloud').storage.createClient({
provider: 'google',
keyFilename: keyfile, // path to a JSON key file
});
var container = "octoprint";
// fetch our files and render our page
client.getFiles(container, function (err, files) {
var stream = fs.createWriteStream(outputfile);
outputPage(filterByExtension(files, [".zip"]), stream);
});