util.ts 7.54 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
3
4
5

'use strict';

6
7
import * as cpp from 'child-process-promise';
import * as cp from 'child_process';
8
import * as fs from 'fs';
9
import ignore from 'ignore';
10
import * as path from 'path';
11
import * as tar from 'tar';
12
import { String } from 'typescript-string-operations';
13
import { validateFileName } from '../../common/utils';
14
import { GPU_INFO_COLLECTOR_FORMAT_WINDOWS } from './gpuData';
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
/**
 * List all files in directory except those ignored by .nniignore.
 * @param source
 * @param destination
 */
export function* listDirWithIgnoredFiles(root: string, relDir: string, ignoreFiles: string[]): Iterable<string> {
    let ignoreFile = undefined;
    const source = path.join(root, relDir);
    if (fs.existsSync(path.join(source, '.nniignore'))) {
        ignoreFile = path.join(source, '.nniignore');
        ignoreFiles.push(ignoreFile);
    }
    const ig = ignore();
    ignoreFiles.forEach((i) => ig.add(fs.readFileSync(i).toString()));
    for (const d of fs.readdirSync(source)) {
        const entry = path.join(relDir, d);
        if (ig.ignores(entry))
            continue;
        const entryStat = fs.statSync(path.join(root, entry));
        if (entryStat.isDirectory()) {
            yield entry;
            yield* listDirWithIgnoredFiles(root, entry, ignoreFiles);
        }
        else if (entryStat.isFile())
            yield entry;
    }
    if (ignoreFile !== undefined) {
        ignoreFiles.pop();
    }
}

47
48
/**
 * Validate codeDir, calculate file count recursively under codeDir, and throw error if any rule is broken
49
 *
50
51
52
 * @param codeDir codeDir in nni config file
 * @returns file number under codeDir
 */
chicm-ms's avatar
chicm-ms committed
53
export async function validateCodeDir(codeDir: string): Promise<number> {
54
55
    let fileCount: number = 0;
    let fileTotalSize: number = 0;
56
    let fileNameValid: boolean = true;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    for (const relPath of listDirWithIgnoredFiles(codeDir, '', [])) {
        const d = path.join(codeDir, relPath);
        fileCount += 1;
        fileTotalSize += fs.statSync(d).size;
        if (fileCount > 2000) {
            throw new Error(`Too many files and directories (${fileCount} already scanned) in ${codeDir},`
                + ` please check if it's a valid code dir`);
        }
        if (fileTotalSize > 300 * 1024 * 1024) {
            throw new Error(`File total size too large in code dir (${fileTotalSize} bytes already scanned, exceeds 300MB).`);
        }
        fileNameValid = true;
        relPath.split(path.sep).forEach(fpart => {
            if (fpart !== '' && !validateFileName(fpart))
                fileNameValid = false;
        });
        if (!fileNameValid) {
            throw new Error(`Validate file name error: '${d}' is an invalid file name.`);
        }
76
    }
77
78

    return fileCount;
79
80
81
82
}

/**
 * crete a new directory
83
 * @param directory
84
 */
85
export async function execMkdir(directory: string, share: boolean = false): Promise<void> {
86
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
87
        await cpp.exec(`powershell.exe New-Item -Path "${directory}" -ItemType "directory" -Force`);
88
    } else if (share) {
SparkSnail's avatar
SparkSnail committed
89
        await cpp.exec(`(umask 0; mkdir -p '${directory}')`);
90
    } else {
SparkSnail's avatar
SparkSnail committed
91
        await cpp.exec(`mkdir -p '${directory}'`);
92
    }
93

94
95
96
    return Promise.resolve();
}

97
98
99
100
101
102
/**
 * copy files to the directory
 * @param source
 * @param destination
 */
export async function execCopydir(source: string, destination: string): Promise<void> {
103
104
105
106
107
108
    if (!fs.existsSync(destination))
        await fs.promises.mkdir(destination);
    for (const relPath of listDirWithIgnoredFiles(source, '', [])) {
        const sourcePath = path.join(source, relPath);
        const destPath = path.join(destination, relPath);
        if (fs.statSync(sourcePath).isDirectory()) {
SparkSnail's avatar
SparkSnail committed
109
110
111
            if (!fs.existsSync(destPath)) {
                await fs.promises.mkdir(destPath);
            }
112
113
114
        } else {
            await fs.promises.copyFile(sourcePath, destPath);
        }
115
    }
116

117
118
119
    return Promise.resolve();
}

120
121
/**
 * crete a new file
122
 * @param filename
123
124
125
 */
