util.ts 6.88 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 * as os from 'os';
10
11
import * as path from 'path';
import { String } from 'typescript-string-operations';
12
import { countFilesRecursively, getNewLine, validateFileNameRecursively } from '../../common/utils';
13
import { GPU_INFO_COLLECTOR_FORMAT_WINDOWS } from './gpuData';
14
15
16

/**
 * Validate codeDir, calculate file count recursively under codeDir, and throw error if any rule is broken
17
 *
18
19
20
 * @param codeDir codeDir in nni config file
 * @returns file number under codeDir
 */
21
// tslint:disable: no-redundant-jsdoc
chicm-ms's avatar
chicm-ms committed
22
export async function validateCodeDir(codeDir: string): Promise<number> {
23
    let fileCount: number | undefined;
24
    let fileNameValid: boolean = true;
25
26
    try {
        fileCount = await countFilesRecursively(codeDir);
27
    } catch (error) {
28
29
        throw new Error(`Call count file error: ${error}`);
    }
30
31
    try {
        fileNameValid = await validateFileNameRecursively(codeDir);
32
    } catch (error) {
33
34
        throw new Error(`Validate file name error: ${error}`);
    }
35

36
    if (fileCount !== undefined && fileCount > 1000) {
37
        const errMessage: string = `Too many files(${fileCount} found}) in ${codeDir},`
38
                                    + ` please check if it's a valid code dir`;
39
        throw new Error(errMessage);
40
    }
41
42
43
44

    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);
45
    }
46
47

    return fileCount;
48
49
50
51
}

/**
 * crete a new directory
52
 * @param directory
53
 */
54
export async function execMkdir(directory: string, share: boolean = false): Promise<void> {
55
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
56
        await cpp.exec(`powershell.exe New-Item -Path "${directory}" -ItemType "directory" -Force`);
57
    } else if (share) {
SparkSnail's avatar
SparkSnail committed
58
        await cpp.exec(`(umask 0; mkdir -p '${directory}')`);
59
    } else {
SparkSnail's avatar
SparkSnail committed
60
        await cpp.exec(`mkdir -p '${directory}'`);
61
    }
62

63
64
65
    return Promise.resolve();
}

66
67
68
69
70
71
72
/**
 * 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
73
        await cpp.exec(`powershell.exe Copy-Item "${source}" -Destination "${destination}" -Recurse`);
74
    } else {
SparkSnail's avatar
SparkSnail committed
75
        await cpp.exec(`cp -r '${source}/.' '${destination}'`);
76
    }
77

78
79
80
    return Promise.resolve();
}

81
82
/**
 * crete a new file
83
 * @param filename
84
85
86
 */
export async function execNewFile(filename: string): Promise<void> {
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
87
        await cpp.exec(`powershell.exe New-Item -Path "${filename}" -ItemType "file" -Force`);
88
    } else {
SparkSnail's avatar
SparkSnail committed
89
        await cpp.exec(`touch '${filename}'`);
90
    }
91

92
93
94
95
    return Promise.resolve();
}

/**
96
 * run script using powershell or bash
97
98
 * @param filePath
 */
99
export function runScript(filePath: string): cp.ChildProcess {
100
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
101
        return cp.exec(`powershell.exe -ExecutionPolicy Bypass -file "${filePath}"`);
102
    } else {
SparkSnail's avatar
SparkSnail committed
103
        return cp.exec(`bash '${filePath}'`);
104
105
106
107
108
    }
}

/**
 * output the last line of a file
109
 * @param filePath
110
111
112
113
 */
export async function execTail(filePath: string): Promise<cpp.childProcessPromise.Result> {
    let cmdresult: cpp.childProcessPromise.Result;
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
114
        cmdresult = await cpp.exec(`powershell.exe Get-Content "${filePath}" -Tail 1`);
115
    } else {
SparkSnail's avatar
SparkSnail committed
116
        cmdresult = await cpp.exec(`tail -n 1 '${filePath}'`);
117
    }
118

119
120
121
122
123
    return Promise.resolve(cmdresult);
}

/**
 * delete a directory
124
 * @param directory
125
 */
126
export async function execRemove(directory: string): Promise<void> {
127
    if (process.platform === 'win32') {
SparkSnail's avatar
SparkSnail committed
128
        await cpp.exec(`powershell.exe Remove-Item "${directory}" -Recurse -Force`);
129
    } else {
SparkSnail's avatar
SparkSnail committed
130
        await cpp.exec(`rm -rf '${directory}'`);
131
    }
132

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

/**
 * kill a process
138
 * @param directory
139
 */
140
export async function execKill(pid: string): Promise<void> {
141
    if (process.platform === 'win32') {
Yuge Zhang's avatar
Yuge Zhang committed
142
        await cpp.exec(`cmd.exe /c taskkill /PID ${pid} /T /F`);
143
144
145
    } else {
        await cpp.exec(`pkill -P ${pid}`);
    }
146

147
148
149
150
    return Promise.resolve();
}

/**
151
 * get command of setting environment variable
152
 * @param  variable
153
 * @returns command string
154
 */
155
export function setEnvironmentVariable(variable: { key: string; value: string }): string {
156
157
    if (process.platform === 'win32') {
        return `$env:${variable.key}="${variable.value}"`;
158
    } else {
SparkSnail's avatar
SparkSnail committed
159
        return `export ${variable.key}='${variable.value}'`;
160
161
162
    }
}

163
164
/**
 * Compress files in directory to tar file
165
166
 * @param  sourcePath
 * @param  tarPath
167
 */
168
export async function tarAdd(tarPath: string, sourcePath: string): Promise<void> {
169
    if (process.platform === 'win32') {
170
171
172
173
174
        const tarFilePath: string = tarPath.split('\\')
                                    .join('\\\\');
        const sourceFilePath: string = sourcePath.split('\\')
                                   .join('\\\\');
        const script: string[] = [];
175
176
177
        script.push(
            `import os`,
            `import tarfile`,
178
            String.Format(`tar = tarfile.open("{0}","w:gz")\r\nfor root,dir,files in os.walk("{1}"):`, tarFilePath, sourceFilePath),
179
180
181
182
183
184
185
186
            `    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 {
187
        await cpp.exec(`tar -czf ${tarPath} -C ${sourcePath} .`);
188
    }
189

190
191
    return Promise.resolve();
}
192
193
194

/**
 * generate script file name
195
 * @param fileNamePrefix
196
197
198
 */
export function getScriptName(fileNamePrefix: string): string {
    if (process.platform === 'win32') {
199
        return String.Format('{0}.ps1', fileNamePrefix);
200
    } else {
201
        return String.Format('{0}.sh', fileNamePrefix);
202
203
204
    }
}

205
206
207
208
209
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 {
210
    if (process.platform === 'win32') {
211
212
213
        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); });
214
    } else {
215
        cp.exec(getGpuMetricsCollectorBashScriptContent(scriptFolder), { shell: '/bin/bash' });
216
217
    }
}