forked from finos/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_utils.js
340 lines (314 loc) · 11.5 KB
/
script_utils.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
require("dotenv").config({path: "./.perspectiverc"});
const execSync = require("child_process").execSync;
const _path = require("path");
const fs = require("fs");
const rimraf = require("rimraf");
const isWin = process.platform === "win32";
/*******************************************************************************
*
* Private
*/
function rimraf_err(e) {
if (e) {
console.error(e.message);
process.exit(1);
}
}
function depath(strings, ...args) {
if (Array.isArray(strings)) {
strings = strings.map((x, i) => x + (args[i] || "")).join("");
}
strings = strings.split("/");
if (strings[0] === "") {
strings = strings.slice(1);
strings[0] = "/" + strings[0];
}
return strings;
}
function cut_last(f) {
let x = f.split(" ");
return x.slice(0, x.length - 1).join(" ");
}
function cut_first(f) {
return f
.split(" ")
.slice(1)
.join(" ");
}
const execute = cmd => {
try {
if (process.argv.indexOf("--debug") > -1) {
console.log(`$ ${cmd}`);
}
execSync(cmd, {stdio: "inherit"});
} catch (e) {
console.error("\n" + e.message);
process.exit(1);
}
};
const execute_throw = cmd => {
if (process.argv.indexOf("--debug") > -1) {
console.log(`$ ${cmd}`);
}
execSync(cmd, {stdio: "inherit"});
};
/*******************************************************************************
*
* Public
*/
/**
* Calls `path.join` on the result of splitting the input string by the default
* path delimiter `/`, which allows writing simpler path statements that will
* still be cross platform. Can be used as an template literal.
*
* @param {string} path a `/` encoded path.
* @returns {string} A system-correct path
* @example
* console.assert(path`a/b/c` === `a\\b\\c`) // on Windows
*/
exports.path = function path(strings, ...args) {
return _path.join(...depath(strings, ...args));
};
/**
* Like `path`, but uses `path.resolve` to get the absolute path, carefully
* preserving leading delimiter. Can be used as an template literal.
*
* @param {string} path a relative `/` encoded path.
* @returns {string} A system-correct absolute path
* @example
* console.assert(path`a/b/c` === `${process.cwd()}\\a\\b\\v`) // on Windows
*/
const resolve = (exports.resolve = function resolve(strings, ...args) {
return _path.resolve(...depath(strings, ...args)).replace(/\\/g, '\\');
});
/**
* Calls `join` on each of the input path arguments, then `rimraf`s the path if
* it exists. Can be used as an template literal, and can also take multiple
* arguments call itself in sequence.
*
* @param {string} [path] a `/` encode path.
* @example
* clean`a/b/c`; // Cleans this dir
* clean(path`a/b/c`, path`d/e/f`); // Cleans both dirs
*/
exports.clean = function clean(...dirs) {
if (Array.isArray(dirs[0])) {
const dir = exports.path(...dirs);
if (fs.existsSync(dir)) {
rimraf(dir, rimraf_err);
}
} else {
for (let dir of dirs) {
dir = exports.path([dir]);
if (fs.existsSync(dir)) {
rimraf(dir, rimraf_err);
}
}
}
};
/**
* For working with shell commands, `bash` knows how to remove consecutive
* text from strings when arguments are "falsey", which makes mapping flags to
* JS expressions a breeze. Can be used as a template literal.
*
* @param {string} expression a bash command to be templated.
* @returns {string} A command with the missing argument's flags removed.
* @example
* console.assert(
* bash`run -t${1} -u"${undefined}" task`,
* `run -t1 task`
* );
*/
const bash = (exports.bash = function bash(strings, ...args) {
let terms = [];
if (strings.length === 1) {
return strings[0];
}
for (let i = 0; i < strings.length - 1; i++) {
const arg = args[i];
const start = terms.length === 0 ? strings[i] : terms.pop();
if (arg === undefined || arg !== arg || arg === false) {
terms = [...terms, cut_last(start), " ", cut_first(strings[i + 1])];
} else if (Array.isArray(arg)) {
terms = [...terms, start, arg.join(" "), strings[i + 1]];
} else {
terms = [...terms, start, arg, strings[i + 1]];
}
}
return terms
.join("")
.replace(/[ \t\n]+/g, " ")
.trim();
});
/**
* Just like `bash, but executes the command immediately. Will log if the
* `--debug` flag is used to build. If an error is encountered, it is logged
* and the child process will exit with error code 1.
*
* @param {string} expression a bash command to be templated.
* @returns {string} A command with the missing argument's flags removed.
* @example
* execute`run -t${1} -u"${undefined}" task`;
*/
exports.execute = (strings, ...args) => execute(Array.isArray(strings) ? bash(strings, ...args) : strings);
/**
* Just like `execute`, except it throws anddoes not exit the child process
* if the command throws an error.
*
* @param {string} expression a bash command to be templated.
* @returns {string} A command with the missing argument's flags removed.
* @example
* execute`run -t${1} -u"${undefined}" task`;
*/
exports.execute_throw = (strings, ...args) => execute_throw(Array.isArray(strings) ? bash(strings, ...args) : strings);
/**
* Returns the value after this command-line flag, or `true` if it is the last
* arg. This makes it easy to null-pun for boolean flags, and capture the
* argument for argument-providing flags, and respect quotes and parens, in
* one function. Can be used as a template literal - not sure why, 2 less
* characters?
*
* @param {string} flag The command line flag name. Returns all arguments if
* this param is `undefined`.
* @returns {string} The next argument after this flag in the command args, or
* `true.
* @example
* console.assert(getarg`--debug`);
*/
const getarg = (exports.getarg = function(flag, ...args) {
if (Array.isArray(flag)) {
flag = flag.map((x, i) => x + (args[i] || "")).join("");
}
const argv = process.argv.slice(2);
if (flag) {
const index = argv.indexOf(flag);
if (index > -1) {
const next = argv[index + 1];
if (next) {
return next;
} else {
return true;
}
}
} else {
return argv
.map(function(arg) {
return "'" + arg.replace(/'/g, "'\\''") + "'";
})
.join(" ");
}
});
/**
* A `bash` expression for running commands in Docker images
*
* @param {string} image The Docker image name.
* @returns {string} A command for invoking this docker image.
* @example
* execute`${docker()} echo "Hello from Docker"`;
*/
exports.docker = function docker(image = "puppeteer") {
console.log(`-- Creating perspective/${image} docker image`);
const IS_WRITE = getarg("--write") || process.env.WRITE_TESTS;
const CPUS = parseInt(process.env.PSP_CPU_COUNT);
const PACKAGE = process.env.PACKAGE;
const CWD = process.cwd();
const IS_CI = getarg("--ci");
const IS_MANYLINUX = image.indexOf("manylinux") > -1 ? true : false;
const IMAGE = `perspective/${image}`;
let env_vars = bash`-eWRITE_TESTS=${IS_WRITE} \
-ePACKAGE="${PACKAGE}"`;
let flags = IS_CI ? bash`--rm` : bash`--rm -it`;
if (IS_MANYLINUX) {
console.log(`-- Using manylinux build`);
env_vars += bash` -ePSP_MANYLINUX=1 `;
}
let ret = bash`docker run \
${flags} \
${env_vars} \
-v${CWD}:/usr/src/app/perspective \
-w /usr/src/app/perspective --shm-size=2g -u root \
--cpus="${CPUS}.0" ${IMAGE}`;
return ret;
};
/**
* Get the docker image to use for the given image/python combination
*
* @param {string} image The Docker image name.
* @param {string} python The python version requested
* @returns {string} The docker image to use
*/
exports.python_image = function python_image(image = "", python = "") {
console.log(`-- Getting image for image: '${image}' and python: '${python}'`);
if (python == "python2") {
if (image == "manylinux2014") {
throw "Python2 not supported for manylinux2014";
} else {
return "manylinux2010";
}
}
return `${image}`;
};
/*******************************************************************************
*
* Tests
*/
function run_suite(tests) {
for (const [actual, expected] of tests) {
console.assert(actual === expected, `"${actual}" received, expected: "${expected}"`);
}
}
if (isWin){
run_suite([
[resolve`a/b/c`, `${process.cwd()}\\a\\b\\c`],
[resolve`${__dirname}/../cpp/perspective`, `${process.cwd()}\\cpp\\perspective`],
[resolve`${__dirname}/../python/perspective/dist`, _path.resolve(__dirname, "..", "python", "perspective", "dist")],
[resolve`${__dirname}/../cpp/perspective`, _path.resolve(__dirname, "..", "cpp", "perspective")],
[resolve`${__dirname}/../cmake`, _path.resolve(__dirname, "..", "cmake")],
[resolve`${resolve`${__dirname}/../python/perspective/dist`}/cmake`, _path.resolve(_path.resolve(__dirname, "..", "python", "perspective", "dist"), "cmake")],
[resolve`${resolve`${__dirname}/../python/perspective/dist`}/obj`, _path.resolve(_path.resolve(__dirname, "..", "python", "perspective", "dist"), "obj")]
]);
} else {
run_suite([
[resolve`a/b/c`, `${process.cwd()}/a/b/c`],
[resolve`${__dirname}/../cpp/perspective`, `${process.cwd()}/cpp/perspective`],
[resolve`${__dirname}/../python/perspective/dist`, _path.resolve(__dirname, "..", "python", "perspective", "dist")],
[resolve`${__dirname}/../cpp/perspective`, _path.resolve(__dirname, "..", "cpp", "perspective")],
[resolve`${__dirname}/../cmake`, _path.resolve(__dirname, "..", "cmake")],
[resolve`${resolve`${__dirname}/../python/perspective/dist`}/cmake`, _path.resolve(_path.resolve(__dirname, "..", "python", "perspective", "dist"), "cmake")],
[resolve`${resolve`${__dirname}/../python/perspective/dist`}/obj`, _path.resolve(_path.resolve(__dirname, "..", "python", "perspective", "dist"), "obj")]
]);
}
run_suite([
[bash`run -t${1}`, `run -t1`],
[bash`run -t${undefined}`, `run`],
[bash`run -t${true}`, `run -ttrue`],
[bash`run -t${false}`, `run`],
[bash`run -t${1} task`, `run -t1 task`],
[bash`run -t${undefined} task`, `run task`],
[bash`run -t="${1}"`, `run -t="1"`],
[bash`run -t="${undefined}"`, `run`],
[bash`run -t="${1}" task`, `run -t="1" task`],
[bash`run -t="${undefined}" task`, `run task`],
[bash`run -t${1} -u${2} task`, `run -t1 -u2 task`],
[bash`run -t${1} -u${undefined} task`, `run -t1 task`],
[bash`run -t${undefined} -u${2} task`, `run -u2 task`],
[bash`run -t${undefined} -u${undefined} task`, `run task`],
[bash`run -t"${undefined}" -u"${undefined}" task`, `run task`],
[bash`run "${undefined}" task`, `run task`],
[bash`run ${undefined} task`, `run task`],
[bash`TEST=${undefined} run`, `run`],
[bash`TEST=${1} run`, `TEST=1 run`],
[bash`TEST=${1}`, `TEST=1`],
[bash`TEST=${undefined}`, ``],
[bash`this is a test`, `this is a test`],
[bash`this is a test `, `this is a test `],
[bash`--test="${undefined}.0" ${1}`, `1`]
]);