-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathindex.ts
238 lines (210 loc) · 6.82 KB
/
index.ts
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
import {
exec,
ExecOptions,
ExecException
} from 'child_process';
import * as fs from 'fs-extra';
import * as CliProgress from 'cli-progress';
import * as path from 'path';
import * as bent from 'bent';
import * as os from 'os';
import * as url from 'url';
import { promisify } from 'util';
import { customAlphabet } from 'nanoid';
import * as constants from '../constants';
import * as extract from 'extract-zip';
import * as realOra from 'ora';
import * as prettyBytes from 'pretty-bytes';
import * as dateformat from 'dateformat';
export * as Script from './script';
export * as Plugin from './plugin';
export * as Cache from './cache';
export * as Framework from './framework';
export * as PredictDataset from './predict-dataset';
export * as PostPredict from './post-predict';
export * as ServePredict from './serve-predict';
const { pipeline } = require('stream');
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 8);
export const pipelineAsync = promisify(pipeline);
export const makeWorkspace = async (): Promise<string> => {
const workspace = path.resolve('./pipcook-output');
await fs.mkdirp(workspace);
return workspace;
};
/**
* download the file and stored in specified directory
* @param url: url of the file
* @param fileName: full path of file that will be stored
*/
export async function download(url: string, fileName: string): Promise<void> {
await fs.ensureFile(fileName);
const stream = await bent(url)('') as NodeJS.ReadableStream;
return pipelineAsync(stream, fs.createWriteStream(fileName));
}
/**
* unzip compressed data
* @param filePath: path of zip
* @param targetPath: target full path
*/
export function unZipData(filePath: string, targetPath: string): Promise<void> {
return extract(filePath, { dir: targetPath });
}
/**
* Fit model directory, if `model/model/pipeline.json` exists, move the subdirectory `model` to top level.
* @param filePath: path of zip
*/
export async function fitModelDir(modelDir: string): Promise<void> {
if (await fs.pathExists(path.join(modelDir, constants.WorkspaceModelDir, constants.PipelineFileInModelDir))) {
await fs.move(path.join(modelDir, constants.WorkspaceModelDir), `${modelDir}.tmp`);
await fs.remove(modelDir);
await fs.move(`${modelDir}.tmp`, modelDir);
}
}
/**
* generate id
*/
export function generateId(): string {
return nanoid();
}
export enum DownloadProtocol { HTTP = 'http:', HTTPS = 'https:', FILE = 'file:' }
export function execAsync(cmd: string, opts?: ExecOptions): Promise<string> {
return new Promise((resolve, reject): void => {
exec(cmd, opts, (err: ExecException | null, stdout: string) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
});
});
}
/**
* download the file and stored in specified directory
* @param url: url of the file
* @param fileName: full path of file that will be stored
*/
export async function downloadWithProgress(url: string, fileName: string): Promise<void> {
await fs.ensureFile(fileName);
const bar = new CliProgress.SingleBar({
format: '{bar} {percentage}% {value}/{total}',
formatValue: (v, _, type): string => {
if (type === 'value' || type === 'total') {
return prettyBytes(v);
} else {
return v.toString();
}
}
}, CliProgress.Presets.shades_classic);
const file = fs.createWriteStream(fileName);
let receivedBytes = 0;
const downloadStream = (await bent(url)('')) as bent.NodeResponse;
const totalBytes = downloadStream.headers['content-length'];
bar.start(Number(totalBytes), 0);
downloadStream.on('data', (chunk: any) => {
receivedBytes += chunk.length;
bar.update(receivedBytes);
});
try {
await pipelineAsync(downloadStream, file);
bar.stop();
} catch (err) {
fs.unlink(fileName);
bar.stop();
throw err;
}
}
/**
* Download the dataset from specific URL and extract to a generated path as the returned value.
* @param resUrl the resource url, support http://, https://, file://.
* @param targetDir the directory to save the files
*/
export async function downloadAndExtractTo(resUrl: string, targetDir: string): Promise<void> {
const { protocol, pathname } = url.parse(resUrl);
if (!protocol || !pathname) {
throw new TypeError('invalid url');
}
const filename = path.basename(pathname);
const extname = path.extname(filename);
if (protocol === 'file:') {
if (extname === '.zip') {
await this.unZipData(pathname, targetDir);
} else {
await fs.copy(pathname, targetDir);
}
} else if (protocol === 'http:' || protocol === 'https:') {
if (extname === '.zip') {
const tmpPath = path.join(constants.PIPCOOK_TMPDIR, this.generateId());
await this.downloadWithProgress(resUrl, tmpPath);
this.logger.start('extracting');
await this.unZipData(tmpPath, targetDir);
await fs.remove(tmpPath);
} else {
await this.downloadWithProgress(resUrl, targetDir);
}
} else {
throw new TypeError(`[${extname}] file format is not supported.`);
}
}
export const mirrorUrl = (mirror: string, framework: string): string => {
const pyVersion = 'py37';
const nodeVersion = `node${process.versions.node.substr(0, process.versions.node.indexOf('.'))}`;
return url.resolve(
mirror,
`${nodeVersion}-${pyVersion}/${encodeURIComponent(framework)}-${os.platform()}-${os.arch()}-v${process.versions.napi}.zip`
);
};
export interface Logger {
success(message: string): void;
fail(message: string, exit?: boolean, code?: number): void;
info(message: string): void;
warn(message: string): void;
}
export class TtyLogger implements Logger {
private spinner: realOra.Ora;
constructor() {
this.spinner = realOra({
stream: process.stdout,
prefixText: (): string => dateformat(new Date(), '[hh:MM:ss:l]')
});
}
success(message: string): void {
this.spinner.succeed(message);
}
fail(message: string, exit = true, code = 1): void {
this.spinner.fail(message);
if (exit) {
process.exit(code);
}
}
info(message: string): void {
this.spinner.info(message);
}
warn(message: string): void {
this.spinner.warn(message);
}
start(message: string): void {
this.spinner.start(message);
}
}
export class DefaultLogger implements Logger {
success(message: string): void {
console.log('[success]: ' + message);
}
fail(message: string, exit = true, code = 1): void {
console.error('[fail]: ' + message);
if (exit) {
process.exit(code);
}
}
info(message: string): void {
console.log('[info]: ' + message);
}
warn(message: string): void {
console.warn('[warn]: ' + message);
}
start(message: string): void {
console.log('[start]: ' + message);
}
}
const { rows, columns, isTTY } = process.stdout;
export const logger: Logger = isTTY && rows > 0 && columns > 0 ? new TtyLogger() : new DefaultLogger();