util.ts 7.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Copyright (c) Microsoft Corporation
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

22
23
import * as cpp from 'child-process-promise';
import * as cp from 'child_process';
24
import * as fs from 'fs';
25
import * as os from 'os';
26
27
import * as path from 'path';
import { String } from 'typescript-string-operations';
28
import { countFilesRecursively, getNewLine, validateFileNameRecursively } from '../../common/utils';
29
import { file } from '../../node_modules/@types/tmp';
30
import { GPU_INFO_COLLECTOR_FORMAT_WINDOWS } from './gpuData';
31
32
33

/**
 * Validate codeDir, calculate file count recursively under codeDir, and throw error if any rule is broken
34
 *
35
36
37
 * @param codeDir codeDir in nni config file
 * @returns file number under codeDir
 */
38
// tslint:disable: no-redundant-jsdoc
39
40
export async function validateCodeDir(codeDir: string) : Promise<number> {
    let fileCount: number | undefined;
41
    let fileNameValid: boolean = true;
42
43
    try {
        fileCount = await countFilesRecursively(codeDir);
44
    } catch (error) {
45
46
        throw new Error(`Call count file error: ${error}`);
    }
47
48
    try {
        fileNameValid = await validateFileNameRecursively(codeDir);
49
    } catch (error) {
50
51
        throw new Error(`Validate file name error: ${error}`);
    }
52

53
    if (fileCount !== undefined && fileCount > 1000) {
54
        const errMessage: string = `Too many files(${fileCount} found}) in ${codeDir},`
55
                                    + ` please check if it's a valid code dir`;
56
        throw new Error(errMessage);
57
    }
58
59
60
61

    if (!fileNameValid) {
        const errMessage: string = `File name in ${codeDir} is not valid, please check file names, only support digit number、alphabet and (.-_) in file name.`;
        throw new Error(errMessage);
62
    }
63
64

    return fileCount;
65
66
67
68
}

/**
 * crete a new directory
69
 * @param directory
70
 */
71
export async function execMkdir(directory: string, share: boolean = false): Promise<void> {
72
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
73
        await cpp.exec(`powershell.exe New-Item -Path "${directory}" -ItemType "directory" -Force`);
74
    } else if (share) {
SparkSnail's avatar
SparkSnail committed
75
        await cpp.exec(`(umask 0; mkdir -p '${directory}')`);
76
    } else {
SparkSnail's avatar
SparkSnail committed
77
        await cpp.exec(`mkdir -p '${directory}'`);
78
    }
79

80
81
82
    return Promise.resolve();
}

83
84
85
86
87
88
89
/**
 * copy files to the directory
 * @param source
 * @param destination
 */
export async function execCopydir(source: string, destination: string): Promise<void> {
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
90
        await cpp.exec(`powershell.exe Copy-Item "${source}" -Destination "${destination}" -Recurse`);
91
    } else {
SparkSnail's avatar
SparkSnail committed
92
        await cpp.exec(`cp -r '${source}/.' '${destination}'`);
93
    }
94

95
96
97
    return Promise.resolve();
}

98
99
/**
 * crete a new file
100
 * @param filename
101
102
103
 */
export async function execNewFile(filename: string): Promise<void> {
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
104
        await cpp.exec(`powershell.exe New-Item -Path "${filename}" -ItemType "file" -Force`);
105
    } else {
SparkSnail's avatar
SparkSnail committed
106
        await cpp.exec(`touch '${filename}'`);
107
    }
108

109
110
111
112
    return Promise.resolve();
}

/**
113
 * run script using powershell or bash
114
115
 * @param filePath
 */
116
export function runScript(filePath: string): cp.ChildProcess {
117
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
118
        return cp.exec(`powershell.exe -ExecutionPolicy Bypass -file "${filePath}"`);
119
    } else {
SparkSnail's avatar
SparkSnail committed
120
        return cp.exec(`bash '${filePath}'`);
121
122
123
124
125
    }
}

