-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathPythonTaskHelper.ts
144 lines (120 loc) · 5.98 KB
/
PythonTaskHelper.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import { PythonScaffoldingOptions } from '../../debugging/DockerDebugScaffoldingProvider';
import { inferPythonArgs } from '../../utils/pythonUtils';
import { unresolveWorkspaceFolder } from "../../utils/resolveVariables";
import { DockerBuildOptions } from "../DockerBuildTaskDefinitionBase";
import { DockerBuildTaskDefinition } from "../DockerBuildTaskProvider";
import { DockerContainerVolume, DockerRunOptions, DockerRunTaskDefinitionBase } from "../DockerRunTaskDefinitionBase";
import { DockerRunTaskDefinition } from "../DockerRunTaskProvider";
import { DockerBuildTaskContext, DockerRunTaskContext, DockerTaskScaffoldContext, TaskHelper, addVolumeWithoutConflicts, getDefaultContainerName, getDefaultImageName, inferImageName } from "../TaskHelper";
import { PythonExtensionHelper } from "./PythonExtensionHelper";
export interface PythonTaskRunOptions {
file?: string;
module?: string;
args?: string[];
wait?: boolean;
debugPort?: number
}
export interface PythonRunTaskDefinition extends DockerRunTaskDefinitionBase {
python?: PythonTaskRunOptions;
}
export class PythonTaskHelper implements TaskHelper {
public async provideDockerBuildTasks(context: DockerTaskScaffoldContext): Promise<DockerBuildTaskDefinition[]> {
return [
{
type: 'docker-build',
label: 'docker-build',
platform: 'python',
dockerBuild: {
tag: getDefaultImageName(context.folder.name),
dockerfile: unresolveWorkspaceFolder(context.dockerfile, context.folder),
// eslint-disable-next-line no-template-curly-in-string
context: '${workspaceFolder}',
pull: true
},
}
];
}
public async provideDockerRunTasks(context: DockerTaskScaffoldContext, options: PythonScaffoldingOptions): Promise<DockerRunTaskDefinition[]> {
const runOptions: PythonTaskRunOptions = {
args: inferPythonArgs(options.projectType, context.ports)
};
let dockerRunOptions: DockerRunOptions | undefined;
if ('file' in options.target) {
runOptions.file = options.target.file;
} else {
runOptions.module = options.target.module;
}
// If the projectType is flask, then we set the module to 'flask' and
// set whatever the user entered in an env variable named "FLASK_APP".
if (options.projectType === 'flask') {
dockerRunOptions = {
env: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"FLASK_APP": runOptions.file || runOptions.module
}
};
runOptions.module = 'flask';
runOptions.file = undefined;
} else if (options.projectType === 'fastapi') {
const basename = path.basename(runOptions.file, '.py');
const dirname = path.dirname(runOptions.file).replace(path.sep, '.');
if (dirname !== '.') {
runOptions.args.unshift(`${dirname}.${basename}:app`);
} else {
runOptions.args.unshift(`${basename}:app`);
}
runOptions.module = 'uvicorn';
runOptions.file = undefined;
}
return [{
type: 'docker-run',
label: 'docker-run: debug',
dependsOn: ['docker-build'],
dockerRun: dockerRunOptions,
python: runOptions,
}];
}
public async getDockerBuildOptions(context: DockerBuildTaskContext, buildDefinition: DockerBuildTaskDefinition): Promise<DockerBuildOptions> {
const buildOptions = buildDefinition.dockerBuild;
/* eslint-disable no-template-curly-in-string */
buildOptions.context = buildOptions.context || '${workspaceFolder}';
buildOptions.dockerfile = buildOptions.dockerfile || '${workspaceFolder}/Dockerfile';
/* eslint-enable no-template-curly-in-string */
buildOptions.tag = buildOptions.tag || getDefaultImageName(context.folder.name);
return buildOptions;
}
public async getDockerRunOptions(context: DockerRunTaskContext, runDefinition: DockerRunTaskDefinition): Promise<DockerRunOptions> {
const runOptions: DockerRunOptions = runDefinition.dockerRun;
const launcherFolder: string = await PythonExtensionHelper.getLauncherFolderPath();
runOptions.image = inferImageName(
runDefinition,
context,
context.folder.name
);
runOptions.containerName = runOptions.containerName || getDefaultContainerName(context.folder.name);
// User input is honored in all of the below.
runOptions.volumes = this.inferVolumes(runOptions, launcherFolder);
// If the user specifies command, we won't set entrypoint; otherwise if they set entrypoint we will respect it; otherwise use 'python3' to start an idle container
runOptions.entrypoint = runOptions.command ? undefined : runOptions.entrypoint || 'python3';
return runOptions;
}
private inferVolumes(runOptions: DockerRunOptions, launcherFolder: string): DockerContainerVolume[] {
if (!launcherFolder) {
return [];
}
const volumes = runOptions?.volumes ? [...runOptions.volumes] : [];
const dbgVolume: DockerContainerVolume = {
localPath: launcherFolder,
containerPath: '/debugpy',
permissions: 'ro'
};
addVolumeWithoutConflicts(volumes, dbgVolume);
return volumes;
}
}
export const pythonTaskHelper = new PythonTaskHelper();