util.ts 7.76 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
30
import { file } from '../../node_modules/@types/tmp';
import { GPU_INFO_COLLECTOR_FORMAT_LINUX, 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
49
50
51
    try {
        fileNameValid = await validateFileNameRecursively(codeDir);
    } catch(error) {
        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
62
    
    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);    
    }
63
64

    return fileCount;
65
66
}

67

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

79
80
81
    return Promise.resolve();
}

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

94
95
96
    return Promise.resolve();
}

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

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

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

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

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

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

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

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

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

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

179
180
/**
 * Compress files in directory to tar file
181
182
 * @param  sourcePath
 * @param  tarPath
183
 */
184
export async function tarAdd(tarPath: string, sourcePath: string): Promise<void> {
185
    if (process.platform === 'win32') {
186
187
188
189
190
        const tarFilePath: string = tarPath.split('\\')
                                    .join('\\\\');
        const sourceFilePath: string = sourcePath.split('\\')
                                   .join('\\\\');
        const script: string[] = [];
191
192
193
        script.push(
            `import os`,
            `import tarfile`,
194
            String.Format(`tar = tarfile.open("{0}","w:gz")\r\nfor root,dir,files in os.walk("{1}"):`, tarFilePath, sourceFilePath),
195
196
197
198
199
200
201
202
            `    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 {
203
        await cpp.exec(`tar -czf ${tarPath} -C ${sourcePath} .`);
204
    }
205

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

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

/**
 * generate script file
223
 * @param gpuMetricCollectorScriptFolder
224
225
 */
export function getgpuMetricsCollectorScriptContent(gpuMetricCollectorScriptFolder: string): string {
226
    if (process.platform === 'win32') {
227
228
229
        return String.Format(
            GPU_INFO_COLLECTOR_FORMAT_WINDOWS,
            gpuMetricCollectorScriptFolder,
230
            path.join(gpuMetricCollectorScriptFolder, 'pid')
231
232
233
234
235
        );
    } else {
        return String.Format(
            GPU_INFO_COLLECTOR_FORMAT_LINUX,
            gpuMetricCollectorScriptFolder,
236
            path.join(gpuMetricCollectorScriptFolder, 'pid')
237
238
239
        );
    }
}