util.ts 7.66 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 { getLogger } from '../../common/log';
13
import { String } from 'typescript-string-operations';
14
import { validateFileName } from '../../common/utils';
15
import { GPU_INFO_COLLECTOR_FORMAT_WINDOWS } from './gpuData';
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
/**
 * 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();
    }
}

48
49
/**
 * Validate codeDir, calculate file count recursively under codeDir, and throw error if any rule is broken
50
 *
51
52
53
 * @param codeDir codeDir in nni config file
 * @returns file number under codeDir
 */
chicm-ms's avatar
chicm-ms committed
54
export async function validateCodeDir(codeDir: string): Promise<number> {
55
56
    let fileCount: number = 0;
    let fileTotalSize: number = 0;
57
    let fileNameValid: boolean = true;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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.`);
        }
77
    }
78
79

    return fileCount;
80
81
82
83
}

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

95
96
97
    return Promise.resolve();
}

98
99
100
101
102
103
/**
 * copy files to the directory
 * @param source
 * @param destination
 */
export async function execCopydir(source: string, destination: string): Promise<void> {
104
105
106
107
108
109
    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
110
111
112
            if (!fs.existsSync(destPath)) {
                await fs.promises.mkdir(destPath);
            }
113
        } else {
114
            getLogger().debug(`Copying file from ${sourcePath} to ${destPath}`);
115
116
            await fs.promises.copyFile(sourcePath, destPath);
        }
117
    }
118

119
120
121
    return Promise.resolve();
}

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

133
134
135
136
    return Promise.resolve();
}

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

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

160
161
162
163
164
    return Promise.resolve(cmdresult);
}

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

174
175
176
177
178
    return Promise.resolve();
}

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

188
189
190
191
    return Promise.resolve();
}

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

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

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

238
239
240
241
242
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 {
243
    if (process.platform === 'win32') {
244
245
246
        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); });
247
    } else {
248
        cp.exec(getGpuMetricsCollectorBashScriptContent(scriptFolder), { shell: '/bin/bash' });
249
250
    }
}