This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
crater-util.js
188 lines (168 loc) · 4.9 KB
/
crater-util.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
'use strict';
var Promise = require('promise');
var _fs = require('graceful-fs'); // Need to parse many files at once
var fs = {
readFile: Promise.denodeify(_fs.readFile),
readFileSync: _fs.readFileSync
};
var exec = require('child_process').exec;
var http = require('http');
var https = require('https');
var assert = require('assert');
var async = require('async');
var url = require('url');
var defaultRustDistAddr = "http://static-rust-lang-org.s3-us-west-1.amazonaws.com/dist";
var defaultCrateIndexAddr = "https://github.com/rust-lang/crates.io-index";
var defaultCacheDir = "./cache";
var defaultDlRootAddr = "https://crates.io/api/v1/crates";
var defaultDbCredentialsFile = "./pg-credentials.json";
var defaultPulseCredentialsFile = "./pulse-credentials.json";
var defaultTcCredentialsFile = "./tc-credentials.json";
var defaultDbName = "crater";
/**
* Parses a string toolchain identifier into an object { channel: string, date: string }
*/
function parseToolchain(toolchainName) {
if (toolchainName == null) { return null; }
var ret_channel;
var ret_date;
["nightly", "beta", "stable"].forEach(function(channel) {
var prefix = channel + "-";
var ix = toolchainName.indexOf(prefix);
if (ix != -1) {
ret_channel = channel;
ret_date = toolchainName.slice(prefix.length);
}
});
if (ret_channel) {
return {
channel: ret_channel,
archiveDate: ret_date,
};
} else {
// It must be a 40-character sha
if (toolchainName.length == 40) {
return {
customSha: toolchainName
}
}
return null;
}
}
function toolchainToString(toolchain) {
assert((toolchain.channel && toolchain.archiveDate) || toolchain.customSha);
if (!toolchain.customSha) {
return toolchain.channel + "-" + toolchain.archiveDate;
} else {
return toolchain.customSha;
}
}
function downloadToMem(addr) {
var addr2 = url.parse(addr);
var opts = {
host: addr2.host,
hostname: addr2.host,
port: addr2.port,
path: addr2.pathname,
pathname: addr2.pathname,
method: 'GET',
headers: {
"User-Agent": "crater - the Rust crate tester"
}
};
if (addr.lastIndexOf("https", 0) === 0) {
return new Promise(function(resolve, reject) {
https.get(opts, function(res) {
var data = '';
res.on('error', function(e) { reject(e); });
res.on('data', function(d) { data += d; });
res.on('end', function() {
resolve(data);
});
});
});
} else if (addr.lastIndexOf("http", 0) === 0) {
return new Promise(function(resolve, reject) {
http.get(opts, function(res) {
var data = '';
res.on('error', function(e) { reject(e); });
res.on('data', function(d) { data += d; });
res.on('end', function() {
resolve(data);
});
});
});
} else {
return Promise.denodeify(fs.readFile)(addr, 'utf-8');
}
}
function runCmd(command, options) {
return new Promise(function(resolve, reject) {
exec(command, options, function(err, sout, serr) {
if (err) {
reject(err);
}
resolve({stdout: sout, stderr: serr});
});
});
}
function rustDate(date) {
var year = date.getUTCFullYear().toString();
var month = (date.getMonth() + 1).toString();
if (month.length == 1) {
month = "0" + month;
}
var day = date.getDate().toString();
if (day.length == 1) {
day = "0" + day;
}
return year + "-" + month + "-" + day;
}
function loadDefaultConfig() {
return {
dbName: defaultDbName,
rustDistAddr: defaultRustDistAddr,
crateIndexAddr: defaultCrateIndexAddr,
dlRootAddr: defaultDlRootAddr,
cacheDir: defaultCacheDir,
dbCredentials: loadCredentials(defaultDbCredentialsFile),
pulseCredentials: loadCredentials(defaultPulseCredentialsFile),
tcCredentials: loadCredentials(defaultTcCredentialsFile)
};
}
function loadCredentials(credentialsFile) {
return JSON.parse(fs.readFileSync(credentialsFile, "utf8"));
}
function workDispatcher(task, cb) {
task(cb);
}
// A queue used to serialize access to the on-disk git repo and caches,
// to avoid corruption.
var actionQueue = async.queue(workDispatcher, 1);
/**
* Takes a function that returns a promise and ensures that no other serial promises execute
* until is resolved. Returns a promise of that resolved value.
*/
function serial(f) {
return new Promise(function(resolve, reject) {
actionQueue.push(function(dispatcherCb) {
f().then(function(r) {
dispatcherCb();
resolve(r);
}).catch(function(e) {
dispatcherCb();
reject(e);
});
});
});
}
exports.parseToolchain = parseToolchain;
exports.toolchainToString = toolchainToString;
exports.downloadToMem = downloadToMem;
exports.runCmd = runCmd;
exports.rustDate = rustDate;
exports.loadDefaultConfig = loadDefaultConfig;
exports.defaultDbCredentialsFile = defaultDbCredentialsFile;
exports.defaultPulseCredentialsFile = defaultPulseCredentialsFile;
exports.defaultTcCredentialsFile = defaultTcCredentialsFile;
exports.serial = serial;