forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonExecutionFactory.ts
More file actions
60 lines (58 loc) · 3.77 KB
/
pythonExecutionFactory.ts
File metadata and controls
60 lines (58 loc) · 3.77 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import { IEnvironmentActivationService } from '../../interpreter/activation/types';
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 { IConfigurationService, IDisposableRegistry } from '../types';
import { ProcessService } from './proc';
import { PythonExecutionService } from './pythonProcess';
import {
ExecutionFactoryCreateWithEnvironmentOptions,
ExecutionFactoryCreationOptions,
IBufferDecoder,
IProcessLogger,
IProcessService,
IProcessServiceFactory,
IPythonExecutionFactory,
IPythonExecutionService
} from './types';
import { WindowsStorePythonProcess } from './windowsStorePythonProcess';
@injectable()
export class PythonExecutionFactory implements IPythonExecutionFactory {
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(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));
if (this.windowsStoreInterpreter.isWindowsStoreInterpreter(pythonPath)) {
return new WindowsStorePythonProcess(this.serviceContainer, processService, pythonPath, this.windowsStoreInterpreter);
}
return new PythonExecutionService(this.serviceContainer, processService, pythonPath);
}
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 new PythonExecutionService(this.serviceContainer, processService, pythonPath);
}
}