"vscode:/vscode.git/clone" did not exist on "954cb14d874691f5e4360c3c8f9a397b921f52af"
util.ts 7.48 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
109
110
111
112
    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()) {
            await fs.promises.mkdir(destPath);
        } else {
            await fs.promises.copyFile(sourcePath, destPath);
        }
113
    }
114

115
116
117
    return Promise.resolve();
}

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

129
130
131
132
    return Promise.resolve();
}

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

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

156
157
158
159
160
    return Promise.resolve(cmdresult);
}

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

170
171
172
173
174
    return Promise.resolve();
}

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

184
185
186
187
    return Promise.resolve();
}

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

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

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

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