forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreterVersion.ts
More file actions
32 lines (30 loc) · 1.63 KB
/
interpreterVersion.ts
File metadata and controls
32 lines (30 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { inject, injectable } from 'inversify';
import '../common/extensions';
import { IProcessServiceFactory } from '../common/process/types';
import { IInterpreterVersionService } from './contracts';
export const PIP_VERSION_REGEX = '\\d+\\.\\d+(\\.\\d+)?';
@injectable()
export class InterpreterVersionService implements IInterpreterVersionService {
constructor(@inject(IProcessServiceFactory) private readonly processServiceFactory: IProcessServiceFactory) { }
public async getVersion(pythonPath: string, defaultValue: string): Promise<string> {
const processService = await this.processServiceFactory.create();
return processService.exec(pythonPath, ['--version'], { mergeStdOutErr: true })
.then(output => output.stdout.splitLines()[0])
.then(version => version.length === 0 ? defaultValue : version)
.catch(() => defaultValue);
}
public async getPipVersion(pythonPath: string): Promise<string> {
const processService = await this.processServiceFactory.create();
const output = await processService.exec(pythonPath, ['-m', 'pip', '--version'], { mergeStdOutErr: true });
if (output.stdout.length > 0) {
// Here's a sample output:
// pip 9.0.1 from /Users/donjayamanne/anaconda3/lib/python3.6/site-packages (python 3.6).
const re = new RegExp(PIP_VERSION_REGEX, 'g');
const matches = re.exec(output.stdout);
if (matches && matches.length > 0) {
return matches[0].trim();
}
}
throw new Error(`Unable to determine pip version from output '${output.stdout}'`);
}
}