forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduleInstaller.ts
More file actions
132 lines (123 loc) · 6.74 KB
/
moduleInstaller.ts
File metadata and controls
132 lines (123 loc) · 6.74 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { injectable } from 'inversify';
import * as path from 'path';
import { CancellationToken, OutputChannel, ProgressLocation, ProgressOptions } from 'vscode';
import { IInterpreterService } from '../../interpreter/contracts';
import { IServiceContainer } from '../../ioc/types';
import { InterpreterType } from '../../pythonEnvironments/discovery/types';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { IApplicationShell } from '../application/types';
import { wrapCancellationTokens } from '../cancellation';
import { STANDARD_OUTPUT_CHANNEL } from '../constants';
import { IFileSystem } from '../platform/types';
import * as internalPython from '../process/internal/python';
import { ITerminalServiceFactory } from '../terminal/types';
import { ExecutionInfo, IConfigurationService, IOutputChannel } from '../types';
import { Products } from '../utils/localize';
import { isResource } from '../utils/misc';
import { IModuleInstaller, InterpreterUri } from './types';
@injectable()
export abstract class ModuleInstaller implements IModuleInstaller {
public abstract get priority(): number;
public abstract get name(): string;
public abstract get displayName(): string;
constructor(protected serviceContainer: IServiceContainer) {}
public async installModule(name: string, resource?: InterpreterUri, cancel?: CancellationToken): Promise<void> {
sendTelemetryEvent(EventName.PYTHON_INSTALL_PACKAGE, undefined, { installer: this.displayName });
const uri = isResource(resource) ? resource : undefined;
const executionInfo = await this.getExecutionInfo(name, resource);
const terminalService = this.serviceContainer
.get<ITerminalServiceFactory>(ITerminalServiceFactory)
.getTerminalService(uri);
const install = async (token?: CancellationToken) => {
const executionInfoArgs = await this.processInstallArgs(executionInfo.args, resource);
if (executionInfo.moduleName) {
const configService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
const settings = configService.getSettings(uri);
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreter = isResource(resource)
? await interpreterService.getActiveInterpreter(resource)
: resource;
const pythonPath = isResource(resource) ? settings.pythonPath : resource.path;
const args = internalPython.execModule(executionInfo.moduleName, executionInfoArgs);
if (!interpreter || interpreter.type !== InterpreterType.Unknown) {
await terminalService.sendCommand(pythonPath, args, token);
} else if (settings.globalModuleInstallation) {
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
if (await fs.isDirReadonly(path.dirname(pythonPath)).catch((_err) => true)) {
this.elevatedInstall(pythonPath, args);
} else {
await terminalService.sendCommand(pythonPath, args, token);
}
} else {
await terminalService.sendCommand(pythonPath, args.concat(['--user']), token);
}
} else {
await terminalService.sendCommand(executionInfo.execPath!, executionInfoArgs, token);
}
};
// Display progress indicator if we have ability to cancel this operation from calling code.
// This is required as its possible the installation can take a long time.
// (i.e. if installation takes a long time in terminal or like, a progress indicator is necessary to let user know what is being waited on).
if (cancel) {
const shell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
const options: ProgressOptions = {
location: ProgressLocation.Notification,
cancellable: true,
title: Products.installingModule().format(name)
};
await shell.withProgress(options, async (_, token: CancellationToken) =>
install(wrapCancellationTokens(token, cancel))
);
} else {
await install(cancel);
}
}
public abstract isSupported(resource?: InterpreterUri): Promise<boolean>;
protected elevatedInstall(execPath: string, args: string[]) {
const options = {
name: 'VS Code Python'
};
const outputChannel = this.serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
const command = `"${execPath.replace(/\\/g, '/')}" ${args.join(' ')}`;
outputChannel.appendLine('');
outputChannel.appendLine(`[Elevated] ${command}`);
// tslint:disable-next-line:no-require-imports no-var-requires
const sudo = require('sudo-prompt');
sudo.exec(command, options, async (error: string, stdout: string, stderr: string) => {
if (error) {
const shell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
await shell.showErrorMessage(error);
} else {
outputChannel.show();
if (stdout) {
outputChannel.appendLine('');
outputChannel.append(stdout);
}
if (stderr) {
outputChannel.appendLine('');
outputChannel.append(`Warning: ${stderr}`);
}
}
});
}
protected abstract getExecutionInfo(moduleName: string, resource?: InterpreterUri): Promise<ExecutionInfo>;
private async processInstallArgs(args: string[], resource?: InterpreterUri): Promise<string[]> {
const indexOfPylint = args.findIndex((arg) => arg.toUpperCase() === 'PYLINT');
if (indexOfPylint === -1) {
return args;
}
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreter = isResource(resource) ? await interpreterService.getActiveInterpreter(resource) : resource;
// If installing pylint on python 2.x, then use pylint~=1.9.0
if (interpreter && interpreter.version && interpreter.version.major === 2) {
const newArgs = [...args];
// This command could be sent to the terminal, hence '<' needs to be escaped for UNIX.
newArgs[indexOfPylint] = '"pylint<2.0.0"';
return newArgs;
}
return args;
}
}