forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
201 lines (180 loc) · 5.71 KB
/
extension.ts
File metadata and controls
201 lines (180 loc) · 5.71 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
import fs from "node:fs";
import assert from "node:assert";
import { addAdditionalFilesToBuild } from "@trigger.dev/build/internal";
import { BuildManifest } from "@trigger.dev/core/v3";
import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build";
export type PythonOptions = {
requirements?: string[];
requirementsFile?: string;
pyprojectFile?: string;
useUv?: boolean;
/**
* [Dev-only] The path to the python binary.
*
* @remarks
* This option is typically used during local development or in specific testing environments
* where a particular Python installation needs to be targeted. It should point to the full path of the python executable.
*
* Example: `/usr/bin/python3` or `C:\\Python39\\python.exe`
*/
devPythonBinaryPath?: string;
/**
* An array of glob patterns that specify which Python scripts are allowed to be executed.
*
* @remarks
* These scripts will be copied to the container during the build process.
*/
scripts?: string[];
};
const splitAndCleanComments = (str: string) =>
str
.split("\n")
.map((line) => line.trim())
.filter((line) => line && !line.startsWith("#"));
export function pythonExtension(options: PythonOptions = {}): BuildExtension {
return new PythonExtension(options);
}
class PythonExtension implements BuildExtension {
public readonly name = "PythonExtension";
constructor(private options: PythonOptions = {}) {
assert(
!(this.options.requirements && this.options.requirementsFile),
"Cannot specify both requirements and requirementsFile"
);
if (this.options.requirementsFile) {
assert(
fs.existsSync(this.options.requirementsFile),
`Requirements file not found: ${this.options.requirementsFile}`
);
this.options.requirements = splitAndCleanComments(
fs.readFileSync(this.options.requirementsFile, "utf-8")
);
}
if (this.options.pyprojectFile) {
assert(
fs.existsSync(this.options.pyprojectFile),
`pyproject.toml not found: ${this.options.pyprojectFile}`
);
}
}
async onBuildComplete(context: BuildContext, manifest: BuildManifest) {
await addAdditionalFilesToBuild(
"pythonExtension",
{
files: this.options.scripts ?? [],
},
context,
manifest
);
if (context.target === "dev") {
if (this.options.devPythonBinaryPath) {
process.env.PYTHON_BIN_PATH = this.options.devPythonBinaryPath;
}
return;
}
context.logger.debug(`Adding ${this.name} to the build`);
context.addLayer({
id: "python-installation",
image: {
instructions: splitAndCleanComments(`
# Install Python
RUN apt-get update && apt-get install -y --no-install-recommends \
python3 python3-pip python3-venv && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Set up Python environment
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
${this.options.useUv ? "RUN pip install uv" : ""}
`),
},
deploy: {
env: {
PYTHON_BIN_PATH: `/opt/venv/bin/python`,
},
override: true,
},
});
if (this.options.requirementsFile) {
if (this.options.requirements) {
context.logger.warn(
`[pythonExtension] Both options.requirements and options.requirementsFile are specified. requirements will be ignored.`
);
}
// Copy requirements file to the container
await addAdditionalFilesToBuild(
"pythonExtension",
{
files: [this.options.requirementsFile],
},
context,
manifest
);
// Add a layer to the build that installs the requirements
context.addLayer({
id: "python-dependencies",
image: {
instructions: splitAndCleanComments(`
# Copy the requirements file
COPY ${this.options.requirementsFile} .
# Install dependencies
${this.options.useUv
? `RUN uv pip install --no-cache -r ${this.options.requirementsFile}`
: `RUN pip install --no-cache-dir -r ${this.options.requirementsFile}`
}
`),
},
deploy: {
override: true,
},
});
} else if (this.options.pyprojectFile) {
// Copy pyproject file to the container
await addAdditionalFilesToBuild(
"pythonExtension",
{
files: [this.options.pyprojectFile],
},
context,
manifest
);
// Add a layer to the build that installs the dependencies
context.addLayer({
id: "python-dependencies",
image: {
instructions: splitAndCleanComments(`
# Copy the pyproject file
COPY ${this.options.pyprojectFile} .
# Install dependencies
${this.options.useUv ? "RUN uv pip install ." : "RUN pip install ."}
`),
},
deploy: {
override: true,
},
});
} else if (this.options.requirements) {
context.addLayer({
id: "python-dependencies",
build: {
env: {
REQUIREMENTS_CONTENT: this.options.requirements?.join("\n") || "",
},
},
image: {
instructions: splitAndCleanComments(`
ARG REQUIREMENTS_CONTENT
RUN echo "$REQUIREMENTS_CONTENT" > requirements.txt
# Install dependencies
${this.options.useUv
? "RUN uv pip install --no-cache -r requirements.txt"
: "RUN pip install --no-cache-dir -r requirements.txt"
}
`),
},
deploy: {
override: true,
},
});
}
}
}