@trigger.dev/python 0.0.0-python-preview-20250226140121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Trigger.dev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # Python Extension for Trigger.dev
2
+
3
+ The Python extension enhances Trigger.dev's build process by enabling limited support for executing Python scripts within your tasks.
4
+
5
+ ## Overview
6
+
7
+ This extension introduces the <code>pythonExtension</code> build extension, which offers several key capabilities:
8
+
9
+ - **Install Python Dependencies (Except in Dev):** Automatically installs Python and specified dependencies using <code>pip</code>.
10
+ - **Requirements File Support:** You can specify dependencies in a <code>requirements.txt</code> file.
11
+ - **Inline Requirements:** Define dependencies directly within your <code>trigger.config.ts</code> file using the <code>requirements</code> option.
12
+ - **Virtual Environment:** Creates a virtual environment (<code>/opt/venv</code>) inside containers to isolate Python dependencies.
13
+ - **Helper Functions:** Provides a variety of functions for executing Python code:
14
+ - <code>run</code>: Executes Python commands with proper environment setup.
15
+ - <code>runInline</code>: Executes inline Python code directly from Node.
16
+ - <code>runScript</code>: Executes standalone <code>.py</code> script files.
17
+ - **Custom Python Path:** In development, you can configure <code>pythonBinaryPath</code> to point to a custom Python installation.
18
+
19
+ ## Usage
20
+
21
+ 1. Add the extension to your <code>trigger.config.ts</code> file:
22
+
23
+ ```typescript
24
+ import { defineConfig } from "@trigger.dev/sdk/v3";
25
+ import pythonExtension from "@trigger.dev/python/extension";
26
+
27
+ export default defineConfig({
28
+ project: "<project ref>",
29
+ build: {
30
+ extensions: [
31
+ pythonExtension({
32
+ requirementsFile: "./requirements.txt", // Optional: Path to your requirements file
33
+ pythonBinaryPath: path.join(rootDir, `.venv/bin/python`), // Optional: Custom Python binary path
34
+ scripts: ["my_script.py"], // List of Python scripts to include
35
+ }),
36
+ ],
37
+ },
38
+ });
39
+ ```
40
+
41
+ 2. (Optional) Create a <code>requirements.txt</code> file in your project root with the necessary Python dependencies.
42
+
43
+ 3. Execute Python scripts within your tasks using one of the provided functions:
44
+
45
+ ### Running a Python Script
46
+
47
+ ```typescript
48
+ import { task } from "@trigger.dev/sdk/v3";
49
+ import python from "@trigger.dev/python";
50
+
51
+ export const myScript = task({
52
+ id: "my-python-script",
53
+ run: async () => {
54
+ const result = await python.runScript("my_script.py", ["hello", "world"]);
55
+ return result.stdout;
56
+ },
57
+ });
58
+ ```
59
+
60
+ ### Running Inline Python Code
61
+
62
+ ```typescript
63
+ import { task } from "@trigger.dev/sdk/v3";
64
+ import python from "@trigger.dev/python";
65
+
66
+ export const myTask = task({
67
+ id: "to_datetime-task",
68
+ run: async () => {
69
+ const result = await python.runInline(`
70
+ import pandas as pd
71
+
72
+ pandas.to_datetime("${+new Date() / 1000}")
73
+ `);
74
+ return result.stdout;
75
+ },
76
+ });
77
+ ```
78
+
79
+ ### Running Lower-Level Commands
80
+
81
+ ```typescript
82
+ import { task } from "@trigger.dev/sdk/v3";
83
+ import python from "@trigger.dev/python";
84
+
85
+ export const pythonVersionTask = task({
86
+ id: "python-version-task",
87
+ run: async () => {
88
+ const result = await python.run(["--version"]);
89
+ return result.stdout; // Expected output: Python 3.12.8
90
+ },
91
+ });
92
+ ```
93
+
94
+ ## Limitations
95
+
96
+ - This is a **partial implementation** and does not provide full Python support as an execution runtime for tasks.
97
+ - Only basic Python script execution is supported; scripts are not automatically copied to staging/production containers.
98
+ - Manual intervention may be required for installing and configuring binary dependencies in development environments.
99
+
100
+ ## Additional Information
101
+
102
+ For more detailed documentation, visit the official docs at [Trigger.dev Documentation](https://trigger.dev/docs).
@@ -0,0 +1,24 @@
1
+ import { BuildExtension } from "@trigger.dev/core/v3/build";
2
+ export type PythonOptions = {
3
+ requirements?: string[];
4
+ requirementsFile?: string;
5
+ /**
6
+ * [Dev-only] The path to the python binary.
7
+ *
8
+ * @remarks
9
+ * This option is typically used during local development or in specific testing environments
10
+ * where a particular Python installation needs to be targeted. It should point to the full path of the python executable.
11
+ *
12
+ * Example: `/usr/bin/python3` or `C:\\Python39\\python.exe`
13
+ */
14
+ pythonBinaryPath?: string;
15
+ /**
16
+ * An array of glob patterns that specify which Python scripts are allowed to be executed.
17
+ *
18
+ * @remarks
19
+ * These scripts will be copied to the container during the build process.
20
+ */
21
+ scripts?: string[];
22
+ };
23
+ export declare function pythonExtension(options?: PythonOptions): BuildExtension;
24
+ export default pythonExtension;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.pythonExtension = pythonExtension;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_assert_1 = __importDefault(require("node:assert"));
9
+ const core_1 = require("@trigger.dev/build/extensions/core");
10
+ const splitAndCleanComments = (str) => str
11
+ .split("\n")
12
+ .map((line) => line.trim())
13
+ .filter((line) => line && !line.startsWith("#"));
14
+ function pythonExtension(options = {}) {
15
+ return new PythonExtension(options);
16
+ }
17
+ class PythonExtension {
18
+ options;
19
+ name = "PythonExtension";
20
+ constructor(options = {}) {
21
+ this.options = options;
22
+ (0, node_assert_1.default)(!(this.options.requirements && this.options.requirementsFile), "Cannot specify both requirements and requirementsFile");
23
+ if (this.options.requirementsFile) {
24
+ (0, node_assert_1.default)(node_fs_1.default.existsSync(this.options.requirementsFile), `Requirements file not found: ${this.options.requirementsFile}`);
25
+ this.options.requirements = splitAndCleanComments(node_fs_1.default.readFileSync(this.options.requirementsFile, "utf-8"));
26
+ }
27
+ }
28
+ async onBuildComplete(context, manifest) {
29
+ await (0, core_1.additionalFiles)({
30
+ files: this.options.scripts ?? [],
31
+ }).onBuildComplete(context, manifest);
32
+ if (context.target === "dev") {
33
+ if (this.options.pythonBinaryPath) {
34
+ process.env.PYTHON_BIN_PATH = this.options.pythonBinaryPath;
35
+ }
36
+ return;
37
+ }
38
+ context.logger.debug(`Adding ${this.name} to the build`);
39
+ context.addLayer({
40
+ id: "python-installation",
41
+ image: {
42
+ instructions: splitAndCleanComments(`
43
+ # Install Python
44
+ RUN apt-get update && apt-get install -y --no-install-recommends \
45
+ python3 python3-pip python3-venv && \
46
+ apt-get clean && rm -rf /var/lib/apt/lists/*
47
+
48
+ # Set up Python environment
49
+ RUN python3 -m venv /opt/venv
50
+ ENV PATH="/opt/venv/bin:$PATH"
51
+ `),
52
+ },
53
+ deploy: {
54
+ env: {
55
+ PYTHON_BIN_PATH: `/opt/venv/bin/python`,
56
+ },
57
+ override: true,
58
+ },
59
+ });
60
+ context.addLayer({
61
+ id: "python-dependencies",
62
+ build: {
63
+ env: {
64
+ REQUIREMENTS_CONTENT: this.options.requirements?.join("\n") || "",
65
+ },
66
+ },
67
+ image: {
68
+ instructions: splitAndCleanComments(`
69
+ ARG REQUIREMENTS_CONTENT
70
+ RUN echo "$REQUIREMENTS_CONTENT" > requirements.txt
71
+
72
+ # Install dependencies
73
+ RUN pip install --no-cache-dir -r requirements.txt
74
+ `),
75
+ },
76
+ deploy: {
77
+ override: true,
78
+ },
79
+ });
80
+ }
81
+ }
82
+ exports.default = pythonExtension;
83
+ //# sourceMappingURL=extension.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension.js","sourceRoot":"","sources":["../../src/extension.ts"],"names":[],"mappings":";;;;;AAkCA,0CAEC;AApCD,sDAAyB;AACzB,8DAAiC;AACjC,6DAAqE;AA0BrE,MAAM,qBAAqB,GAAG,CAAC,GAAW,EAAE,EAAE,CAC5C,GAAG;KACA,KAAK,CAAC,IAAI,CAAC;KACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;KAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;AAErD,SAAgB,eAAe,CAAC,UAAyB,EAAE;IACzD,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,eAAe;IAGC;IAFJ,IAAI,GAAG,iBAAiB,CAAC;IAEzC,YAAoB,UAAyB,EAAE;QAA3B,YAAO,GAAP,OAAO,CAAoB;QAC7C,IAAA,qBAAM,EACJ,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAC7D,uDAAuD,CACxD,CAAC;QAEF,IAAI,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAAC;YAClC,IAAA,qBAAM,EACJ,iBAAE,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAC5C,gCAAgC,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAChE,CAAC;YACF,IAAI,CAAC,OAA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ACF,CAAC,CAAC;IACL,CAAC;CACF;AAED,kBAAe,eAAe,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { Options as XOptions, Result } from "tinyexec";
2
+ export declare const run: (scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
3
+ export declare const runScript: (scriptPath: string, scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
4
+ export declare const runInline: (scriptContent: string, options?: Partial<XOptions>) => Promise<import("tinyexec").Output>;
5
+ declare const _default: {
6
+ run: (scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
7
+ runScript: (scriptPath: string, scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
8
+ runInline: (scriptContent: string, options?: Partial<XOptions>) => Promise<import("tinyexec").Output>;
9
+ };
10
+ export default _default;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.runInline = exports.runScript = exports.run = void 0;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_assert_1 = __importDefault(require("node:assert"));
9
+ const v3_1 = require("@trigger.dev/sdk/v3");
10
+ const tinyexec_1 = require("tinyexec");
11
+ const run = async (scriptArgs = [], options = {}) => {
12
+ const pythonBin = process.env.PYTHON_BIN_PATH || "python";
13
+ return await v3_1.logger.trace("Python call", async (span) => {
14
+ span.addEvent("Properties", {
15
+ command: `${pythonBin} ${scriptArgs.join(" ")}`,
16
+ });
17
+ const result = await (0, tinyexec_1.x)(pythonBin, scriptArgs, {
18
+ ...options,
19
+ throwOnError: false, // Ensure errors are handled manually
20
+ });
21
+ span.addEvent("Output", { ...result });
22
+ if (result.exitCode !== 0) {
23
+ v3_1.logger.error(result.stderr, { ...result });
24
+ throw new Error(`Python command exited with non-zero code ${result.exitCode}`);
25
+ }
26
+ return result;
27
+ });
28
+ };
29
+ exports.run = run;
30
+ const runScript = (scriptPath, scriptArgs = [], options = {}) => {
31
+ (0, node_assert_1.default)(scriptPath, "Script path is required");
32
+ (0, node_assert_1.default)(node_fs_1.default.existsSync(scriptPath), `Script does not exist: ${scriptPath}`);
33
+ return (0, exports.run)([scriptPath, ...scriptArgs], options);
34
+ };
35
+ exports.runScript = runScript;
36
+ const runInline = async (scriptContent, options = {}) => {
37
+ (0, node_assert_1.default)(scriptContent, "Script content is required");
38
+ const tmpFile = `/tmp/script_${Date.now()}.py`;
39
+ await node_fs_1.default.promises.writeFile(tmpFile, scriptContent, { mode: 0o600 });
40
+ try {
41
+ return await (0, exports.runScript)(tmpFile, [], options);
42
+ }
43
+ finally {
44
+ try {
45
+ await node_fs_1.default.promises.unlink(tmpFile);
46
+ }
47
+ catch (error) {
48
+ v3_1.logger.warn(`Failed to clean up temporary file ${tmpFile}:`, {
49
+ error: error.stack || error.message,
50
+ });
51
+ }
52
+ }
53
+ };
54
+ exports.runInline = runInline;
55
+ exports.default = { run: exports.run, runScript: exports.runScript, runInline: exports.runInline };
56
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAAyB;AACzB,8DAAiC;AACjC,4CAA6C;AAC7C,uCAA0D;AAEnD,MAAM,GAAG,GAAG,KAAK,EACtB,aAAuB,EAAE,EACzB,UAA6B,EAAE,EACd,EAAE;IACnB,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,QAAQ,CAAC;IAE1D,OAAO,MAAM,WAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QACtD,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE;YAC1B,OAAO,EAAE,GAAG,SAAS,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;SAChD,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,MAAM,I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I,CAAC;QACH,OAAO,MAAM,IAAA,iBAAS,EAAC,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,MAAM,iBAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACpC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,WAAM,CAAC,IAAI,CAAC,qCAAqC,OAAO,GAAG,EAAE;gBAC3D,KAAK,EAAG,KAAe,CAAC,KAAK,IAAK,KAAe,CAAC,OAAO;aAC1D,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAjBW,QAAA,SAAS,aAiBpB;AAEF,kBAAe,EAAE,GAAG,EAAH,WAAG,EAAE,SAAS,EAAT,iBAAS,EAAE,SAAS,EAAT,iBAAS,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,24 @@
1
+ import { BuildExtension } from "@trigger.dev/core/v3/build";
2
+ export type PythonOptions = {
3
+ requirements?: string[];
4
+ requirementsFile?: string;
5
+ /**
6
+ * [Dev-only] The path to the python binary.
7
+ *
8
+ * @remarks
9
+ * This option is typically used during local development or in specific testing environments
10
+ * where a particular Python installation needs to be targeted. It should point to the full path of the python executable.
11
+ *
12
+ * Example: `/usr/bin/python3` or `C:\\Python39\\python.exe`
13
+ */
14
+ pythonBinaryPath?: string;
15
+ /**
16
+ * An array of glob patterns that specify which Python scripts are allowed to be executed.
17
+ *
18
+ * @remarks
19
+ * These scripts will be copied to the container during the build process.
20
+ */
21
+ scripts?: string[];
22
+ };
23
+ export declare function pythonExtension(options?: PythonOptions): BuildExtension;
24
+ export default pythonExtension;
@@ -0,0 +1,77 @@
1
+ import fs from "node:fs";
2
+ import assert from "node:assert";
3
+ import { additionalFiles } from "@trigger.dev/build/extensions/core";
4
+ const splitAndCleanComments = (str) => str
5
+ .split("\n")
6
+ .map((line) => line.trim())
7
+ .filter((line) => line && !line.startsWith("#"));
8
+ export function pythonExtension(options = {}) {
9
+ return new PythonExtension(options);
10
+ }
11
+ class PythonExtension {
12
+ options;
13
+ name = "PythonExtension";
14
+ constructor(options = {}) {
15
+ this.options = options;
16
+ assert(!(this.options.requirements && this.options.requirementsFile), "Cannot specify both requirements and requirementsFile");
17
+ if (this.options.requirementsFile) {
18
+ assert(fs.existsSync(this.options.requirementsFile), `Requirements file not found: ${this.options.requirementsFile}`);
19
+ this.options.requirements = splitAndCleanComments(fs.readFileSync(this.options.requirementsFile, "utf-8"));
20
+ }
21
+ }
22
+ async onBuildComplete(context, manifest) {
23
+ await additionalFiles({
24
+ files: this.options.scripts ?? [],
25
+ }).onBuildComplete(context, manifest);
26
+ if (context.target === "dev") {
27
+ if (this.options.pythonBinaryPath) {
28
+ process.env.PYTHON_BIN_PATH = this.options.pythonBinaryPath;
29
+ }
30
+ return;
31
+ }
32
+ context.logger.debug(`Adding ${this.name} to the build`);
33
+ context.addLayer({
34
+ id: "python-installation",
35
+ image: {
36
+ instructions: splitAndCleanComments(`
37
+ # Install Python
38
+ RUN apt-get update && apt-get install -y --no-install-recommends \
39
+ python3 python3-pip python3-venv && \
40
+ apt-get clean && rm -rf /var/lib/apt/lists/*
41
+
42
+ # Set up Python environment
43
+ RUN python3 -m venv /opt/venv
44
+ ENV PATH="/opt/venv/bin:$PATH"
45
+ `),
46
+ },
47
+ deploy: {
48
+ env: {
49
+ PYTHON_BIN_PATH: `/opt/venv/bin/python`,
50
+ },
51
+ override: true,
52
+ },
53
+ });
54
+ context.addLayer({
55
+ id: "python-dependencies",
56
+ build: {
57
+ env: {
58
+ REQUIREMENTS_CONTENT: this.options.requirements?.join("\n") || "",
59
+ },
60
+ },
61
+ image: {
62
+ instructions: splitAndCleanComments(`
63
+ ARG REQUIREMENTS_CONTENT
64
+ RUN echo "$REQUIREMENTS_CONTENT" > requirements.txt
65
+
66
+ # Install dependencies
67
+ RUN pip install --no-cache-dir -r requirements.txt
68
+ `),
69
+ },
70
+ deploy: {
71
+ override: true,
72
+ },
73
+ });
74
+ }
75
+ }
76
+ export default pythonExtension;
77
+ //# sourceMappingURL=extension.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension.js","sourceRoot":"","sources":["../../src/extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AA0BrE,MAAM,qBAAqB,GAAG,CAAC,GAAW,EAAE,EAAE,CAC5C,GAAG;KACA,KAAK,CAAC,IAAI,CAAC;KACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;KAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;AAErD,MAAM,UAAU,eAAe,CAAC,UAAyB,EAAE;IACzD,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,eAAe;IAGC;IAFJ,IAAI,GAAG,iBAAiB,CAAC;IAEzC,YAAoB,UAAyB,EAAE;QAA3B,YAAO,GAAP,OAAO,CAAoB;QAC7C,MAAM,CACJ,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAC7D,uDAAuD,CACxD,CAAC;QAEF,IAAI,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAAC;YAClC,MAAM,CACJ,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAC5C,gCAAgC,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAChE,CAAC;YACF,IAAI,CAAC,OAAO,CAAC,YAAY,GAAG,qBAAqB,CAC/C,EAAE,CAAC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
@@ -0,0 +1,10 @@
1
+ import { Options as XOptions, Result } from "tinyexec";
2
+ export declare const run: (scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
3
+ export declare const runScript: (scriptPath: string, scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
4
+ export declare const runInline: (scriptContent: string, options?: Partial<XOptions>) => Promise<import("tinyexec").Output>;
5
+ declare const _default: {
6
+ run: (scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
7
+ runScript: (scriptPath: string, scriptArgs?: string[], options?: Partial<XOptions>) => Promise<Result>;
8
+ runInline: (scriptContent: string, options?: Partial<XOptions>) => Promise<import("tinyexec").Output>;
9
+ };
10
+ export default _default;
@@ -0,0 +1,47 @@
1
+ import fs from "node:fs";
2
+ import assert from "node:assert";
3
+ import { logger } from "@trigger.dev/sdk/v3";
4
+ import { x } from "tinyexec";
5
+ export const run = async (scriptArgs = [], options = {}) => {
6
+ const pythonBin = process.env.PYTHON_BIN_PATH || "python";
7
+ return await logger.trace("Python call", async (span) => {
8
+ span.addEvent("Properties", {
9
+ command: `${pythonBin} ${scriptArgs.join(" ")}`,
10
+ });
11
+ const result = await x(pythonBin, scriptArgs, {
12
+ ...options,
13
+ throwOnError: false, // Ensure errors are handled manually
14
+ });
15
+ span.addEvent("Output", { ...result });
16
+ if (result.exitCode !== 0) {
17
+ logger.error(result.stderr, { ...result });
18
+ throw new Error(`Python command exited with non-zero code ${result.exitCode}`);
19
+ }
20
+ return result;
21
+ });
22
+ };
23
+ export const runScript = (scriptPath, scriptArgs = [], options = {}) => {
24
+ assert(scriptPath, "Script path is required");
25
+ assert(fs.existsSync(scriptPath), `Script does not exist: ${scriptPath}`);
26
+ return run([scriptPath, ...scriptArgs], options);
27
+ };
28
+ export const runInline = async (scriptContent, options = {}) => {
29
+ assert(scriptContent, "Script content is required");
30
+ const tmpFile = `/tmp/script_${Date.now()}.py`;
31
+ await fs.promises.writeFile(tmpFile, scriptContent, { mode: 0o600 });
32
+ try {
33
+ return await runScript(tmpFile, [], options);
34
+ }
35
+ finally {
36
+ try {
37
+ await fs.promises.unlink(tmpFile);
38
+ }
39
+ catch (error) {
40
+ logger.warn(`Failed to clean up temporary file ${tmpFile}:`, {
41
+ error: error.stack || error.message,
42
+ });
43
+ }
44
+ }
45
+ };
46
+ export default { run, runScript, runInline };
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,CAAC,EAA+B,MAAM,UAAU,CAAC;AAE1D,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,EACtB,aAAuB,EAAE,EACzB,UAA6B,EAAE,EACd,EAAE;IACnB,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,QAAQ,CAAC;IAE1D,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QACtD,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE;YAC1B,OAAO,EAAE,GAAG,SAAS,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;SAChD,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,SAAS,EAAE,UAAU,EAAE;YAC5C,GAAG,OAAO;YACV,YAAY,EAAE,KAAK,EAAE,qCAAqC;SAC3D,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;QAEvC,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC,EAAE,CAAC;YAC1B,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,GAAG,MAA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H,CAAC;AACH,CAAC,CAAC;AAEF,eAAe,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,93 @@
1
+ {
2
+ "name": "@trigger.dev/python",
3
+ "version": "0.0.0-python-preview-20250226140121",
4
+ "description": "Python runtime and build extension for Trigger.dev",
5
+ "license": "MIT",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/triggerdotdev/trigger.dev",
12
+ "directory": "packages/python"
13
+ },
14
+ "type": "module",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "tshy": {
19
+ "selfLink": false,
20
+ "main": true,
21
+ "module": true,
22
+ "project": "./tsconfig.src.json",
23
+ "exports": {
24
+ "./package.json": "./package.json",
25
+ ".": "./src/index.ts",
26
+ "./extension": "./src/extension.ts"
27
+ },
28
+ "sourceDialects": [
29
+ "@triggerdotdev/source"
30
+ ]
31
+ },
32
+ "typesVersions": {
33
+ "*": {
34
+ "extension": [
35
+ "dist/commonjs/extension.d.ts"
36
+ ]
37
+ }
38
+ },
39
+ "dependencies": {
40
+ "@trigger.dev/build": "0.0.0-python-preview-20250226140121",
41
+ "@trigger.dev/core": "0.0.0-python-preview-20250226140121",
42
+ "@trigger.dev/sdk": "0.0.0-python-preview-20250226140121",
43
+ "tinyexec": "^0.3.2"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "20.14.14",
47
+ "rimraf": "6.0.1",
48
+ "tshy": "^3.0.2",
49
+ "typescript": "^5.5.4",
50
+ "tsx": "4.17.0",
51
+ "esbuild": "^0.23.0",
52
+ "@arethetypeswrong/cli": "^0.15.4"
53
+ },
54
+ "engines": {
55
+ "node": ">=18.20.0"
56
+ },
57
+ "exports": {
58
+ "./package.json": "./package.json",
59
+ ".": {
60
+ "import": {
61
+ "@triggerdotdev/source": "./src/index.ts",
62
+ "types": "./dist/esm/index.d.ts",
63
+ "default": "./dist/esm/index.js"
64
+ },
65
+ "require": {
66
+ "types": "./dist/commonjs/index.d.ts",
67
+ "default": "./dist/commonjs/index.js"
68
+ }
69
+ },
70
+ "./extension": {
71
+ "import": {
72
+ "@triggerdotdev/source": "./src/extension.ts",
73
+ "types": "./dist/esm/extension.d.ts",
74
+ "default": "./dist/esm/extension.js"
75
+ },
76
+ "require": {
77
+ "types": "./dist/commonjs/extension.d.ts",
78
+ "default": "./dist/commonjs/extension.js"
79
+ }
80
+ }
81
+ },
82
+ "main": "./dist/commonjs/index.js",
83
+ "types": "./dist/commonjs/index.d.ts",
84
+ "module": "./dist/esm/index.js",
85
+ "scripts": {
86
+ "clean": "rimraf dist",
87
+ "build": "tshy && pnpm run update-version",
88
+ "dev": "tshy --watch",
89
+ "typecheck": "tsc --noEmit -p tsconfig.src.json",
90
+ "update-version": "tsx ../../scripts/updateVersion.ts",
91
+ "check-exports": "attw --pack ."
92
+ }
93
+ }