forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpythonExecutionFactory.ts
More file actions
229 lines (215 loc) · 10.8 KB
/
Copy pathpythonExecutionFactory.ts
File metadata and controls
229 lines (215 loc) · 10.8 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import { gte } from 'semver';
import { Uri } from 'vscode';
import { IEnvironmentActivationService } from '../../interpreter/activation/types';
import { CondaEnvironmentInfo, ICondaService, IInterpreterService } from '../../interpreter/contracts';
import { WindowsStoreInterpreter } from '../../interpreter/locators/services/windowsStoreInterpreter';
import { IWindowsStoreInterpreter } from '../../interpreter/locators/types';
import { IServiceContainer } from '../../ioc/types';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { traceError } from '../logger';
import { IFileSystem } from '../platform/types';
import { IConfigurationService, IDisposable, IDisposableRegistry } from '../types';
import { ProcessService } from './proc';
import { PythonDaemonFactory } from './pythonDaemonFactory';
import { PythonDaemonExecutionServicePool } from './pythonDaemonPool';
import { createCondaEnv, createPythonEnv, createWindowsStoreEnv } from './pythonEnvironment';
import { createPythonProcessService } from './pythonProcess';
import {
DaemonExecutionFactoryCreationOptions,
ExecutionFactoryCreateWithEnvironmentOptions,
ExecutionFactoryCreationOptions,
IBufferDecoder,
IProcessLogger,
IProcessService,
IProcessServiceFactory,
IPythonDaemonExecutionService,
IPythonExecutionFactory,
IPythonExecutionService,
isDaemonPoolCreationOption
} from './types';
// Minimum version number of conda required to be able to use 'conda run'
export const CONDA_RUN_VERSION = '4.6.0';
@injectable()
export class PythonExecutionFactory implements IPythonExecutionFactory {
private readonly daemonsPerPythonService = new Map<string, Promise<IPythonDaemonExecutionService>>();
constructor(
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(IEnvironmentActivationService) private readonly activationHelper: IEnvironmentActivationService,
@inject(IProcessServiceFactory) private readonly processServiceFactory: IProcessServiceFactory,
@inject(IConfigurationService) private readonly configService: IConfigurationService,
@inject(ICondaService) private readonly condaService: ICondaService,
@inject(IBufferDecoder) private readonly decoder: IBufferDecoder,
@inject(WindowsStoreInterpreter) private readonly windowsStoreInterpreter: IWindowsStoreInterpreter
) {}
public async create(options: ExecutionFactoryCreationOptions): Promise<IPythonExecutionService> {
const pythonPath = options.pythonPath
? options.pythonPath
: this.configService.getSettings(options.resource).pythonPath;
const processService: IProcessService = await this.processServiceFactory.create(options.resource);
const processLogger = this.serviceContainer.get<IProcessLogger>(IProcessLogger);
processService.on('exec', processLogger.logProcess.bind(processLogger));
return createPythonService(
pythonPath,
processService,
this.serviceContainer.get<IFileSystem>(IFileSystem),
undefined,
this.windowsStoreInterpreter.isWindowsStoreInterpreter(pythonPath)
);
}
public async createDaemon<T extends IPythonDaemonExecutionService | IDisposable>(
options: DaemonExecutionFactoryCreationOptions
): Promise<T> {
const pythonPath = options.pythonPath
? options.pythonPath
: this.configService.getSettings(options.resource).pythonPath;
const daemonPoolKey = `${pythonPath}#${options.daemonClass || ''}#${options.daemonModule || ''}`;
const disposables = this.serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const logger = this.serviceContainer.get<IProcessLogger>(IProcessLogger);
const interpreter = await interpreterService.getInterpreterDetails(pythonPath);
const activatedProcPromise = this.createActivatedEnvironment({
allowEnvironmentFetchExceptions: true,
interpreter: interpreter,
resource: options.resource,
bypassCondaExecution: true
});
// No daemon support in Python 2.7.
if (interpreter?.version && interpreter.version.major < 3) {
return (activatedProcPromise! as unknown) as T;
}
// Ensure we do not start multiple daemons for the same interpreter.
// Cache the promise.
const start = async (): Promise<T> => {
const [activatedProc, activatedEnvVars] = await Promise.all([
activatedProcPromise,
this.activationHelper.getActivatedEnvironmentVariables(options.resource, interpreter, true)
]);
if (isDaemonPoolCreationOption(options)) {
const daemon = new PythonDaemonExecutionServicePool(
logger,
disposables,
{ ...options, pythonPath },
activatedProc!,
activatedEnvVars
);
await daemon.initialize();
disposables.push(daemon);
return (daemon as unknown) as T;
} else {
const factory = new PythonDaemonFactory(
disposables,
{ ...options, pythonPath },
activatedProc!,
activatedEnvVars
);
return factory.createDaemonService<T>();
}
};
let promise: Promise<T>;
if (isDaemonPoolCreationOption(options)) {
// Ensure we do not create multiple daemon pools for the same python interpreter.
promise = (this.daemonsPerPythonService.get(daemonPoolKey) as unknown) as Promise<T>;
if (!promise) {
promise = start();
this.daemonsPerPythonService.set(daemonPoolKey, promise as Promise<IPythonDaemonExecutionService>);
}
} else {
promise = start();
}
return promise.catch((ex) => {
// Ok, we failed to create the daemon (or failed to start).
// What ever the cause, we need to log this & give a standard IPythonExecutionService
traceError('Failed to create the daemon service, defaulting to activated environment', ex);
this.daemonsPerPythonService.delete(daemonPoolKey);
return (activatedProcPromise as unknown) as T;
});
}
public async createActivatedEnvironment(
options: ExecutionFactoryCreateWithEnvironmentOptions
): Promise<IPythonExecutionService> {
const envVars = await this.activationHelper.getActivatedEnvironmentVariables(
options.resource,
options.interpreter,
options.allowEnvironmentFetchExceptions
);
const hasEnvVars = envVars && Object.keys(envVars).length > 0;
sendTelemetryEvent(EventName.PYTHON_INTERPRETER_ACTIVATION_ENVIRONMENT_VARIABLES, undefined, { hasEnvVars });
if (!hasEnvVars) {
return this.create({
resource: options.resource,
pythonPath: options.interpreter ? options.interpreter.path : undefined
});
}
const pythonPath = options.interpreter
? options.interpreter.path
: this.configService.getSettings(options.resource).pythonPath;
const processService: IProcessService = new ProcessService(this.decoder, { ...envVars });
const processLogger = this.serviceContainer.get<IProcessLogger>(IProcessLogger);
processService.on('exec', processLogger.logProcess.bind(processLogger));
this.serviceContainer.get<IDisposableRegistry>(IDisposableRegistry).push(processService);
return createPythonService(pythonPath, processService, this.serviceContainer.get<IFileSystem>(IFileSystem));
}
// Not using this function for now because there are breaking issues with conda run (conda 4.8, PVSC 2020.1).
// See https://github.com/microsoft/vscode-python/issues/9490
public async createCondaExecutionService(
pythonPath: string,
processService?: IProcessService,
resource?: Uri
): Promise<IPythonExecutionService | undefined> {
const processServicePromise = processService
? Promise.resolve(processService)
: this.processServiceFactory.create(resource);
const [condaVersion, condaEnvironment, condaFile, procService] = await Promise.all([
this.condaService.getCondaVersion(),
this.condaService.getCondaEnvironment(pythonPath),
this.condaService.getCondaFile(),
processServicePromise
]);
if (condaVersion && gte(condaVersion, CONDA_RUN_VERSION) && condaEnvironment && condaFile && procService) {
// Add logging to the newly created process service
if (!processService) {
const processLogger = this.serviceContainer.get<IProcessLogger>(IProcessLogger);
procService.on('exec', processLogger.logProcess.bind(processLogger));
this.serviceContainer.get<IDisposableRegistry>(IDisposableRegistry).push(procService);
}
return createPythonService(
pythonPath,
procService,
this.serviceContainer.get<IFileSystem>(IFileSystem),
// This is what causes a CondaEnvironment to be returned:
[condaFile, condaEnvironment]
);
}
return Promise.resolve(undefined);
}
}
function createPythonService(
pythonPath: string,
procService: IProcessService,
fs: IFileSystem,
conda?: [string, CondaEnvironmentInfo],
isWindowsStore?: boolean
): IPythonExecutionService {
let env = createPythonEnv(pythonPath, procService, fs);
if (conda) {
const [condaPath, condaInfo] = conda;
env = createCondaEnv(condaPath, condaInfo, pythonPath, procService, fs);
} else if (isWindowsStore) {
env = createWindowsStoreEnv(pythonPath, procService);
}
const procs = createPythonProcessService(procService, env);
return {
getInterpreterInformation: () => env.getInterpreterInformation(),
getExecutablePath: () => env.getExecutablePath(),
isModuleInstalled: (m) => env.isModuleInstalled(m),
getExecutionInfo: (a) => env.getExecutionInfo(a),
execObservable: (a, o) => procs.execObservable(a, o),
execModuleObservable: (m, a, o) => procs.execModuleObservable(m, a, o),
exec: (a, o) => procs.exec(a, o),
execModule: (m, a, o) => procs.execModule(m, a, o)
};
}