export async function execNewFile(filename: string): Promise<void> {
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
126
        await cpp.exec(`powershell.exe New-Item -Path "${filename}" -ItemType "file" -Force`);
127
    } else {
SparkSnail's avatar
SparkSnail committed
128
        await cpp.exec(`touch '${filename}'`);
129
    }
130

131
132
133
134
    return Promise.resolve();
}

/**
135
 * run script using powershell or bash
136
137
 * @param filePath
 */
138
export function runScript(filePath: string): cp.ChildProcess {
139
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
140
        return cp.exec(`powershell.exe -ExecutionPolicy Bypass -file "${filePath}"`);
141
    } else {
SparkSnail's avatar
SparkSnail committed
142
        return cp.exec(`bash '${filePath}'`);
143
144
145
146
147
    }
}

/**
 * output the last line of a file
148
 * @param filePath
149
150
151
152
 */
export async function execTail(filePath: string): Promise<cpp.childProcessPromise.Result> {
    let cmdresult: cpp.childProcessPromise.Result;
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
153
        cmdresult = await cpp.exec(`powershell.exe Get-Content "${filePath}" -Tail 1`);
154
    } else {
SparkSnail's avatar
SparkSnail committed
155
        cmdresult = await cpp.exec(`tail -n 1 '${filePath}'`);
156
    }
157

158
159
160
161
162
    return Promise.resolve(cmdresult);
}

/**
 * delete a directory
163
 * @param directory
164
 */
165
export async function execRemove(directory: string): Promise<void> {
166
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
167
        await cpp.exec(`powershell.exe Remove-Item "${directory}" -Recurse -Force`);
168
    } else {
SparkSnail's avatar
SparkSnail committed
169
        await cpp.exec(`rm -rf '${directory}'`);
170
    }
171

172
173
174
175
176
    return Promise.resolve();
}

/**
 * kill a process
177
 * @param directory
178
 */
179
export async function execKill(pid: string): Promise<void> {
180
    if (process.platform === 'win32') {
Yuge Zhang's avatar
Yuge Zhang committed
181
        await cpp.exec(`cmd.exe /c taskkill /PID ${pid} /T /F`);
182
183
184
    } else {
        await cpp.exec(`pkill -P ${pid}`);
    }
185

186
187
188
189
    return Promise.resolve();
}

/**
190
 * get command of setting environment variable
191
 * @param  variable
192
 * @returns command string
193
 */
194
export function setEnvironmentVariable(variable: { key: string; value: string }): string {
195
196
    if (process.platform === 'win32') {
        return `$env:${variable.key}="${variable.value}"`;
197
    } else {
SparkSnail's avatar
SparkSnail committed
198
        return `export ${variable.key}='${variable.value}'`;
199
200
201
    }
}

202
203
/**
 * Compress files in directory to tar file
204
205
 * @param  sourcePath
 * @param  tarPath
206
 */
207
export async function tarAdd(tarPath: string, sourcePath: string): Promise<void> {
208
209
210
    const fileList = [];
    for (const d of listDirWithIgnoredFiles(sourcePath, '', [])) {
        fileList.push(d);
211
    }
212
213
214
215
216
217
218
219
220
    tar.create(
        {
            gzip: true,
            file: tarPath,
            sync: true,
            cwd: sourcePath,
        },
        fileList
    );
221
222
    return Promise.resolve();
}
223
224
225

/**
 * generate script file name
226
 * @param fileNamePrefix
227
228
229
 */
export function getScriptName(fileNamePrefix: string): string {
    if (process.platform === 'win32') {
230
        return String.Format('{0}.ps1', fileNamePrefix);
231
    } else {
232
        return String.Format('{0}.sh', fileNamePrefix);
233
234
235
    }
}

236
237
238
239
240
export function getGpuMetricsCollectorBashScriptContent(scriptFolder: string): string {
    return `echo $$ > ${scriptFolder}/pid ; METRIC_OUTPUT_DIR=${scriptFolder} python3 -m nni_gpu_tool.gpu_metrics_collector`;
}

export function runGpuMetricsCollector(scriptFolder: string): void {
241
    if (process.platform === 'win32') {
242
243
244
        const scriptPath = path.join(scriptFolder, 'gpu_metrics_collector.ps1');
        const content = String.Format(GPU_INFO_COLLECTOR_FORMAT_WINDOWS, scriptFolder, path.join(scriptFolder, 'pid'));
        fs.writeFile(scriptPath, content, { encoding: 'utf8' }, () => { runScript(scriptPath); });
245
    } else {
246
        cp.exec(getGpuMetricsCollectorBashScriptContent(scriptFolder), { shell: '/bin/bash' });
247
248
    }
}