Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2a4aa8c
add PythonExtension
zvictor Feb 9, 2025
987ab35
remove potential shell injection risk
zvictor Feb 9, 2025
8b97cec
Filter out blank lines or comment lines
zvictor Feb 9, 2025
84699e5
fix spelling
zvictor Feb 9, 2025
d00d147
add pythonExtension's `runInline`
zvictor Feb 9, 2025
500d82b
changes to requirements don’t invalidate the entire install layer
zvictor Feb 9, 2025
f0b30d6
copy script files on-demand
zvictor Feb 9, 2025
b63554e
improve PythonExtension types and logging
zvictor Feb 9, 2025
8ad7009
add changeset
zvictor Feb 9, 2025
c3cbd61
fix broken imports
zvictor Feb 10, 2025
be2cefe
Improve security of inline script execution
zvictor Feb 10, 2025
bb59bf8
Add file existence check for requirementsFile
zvictor Feb 10, 2025
65d84d0
update lock file
zvictor Feb 10, 2025
7c96930
Enhance error handling with detailed error information
zvictor Feb 10, 2025
c599809
Add portable type annotation
zvictor Feb 10, 2025
015f3aa
fix error TS18046: 'e' is of type 'unknown'
zvictor Feb 10, 2025
4302077
export the python extension
zvictor Feb 10, 2025
8ef78ee
add `pythonExtension` to the catalog
zvictor Feb 10, 2025
ca51709
fix `Cannot find module '@trigger.dev/build/extensions/core' (TS2307)
zvictor Feb 11, 2025
ddc706a
replace execa by tinyexec
zvictor Feb 11, 2025
311aed3
Merge branch 'main' into main
zvictor Feb 11, 2025
0e2d2e5
Merge branch 'main' into main
matt-aitken Feb 11, 2025
cf95fca
Update pnpm-lock.yaml
matt-aitken Feb 11, 2025
914d323
Merge branch 'main' into main
zvictor Feb 14, 2025
c246120
add custom traces instead of logging
zvictor Feb 16, 2025
b0f08c4
Merge branch 'triggerdotdev:main' into main
zvictor Feb 25, 2025
4776637
The cleanup in the finally block does not fail silently anymore
zvictor Feb 25, 2025
be76d69
move python runtime/extension to independent package
zvictor Feb 25, 2025
ec5c390
fix build package readme
zvictor Feb 25, 2025
cccf3e0
update lock file
zvictor Feb 25, 2025
a433061
add documentation to python's package
zvictor Feb 25, 2025
751ac55
add missing dependency
zvictor Feb 25, 2025
7eddd20
Update little-trains-begin.md
ericallam Feb 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move python runtime/extension to independent package
  • Loading branch information
zvictor committed Feb 25, 2025
commit be76d697e808af8375a7b52248b653a0550b458a
2 changes: 1 addition & 1 deletion .changeset/little-trains-begin.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"@trigger.dev/build": minor
"@trigger.dev/python": 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.
17 changes: 0 additions & 17 deletions packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"./extensions/prisma": "./src/extensions/prisma.ts",
"./extensions/audioWaveform": "./src/extensions/audioWaveform.ts",
"./extensions/typescript": "./src/extensions/typescript.ts",
"./extensions/python": "./src/extensions/python.ts",
"./extensions/puppeteer": "./src/extensions/puppeteer.ts"
},
"sourceDialects": [
Expand All @@ -52,9 +51,6 @@
"extensions/typescript": [
"dist/commonjs/extensions/typescript.d.ts"
],
"extensions/python": [
"dist/commonjs/extensions/python.d.ts"
],
"extensions/puppeteer": [
"dist/commonjs/extensions/puppeteer.d.ts"
]
Expand All @@ -70,9 +66,7 @@
},
"dependencies": {
"@trigger.dev/core": "workspace:3.3.16",
"@trigger.dev/sdk": "workspace:3.3.16",
"pkg-types": "^1.1.3",
"tinyexec": "^0.3.2",
"tinyglobby": "^0.2.2",
"tsconfck": "3.1.3"
},
Expand Down Expand Up @@ -156,17 +150,6 @@
"default": "./dist/commonjs/extensions/typescript.js"
}
},
"./extensions/python": {
"import": {
"@triggerdotdev/source": "./src/extensions/python.ts",
"types": "./dist/esm/extensions/python.d.ts",
"default": "./dist/esm/extensions/python.js"
},
"require": {
"types": "./dist/commonjs/extensions/python.d.ts",
"default": "./dist/commonjs/extensions/python.js"
}
},
"./extensions/puppeteer": {
"import": {
"@triggerdotdev/source": "./src/extensions/puppeteer.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @trigger.dev/python
21 changes: 21 additions & 0 deletions packages/python/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Trigger.dev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Official Python extension for Trigger.dev

View the full documentation [here](https://trigger.dev/docs)
93 changes: 93 additions & 0 deletions packages/python/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"name": "@trigger.dev/python",
"version": "3.3.16",
"description": "Python runtime and build extension for Trigger.dev",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/triggerdotdev/trigger.dev",
"directory": "packages/python"
},
"type": "module",
"files": [
"dist"
],
"tshy": {
"selfLink": false,
"main": true,
"module": true,
"project": "./tsconfig.src.json",
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts",
"./extension": "./src/extension.ts"
},
"sourceDialects": [
"@triggerdotdev/source"
]
},
"typesVersions": {
"*": {
"extension": [
"dist/commonjs/extension.d.ts"
]
}
},
"scripts": {
"clean": "rimraf dist",
"build": "tshy && pnpm run update-version",
"dev": "tshy --watch",
"typecheck": "tsc --noEmit -p tsconfig.src.json",
"update-version": "tsx ../../scripts/updateVersion.ts",
"check-exports": "attw --pack ."
},
"dependencies": {
"@trigger.dev/build": "workspace:3.3.16",
"@trigger.dev/core": "workspace:3.3.16",
"@trigger.dev/sdk": "workspace:3.3.16",
"tinyexec": "^0.3.2"
},
"devDependencies": {
"@types/node": "20.14.14",
"rimraf": "6.0.1",
"tshy": "^3.0.2",
"typescript": "^5.5.4",
"tsx": "4.17.0",
"esbuild": "^0.23.0",
"@arethetypeswrong/cli": "^0.15.4"
},
"engines": {
"node": ">=18.20.0"
},
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@triggerdotdev/source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./extension": {
"import": {
"@triggerdotdev/source": "./src/extension.ts",
"types": "./dist/esm/extension.d.ts",
"default": "./dist/esm/extension.js"
},
"require": {
"types": "./dist/commonjs/extension.d.ts",
"default": "./dist/commonjs/extension.js"
}
}
},
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import fs from "node:fs";
import assert from "node:assert";
import { additionalFiles } from "./core/additionalFiles.js";
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 { x, Options as XOptions, Result } from "tinyexec";

export type PythonOptions = {
requirements?: string[];
Expand Down Expand Up @@ -118,61 +116,4 @@ class PythonExtension implements BuildExtension {
}
}

export const run = async (
scriptArgs: string[] = [],
options: Partial<XOptions> = {}
): Promise<Result> => {
const pythonBin = process.env.PYTHON_BIN_PATH || "python";

return await logger.trace("Python call", async (span) => {
span.addEvent("Properties", {
command: `${pythonBin} ${scriptArgs.join(" ")}`,
});

const result = await x(pythonBin, scriptArgs, {
...options,
throwOnError: false, // Ensure errors are handled manually
});

span.addEvent("Output", { ...result });

if (result.exitCode !== 0) {
logger.error(result.stderr, { ...result });
throw new Error(`Python command exited with non-zero code ${result.exitCode}`);
}

return result;
});
};

export const runScript = (
scriptPath: string,
scriptArgs: string[] = [],
options: Partial<XOptions> = {}
) => {
assert(scriptPath, "Script path is required");
assert(fs.existsSync(scriptPath), `Script does not exist: ${scriptPath}`);

return run([scriptPath, ...scriptArgs], options);
};

export const runInline = async (scriptContent: string, options: Partial<XOptions> = {}) => {
assert(scriptContent, "Script content is required");

const tmpFile = `/tmp/script_${Date.now()}.py`;
await fs.promises.writeFile(tmpFile, scriptContent, { mode: 0o600 });

try {
return await runScript(tmpFile, [], options);
} finally {
try {
await fs.promises.unlink(tmpFile);
} catch (error) {
logger.warn(`Failed to clean up temporary file ${tmpFile}:`, {
error: (error as Error).stack || (error as Error).message,
});
}
}
};

export default { run, runScript, runInline };
export default pythonExtension;
63 changes: 63 additions & 0 deletions packages/python/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from "node:fs";
import assert from "node:assert";
import { logger } from "@trigger.dev/sdk/v3";
import { x, Options as XOptions, Result } from "tinyexec";

export const run = async (
scriptArgs: string[] = [],
options: Partial<XOptions> = {}
): Promise<Result> => {
const pythonBin = process.env.PYTHON_BIN_PATH || "python";

return await logger.trace("Python call", async (span) => {
span.addEvent("Properties", {
command: `${pythonBin} ${scriptArgs.join(" ")}`,
});

const result = await x(pythonBin, scriptArgs, {
...options,
throwOnError: false, // Ensure errors are handled manually
});

span.addEvent("Output", { ...result });

if (result.exitCode !== 0) {
logger.error(result.stderr, { ...result });
throw new Error(`Python command exited with non-zero code ${result.exitCode}`);
}

return result;
});
};

export const runScript = (
scriptPath: string,
scriptArgs: string[] = [],
options: Partial<XOptions> = {}
) => {
assert(scriptPath, "Script path is required");
assert(fs.existsSync(scriptPath), `Script does not exist: ${scriptPath}`);

return run([scriptPath, ...scriptArgs], options);
};

export const runInline = async (scriptContent: string, options: Partial<XOptions> = {}) => {
assert(scriptContent, "Script content is required");

const tmpFile = `/tmp/script_${Date.now()}.py`;
await fs.promises.writeFile(tmpFile, scriptContent, { mode: 0o600 });

try {
return await runScript(tmpFile, [], options);
} finally {
try {
await fs.promises.unlink(tmpFile);
} catch (error) {
logger.warn(`Failed to clean up temporary file ${tmpFile}:`, {
error: (error as Error).stack || (error as Error).message,
});
}
}
};

export default { run, runScript, runInline };
8 changes: 8 additions & 0 deletions packages/python/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../.configs/tsconfig.base.json",
"references": [
{
"path": "./tsconfig.src.json"
}
]
}
10 changes: 10 additions & 0 deletions packages/python/tsconfig.src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"include": ["./src/**/*.ts"],
"compilerOptions": {
"isolatedDeclarations": false,
"composite": true,
"sourceMap": true,
"customConditions": ["@triggerdotdev/source"]
}
}
2 changes: 1 addition & 1 deletion references/v3-catalog/trigger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ffmpeg, syncEnvVars } from "@trigger.dev/build/extensions/core";
import { puppeteer } from "@trigger.dev/build/extensions/puppeteer";
import { prismaExtension } from "@trigger.dev/build/extensions/prisma";
import { emitDecoratorMetadata } from "@trigger.dev/build/extensions/typescript";
import { pythonExtension } from "@trigger.dev/build/extensions/python";
import { pythonExtension } from "@trigger.dev/python/extension";
import { defineConfig } from "@trigger.dev/sdk/v3";

export { handleError } from "./src/handleError.js";
Expand Down