/**
 * output the last line of a file
126
 * @param filePath
127
128
129
130
 */
export async function execTail(filePath: string): Promise<cpp.childProcessPromise.Result> {
    let cmdresult: cpp.childProcessPromise.Result;
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
131
        cmdresult = await cpp.exec(`powershell.exe Get-Content "${filePath}" -Tail 1`);
132
    } else {
SparkSnail's avatar
SparkSnail committed
133
        cmdresult = await cpp.exec(`tail -n 1 '${filePath}'`);
134
    }
135

136
137
138
139
140
    return Promise.resolve(cmdresult);
}

/**
 * delete a directory
141
 * @param directory
142
 */
143
export async function execRemove(directory: string): Promise<void> {
144
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
145
        await cpp.exec(`powershell.exe Remove-Item "${directory}" -Recurse -Force`);
146
    } else {
SparkSnail's avatar
SparkSnail committed
147
        await cpp.exec(`rm -rf '${directory}'`);
148
    }
149

150
151
152
153
154
    return Promise.resolve();
}

/**
 * kill a process
155
 * @param directory
156
 */
157
export async function execKill(pid: string): Promise<void> {
158
    if (process.platform === 'win32') {
Yuge Zhang's avatar
Yuge Zhang committed
159
        await cpp.exec(`cmd.exe /c taskkill /PID ${pid} /T /F`);
160
161
162
    } else {
        await cpp.exec(`pkill -P ${pid}`);
    }
163

164
165
166
167
    return Promise.resolve();
}

/**
168
 * get command of setting environment variable
169
 * @param  variable
170
 * @returns command string
171
 */
172
export function setEnvironmentVariable(variable: { key: string; value: string }): string {
173
174
    if (process.platform === 'win32') {
        return `$env:${variable.key}="${variable.value}"`;
175
    } else {
SparkSnail's avatar
SparkSnail committed
176
        return `export ${variable.key}='${variable.value}'`;
177
178
179
    }
}

180
181
/**
 * Compress files in directory to tar file
182
183
 * @param  sourcePath
 * @param  tarPath
184
 */
185
export async function tarAdd(tarPath: string, sourcePath: string): Promise<void> {
186
    if (process.platform === 'win32') {
187
188
189
190
191
        const tarFilePath: string = tarPath.split('\\')
                                    .join('\\\\');
        const sourceFilePath: string = sourcePath.split('\\')
                                   .join('\\\\');
        const script: string[] = [];
192
193
194
        script.push(
            `import os`,
            `import tarfile`,
195
            String.Format(`tar = tarfile.open("{0}","w:gz")\r\nfor root,dir,files in os.walk("{1}"):`, tarFilePath, sourceFilePath),
196
197
198
199
200
201
202
203
            `    for file in files:`,
            `        fullpath = os.path.join(root,file)`,
            `        tar.add(fullpath, arcname=file)`,
            `tar.close()`);
        await fs.promises.writeFile(path.join(os.tmpdir(), 'tar.py'), script.join(getNewLine()), { encoding: 'utf8', mode: 0o777 });
        const tarScript: string = path.join(os.tmpdir(), 'tar.py');
        await cpp.exec(`python ${tarScript}`);
    } else {
204
        await cpp.exec(`tar -czf ${tarPath} -C ${sourcePath} .`);
205
    }
206

207
208
    return Promise.resolve();
}
209
210
211

/**
 * generate script file name
212
 * @param fileNamePrefix
213
214
215
 */
export function getScriptName(fileNamePrefix: string): string {
    if (process.platform === 'win32') {
216
        return String.Format('{0}.ps1', fileNamePrefix);
217
    } else {
218
        return String.Format('{0}.sh', fileNamePrefix);
219
220
221
    }
}

222
223
224
225
226
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 {
227
    if (process.platform === 'win32') {
228
229
230
        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); });
231
    } else {
232
        cp.exec(getGpuMetricsCollectorBashScriptContent(scriptFolder), { shell: '/bin/bash' });
233
234
    }
}