-
-
Notifications
You must be signed in to change notification settings - Fork 873
feat(build): Add support for Python scripts via pythonExtension
#1686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
2a4aa8c
987ab35
8b97cec
84699e5
d00d147
500d82b
f0b30d6
b63554e
8ad7009
c3cbd61
be2cefe
bb59bf8
65d84d0
7c96930
c599809
015f3aa
4302077
8ef78ee
ca51709
ddc706a
311aed3
0e2d2e5
cf95fca
914d323
c246120
b0f08c4
4776637
be76d69
ec5c390
cccf3e0
a433061
751ac55
7eddd20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@trigger.dev/build": minor | ||
| --- | ||
|
|
||
| Introduced a new Python extension to enhance the build process. It now allows users to execute Python scripts with improved support and error handling. |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,172 @@ | ||||||||||||
| import fs from "node:fs"; | ||||||||||||
| import assert from "node:assert"; | ||||||||||||
| import { execa } from "execa"; | ||||||||||||
| import { additionalFiles } from "@trigger.dev/build/extensions/core"; | ||||||||||||
| import { BuildManifest } from "@trigger.dev/core/v3"; | ||||||||||||
| import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build"; | ||||||||||||
| import { logger } from "@trigger.dev/sdk/v3"; | ||||||||||||
|
|
||||||||||||
| import type { VerboseObject } from "execa"; | ||||||||||||
|
|
||||||||||||
| export type PythonOptions = { | ||||||||||||
| requirements?: string[]; | ||||||||||||
| requirementsFile?: string; | ||||||||||||
| /** | ||||||||||||
| * [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` | ||||||||||||
| */ | ||||||||||||
| pythonBinaryPath?: 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[]; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| type ExecaOptions = Parameters<typeof execa>[1]; | ||||||||||||
|
|
||||||||||||
| 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") | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| async onBuildComplete(context: BuildContext, manifest: BuildManifest) { | ||||||||||||
| await additionalFiles({ | ||||||||||||
| files: this.options.scripts ?? [], | ||||||||||||
| }).onBuildComplete!(context, manifest); | ||||||||||||
|
|
||||||||||||
| if (context.target === "dev") { | ||||||||||||
| if (this.options.pythonBinaryPath) { | ||||||||||||
| process.env.PYTHON_BIN_PATH = this.options.pythonBinaryPath; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| 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" | ||||||||||||
| `), | ||||||||||||
| }, | ||||||||||||
| deploy: { | ||||||||||||
| env: { | ||||||||||||
| PYTHON_BIN_PATH: `/opt/venv/bin/python`, | ||||||||||||
| }, | ||||||||||||
| override: true, | ||||||||||||
| }, | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| 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 | ||||||||||||
| RUN pip install --no-cache-dir -r requirements.txt | ||||||||||||
| `), | ||||||||||||
zvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
| }, | ||||||||||||
| deploy: { | ||||||||||||
| override: true, | ||||||||||||
| }, | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export const run = async (scriptArgs: string[] = [], options: ExecaOptions = {}) => { | ||||||||||||
| const pythonBin = process.env.PYTHON_BIN_PATH || "python"; | ||||||||||||
|
|
||||||||||||
| const result = await execa({ | ||||||||||||
| shell: true, | ||||||||||||
| verbose: (verboseLine: string, verboseObject: VerboseObject) => | ||||||||||||
| logger.debug(verboseObject.message, verboseObject), | ||||||||||||
| ...options, | ||||||||||||
| })(pythonBin, scriptArgs); | ||||||||||||
|
|
||||||||||||
| try { | ||||||||||||
| assert(!result.failed, `Command failed: ${result.stderr}`); | ||||||||||||
| assert(result.exitCode === 0, `Non-zero exit code: ${result.exitCode}`); | ||||||||||||
| } catch (e) { | ||||||||||||
| logger.error(e.message, result); | ||||||||||||
| throw e; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return result; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| export const runScript = ( | ||||||||||||
| scriptPath: string, | ||||||||||||
| scriptArgs: string[] = [], | ||||||||||||
| options: ExecaOptions = {} | ||||||||||||
| ) => { | ||||||||||||
| assert(scriptPath, "Script path is required"); | ||||||||||||
| assert(fs.existsSync(scriptPath), `Script does not exist: ${scriptPath}`); | ||||||||||||
|
||||||||||||
| async function findStaticAssetFiles( | |
| matchers: string[], | |
| destinationPath: string, | |
| options?: { cwd?: string; ignore?: string[] } | |
| ): Promise<FoundStaticAssetFiles> { |
Questions
- How do I check if I am in dev when I have no access to
context: BuildContext?process.env.NODE_DEV === 'development'? - Should
doesMatchPatternbe defined locally or exported fromadditionalFiles.ts? - Do I store the patterns in
this.options.scriptsin an env var as well, in order to have accessible fromrunScript?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Address the TODO comment about script pattern validation.
As noted in the TODO comment, we need to implement the fail-fast approach for script pattern validation in development.
Let's verify the current behavior and propose a solution:
🏁 Script executed:
#!/bin/bash
# Search for any existing pattern matching utilities
rg -A 5 "doesMatchPattern|findStaticAssetFiles" .Length of output: 1173
Implement the script pattern validation check in development
The search confirms that the functionality to validate script patterns (using a fail-fast approach during development) hasn’t been implemented in the Python extension. To address the TODO, consider adding a validation function similar to the following:
- Create a utility function (e.g.,
validateScriptPattern) that verifies the script’s filename (or full path) complies with the expected pattern (for example, ensuring it ends with.py). - Invoke this function immediately after the existing assertions, but only when in development mode (e.g., by checking
process.env.NODE_ENVor a similar flag). - Fail fast by throwing an error with a descriptive message if the validation fails.
Example implementation snippet:
// packages/build/src/extensions/python.ts
import * as path from 'path';
/**
* Validates that the script matches the expected Python script pattern.
* @param scriptPath The path of the script.
*/
function validateScriptPattern(scriptPath: string) {
const scriptName = path.basename(scriptPath);
// Define your expected pattern for Python scripts (customize as needed)
const pattern = /^[\w\-]+\.py$/;
if (!pattern.test(scriptName)) {
throw new Error(`Invalid script file name: ${scriptName}. Ensure it follows the expected naming convention.`);
}
}
// Existing assertions
assert(scriptPath, "Script path is required");
assert(fs.existsSync(scriptPath), `Script does not exist: ${scriptPath}`);
// In development, validate the script pattern early
if (process.env.NODE_ENV === 'development') {
validateScriptPattern(scriptPath);
}This approach ensures that any deviation from the expected script naming convention is caught early in the development phase, adhering to the fail-fast principle.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.