forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcondaCreationProvider.ts
227 lines (206 loc) · 8.24 KB
/
condaCreationProvider.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { CancellationToken, ProgressLocation, WorkspaceFolder } from 'vscode';
import * as path from 'path';
import { Commands, PVSC_EXTENSION_ID } from '../../../common/constants';
import { traceError, traceLog } from '../../../logging';
import {
CreateEnvironmentOptions,
CreateEnvironmentProgress,
CreateEnvironmentProvider,
CreateEnvironmentResult,
} from '../types';
import { pickWorkspaceFolder } from '../common/workspaceSelection';
import { execObservable } from '../../../common/process/rawProcessApis';
import { createDeferred } from '../../../common/utils/async';
import { getEnvironmentVariable, getOSType, OSType } from '../../../common/utils/platform';
import { createCondaScript } from '../../../common/process/internal/scripts';
import { Common, CreateEnv } from '../../../common/utils/localize';
import { getConda, pickPythonVersion } from './condaUtils';
import { showErrorMessageWithLogs } from '../common/commonUtils';
import { withProgress } from '../../../common/vscodeApis/windowApis';
import { EventName } from '../../../telemetry/constants';
import { sendTelemetryEvent } from '../../../telemetry';
import {
CondaProgressAndTelemetry,
CONDA_ENV_CREATED_MARKER,
CONDA_ENV_EXISTING_MARKER,
} from './condaProgressAndTelemetry';
function generateCommandArgs(version?: string, options?: CreateEnvironmentOptions): string[] {
let addGitIgnore = true;
let installPackages = true;
if (options) {
addGitIgnore = options?.ignoreSourceControl !== undefined ? options.ignoreSourceControl : true;
installPackages = options?.installPackages !== undefined ? options.installPackages : true;
}
const command: string[] = [createCondaScript()];
if (addGitIgnore) {
command.push('--git-ignore');
}
if (installPackages) {
command.push('--install');
}
if (version) {
command.push('--python');
command.push(version);
}
return command;
}
function getCondaEnvFromOutput(output: string): string | undefined {
try {
const envPath = output
.split(/\r?\n/g)
.map((s) => s.trim())
.filter((s) => s.startsWith(CONDA_ENV_CREATED_MARKER) || s.startsWith(CONDA_ENV_EXISTING_MARKER))[0];
if (envPath.includes(CONDA_ENV_CREATED_MARKER)) {
return envPath.substring(CONDA_ENV_CREATED_MARKER.length);
}
return envPath.substring(CONDA_ENV_EXISTING_MARKER.length);
} catch (ex) {
traceError('Parsing out environment path failed.');
return undefined;
}
}
async function createCondaEnv(
workspace: WorkspaceFolder,
command: string,
args: string[],
progress: CreateEnvironmentProgress,
token?: CancellationToken,
): Promise<string | undefined> {
progress.report({
message: CreateEnv.Conda.creating,
});
const deferred = createDeferred<string>();
let pathEnv = getEnvironmentVariable('PATH') || getEnvironmentVariable('Path') || '';
if (getOSType() === OSType.Windows) {
// On windows `conda.bat` is used, which adds the following bin directories to PATH
// then launches `conda.exe` which is a stub to `python.exe -m conda`. Here, we are
// instead using the `python.exe` that ships with conda to run a python script that
// handles conda env creation and package installation.
// See conda issue: https://github.com/conda/conda/issues/11399
const root = path.dirname(command);
const libPath1 = path.join(root, 'Library', 'bin');
const libPath2 = path.join(root, 'Library', 'mingw-w64', 'bin');
const libPath3 = path.join(root, 'Library', 'usr', 'bin');
const libPath4 = path.join(root, 'bin');
const libPath5 = path.join(root, 'Scripts');
const libPath = [libPath1, libPath2, libPath3, libPath4, libPath5].join(path.delimiter);
pathEnv = `${libPath}${path.delimiter}${pathEnv}`;
}
traceLog('Running Conda Env creation script: ', [command, ...args]);
const { out, dispose } = execObservable(command, args, {
mergeStdOutErr: true,
token,
cwd: workspace.uri.fsPath,
env: {
PATH: pathEnv,
},
});
const progressAndTelemetry = new CondaProgressAndTelemetry(progress);
let condaEnvPath: string | undefined;
out.subscribe(
(value) => {
const output = value.out.splitLines().join('\r\n');
traceLog(output);
if (output.includes(CONDA_ENV_CREATED_MARKER) || output.includes(CONDA_ENV_EXISTING_MARKER)) {
condaEnvPath = getCondaEnvFromOutput(output);
}
progressAndTelemetry.process(output);
},
async (error) => {
traceError('Error while running conda env creation script: ', error);
deferred.reject(error);
},
() => {
dispose();
if (!deferred.rejected) {
deferred.resolve(condaEnvPath);
}
},
);
return deferred.promise;
}
function getExecutableCommand(condaPath: string): string {
if (getOSType() === OSType.Windows) {
// Both Miniconda3 and Anaconda3 have the following structure:
// Miniconda3 (or Anaconda3)
// |- condabin
// | |- conda.bat <--- this actually points to python.exe below,
// | after adding few paths to PATH.
// |- Scripts
// | |- conda.exe <--- this is the path we get as condaPath,
// | which is really a stub for `python.exe -m conda`.
// |- python.exe <--- this is the python that we want.
return path.join(path.dirname(path.dirname(condaPath)), 'python.exe');
}
// On non-windows machines:
// miniconda (or miniforge or anaconda3)
// |- bin
// |- conda <--- this is the path we get as condaPath.
// |- python <--- this is the python that we want.
return path.join(path.dirname(condaPath), 'python');
}
async function createEnvironment(options?: CreateEnvironmentOptions): Promise<CreateEnvironmentResult | undefined> {
const conda = await getConda();
if (!conda) {
return undefined;
}
const workspace = (await pickWorkspaceFolder()) as WorkspaceFolder | undefined;
if (!workspace) {
traceError('Workspace was not selected or found for creating virtual env.');
return undefined;
}
const version = await pickPythonVersion();
if (!version) {
traceError('Conda environments for use with python extension require Python.');
return undefined;
}
return withProgress(
{
location: ProgressLocation.Notification,
title: `${CreateEnv.statusTitle} ([${Common.showLogs}](command:${Commands.ViewOutput}))`,
cancellable: true,
},
async (
progress: CreateEnvironmentProgress,
token: CancellationToken,
): Promise<CreateEnvironmentResult | undefined> => {
let hasError = false;
progress.report({
message: CreateEnv.statusStarting,
});
let envPath: string | undefined;
try {
sendTelemetryEvent(EventName.ENVIRONMENT_CREATING, undefined, {
environmentType: 'conda',
pythonVersion: version,
});
envPath = await createCondaEnv(
workspace,
getExecutableCommand(conda),
generateCommandArgs(version, options),
progress,
token,
);
} catch (ex) {
traceError(ex);
hasError = true;
throw ex;
} finally {
if (hasError) {
showErrorMessageWithLogs(CreateEnv.Conda.errorCreatingEnvironment);
}
}
return { path: envPath, uri: workspace.uri };
},
);
}
export function condaCreationProvider(): CreateEnvironmentProvider {
return {
createEnvironment,
name: 'Conda',
description: CreateEnv.Conda.providerDescription,
id: `${PVSC_EXTENSION_ID}:conda`,
};
}