|
| 1 | +import fs from "node:fs"; |
| 2 | +import assert from "node:assert"; |
| 3 | +import { additionalFiles } from "@trigger.dev/build/extensions/core"; |
| 4 | +import { BuildManifest } from "@trigger.dev/core/v3"; |
| 5 | +import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build"; |
| 6 | + |
| 7 | +export type PythonOptions = { |
| 8 | + requirements?: string[]; |
| 9 | + requirementsFile?: string; |
| 10 | + /** |
| 11 | + * [Dev-only] The path to the python binary. |
| 12 | + * |
| 13 | + * @remarks |
| 14 | + * This option is typically used during local development or in specific testing environments |
| 15 | + * where a particular Python installation needs to be targeted. It should point to the full path of the python executable. |
| 16 | + * |
| 17 | + * Example: `/usr/bin/python3` or `C:\\Python39\\python.exe` |
| 18 | + */ |
| 19 | + pythonBinaryPath?: string; |
| 20 | + /** |
| 21 | + * An array of glob patterns that specify which Python scripts are allowed to be executed. |
| 22 | + * |
| 23 | + * @remarks |
| 24 | + * These scripts will be copied to the container during the build process. |
| 25 | + */ |
| 26 | + scripts?: string[]; |
| 27 | +}; |
| 28 | + |
| 29 | +const splitAndCleanComments = (str: string) => |
| 30 | + str |
| 31 | + .split("\n") |
| 32 | + .map((line) => line.trim()) |
| 33 | + .filter((line) => line && !line.startsWith("#")); |
| 34 | + |
| 35 | +export function pythonExtension(options: PythonOptions = {}): BuildExtension { |
| 36 | + return new PythonExtension(options); |
| 37 | +} |
| 38 | + |
| 39 | +class PythonExtension implements BuildExtension { |
| 40 | + public readonly name = "PythonExtension"; |
| 41 | + |
| 42 | + constructor(private options: PythonOptions = {}) { |
| 43 | + assert( |
| 44 | + !(this.options.requirements && this.options.requirementsFile), |
| 45 | + "Cannot specify both requirements and requirementsFile" |
| 46 | + ); |
| 47 | + |
| 48 | + if (this.options.requirementsFile) { |
| 49 | + assert( |
| 50 | + fs.existsSync(this.options.requirementsFile), |
| 51 | + `Requirements file not found: ${this.options.requirementsFile}` |
| 52 | + ); |
| 53 | + this.options.requirements = splitAndCleanComments( |
| 54 | + fs.readFileSync(this.options.requirementsFile, "utf-8") |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + async onBuildComplete(context: BuildContext, manifest: BuildManifest) { |
| 60 | + await additionalFiles({ |
| 61 | + files: this.options.scripts ?? [], |
| 62 | + }).onBuildComplete!(context, manifest); |
| 63 | + |
| 64 | + if (context.target === "dev") { |
| 65 | + if (this.options.pythonBinaryPath) { |
| 66 | + process.env.PYTHON_BIN_PATH = this.options.pythonBinaryPath; |
| 67 | + } |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + context.logger.debug(`Adding ${this.name} to the build`); |
| 73 | + |
| 74 | + context.addLayer({ |
| 75 | + id: "python-installation", |
| 76 | + image: { |
| 77 | + instructions: splitAndCleanComments(` |
| 78 | + # Install Python |
| 79 | + RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 80 | + python3 python3-pip python3-venv && \ |
| 81 | + apt-get clean && rm -rf /var/lib/apt/lists/* |
| 82 | +
|
| 83 | + # Set up Python environment |
| 84 | + RUN python3 -m venv /opt/venv |
| 85 | + ENV PATH="/opt/venv/bin:$PATH" |
| 86 | + `), |
| 87 | + }, |
| 88 | + deploy: { |
| 89 | + env: { |
| 90 | + PYTHON_BIN_PATH: `/opt/venv/bin/python`, |
| 91 | + }, |
| 92 | + override: true, |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + context.addLayer({ |
| 97 | + id: "python-dependencies", |
| 98 | + build: { |
| 99 | + env: { |
| 100 | + REQUIREMENTS_CONTENT: this.options.requirements?.join("\n") || "", |
| 101 | + }, |
| 102 | + }, |
| 103 | + image: { |
| 104 | + instructions: splitAndCleanComments(` |
| 105 | + ARG REQUIREMENTS_CONTENT |
| 106 | + RUN echo "$REQUIREMENTS_CONTENT" > requirements.txt |
| 107 | +
|
| 108 | + # Install dependencies |
| 109 | + RUN pip install --no-cache-dir -r requirements.txt |
| 110 | + `), |
| 111 | + }, |
| 112 | + deploy: { |
| 113 | + override: true, |
| 114 | + }, |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export default pythonExtension; |
0 commit comments