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

4
5
6
import cpp from 'child-process-promise';
import cp from 'child_process';
import fs from 'fs';
7
import ignore from 'ignore';
8
9
10
import path from 'path';
import tar from 'tar';
import { getLogger } from 'common/log';
11
import { String } from 'typescript-string-operations';
12
import { GPU_INFO_COLLECTOR_FORMAT_WINDOWS } from './gpuData';
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
/**
 * 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();
    }
}

45
46
/**
 * Validate codeDir, calculate file count recursively under codeDir, and throw error if any rule is broken
47
 *
48
49
50
 * @param codeDir codeDir in nni config file
 * @returns file number under codeDir
 */
chicm-ms's avatar
chicm-ms committed
51
export async function validateCodeDir(codeDir: string): Promise<number> {
52
53
54
55
56
57
58
59
60
61
62
63
64
    let fileCount: number = 0;
    let fileTotalSize: number = 0;
    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).`);
        }
65
66
67
        // NOTE: We added this test in case any training service or shared storage (e.g. HDFS) does not support complex file name.
        // If there is no bug found for long time, feel free to remove it.
        const fileNameValid = relPath.split(path.sep).every(fpart => (fpart.match('^[a-z0-9A-Z._-]*$') !== null));
68
        if (!fileNameValid) {
69
70
71
72
73
74
            const message = [
                `File ${relPath} in directory ${codeDir} contains spaces or special characters in its name.`,
                'This might cause problem when uploading to cloud or remote machine.',
                'If you encounter any error, please report an issue: https://github.com/microsoft/nni/issues'
            ].join(' ');
            getLogger('validateCodeDir').warning(message);
75
        }
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
        } else {
liuzhe-lz's avatar
liuzhe-lz committed
113
            getLogger('execCopydir').debug(`Copying file from ${sourcePath} to ${destPath}`);
114
115
            await fs.promises.copyFile(sourcePath, destPath);
        }
116
    }
117

118
119
120
    return Promise.resolve();
}

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

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

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

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

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

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

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

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

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

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

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

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

237
export function getGpuMetricsCollectorBashScriptContent(scriptFolder: string): string {
238
239
    return `echo $$ > ${scriptFolder}/pid ; METRIC_OUTPUT_DIR=${scriptFolder} python3 -m nni.tools.gpu_tool.gpu_metrics_collector \
1>${scriptFolder}/stdout 2>${scriptFolder}/stderr`;
240
241
242
}

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
    }
}