@freestyle-sh/with-python 0.2.1 → 0.2.3
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/dist/index.d.ts +3 -0
- package/dist/index.js +17 -7
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,9 @@ declare class PythonRuntimeInstance extends VmWithInstance implements VmRunCodeI
|
|
|
11
11
|
constructor(builder: VmPython);
|
|
12
12
|
runCode<Result extends JSONValue = any>(args: string | {
|
|
13
13
|
code: string;
|
|
14
|
+
argv?: string[];
|
|
15
|
+
env?: Record<string, string>;
|
|
16
|
+
workdir?: string;
|
|
14
17
|
}): Promise<RunCodeResponse<Result>>;
|
|
15
18
|
}
|
|
16
19
|
|
package/dist/index.js
CHANGED
|
@@ -23,17 +23,27 @@ class PythonRuntimeInstance extends VmWithInstance {
|
|
|
23
23
|
this.builder = builder;
|
|
24
24
|
}
|
|
25
25
|
async runCode(args) {
|
|
26
|
-
const
|
|
26
|
+
const options = typeof args === "string" ? { code: args } : args;
|
|
27
|
+
const { code, argv, env, workdir } = options;
|
|
28
|
+
const shellEscape = (value) => `'${value.replace(/'/g, "'\\''")}'`;
|
|
29
|
+
const argvArgs = argv?.map(shellEscape).join(" ");
|
|
30
|
+
const envPrefix = env ? `${Object.entries(env).map(([key, value]) => `${key}=${shellEscape(value)}`).join(" ")} ` : "";
|
|
31
|
+
const cdPrefix = workdir ? `cd ${shellEscape(workdir)} && ` : "";
|
|
32
|
+
const command = `${cdPrefix}${envPrefix}python3 -c "${code.replace(/"/g, '\\"')}"${argvArgs ? ` -- ${argvArgs}` : ""}`;
|
|
27
33
|
const result = await this.vm.exec({
|
|
28
|
-
command
|
|
34
|
+
command
|
|
29
35
|
});
|
|
30
36
|
let parsedResult = void 0;
|
|
31
37
|
if (result.stdout) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
const lines = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
39
|
+
const lastLine = lines[lines.length - 1];
|
|
40
|
+
if (lastLine) {
|
|
41
|
+
try {
|
|
42
|
+
parsedResult = JSON.parse(lastLine);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
if (result.stderr) {
|
|
45
|
+
`Failed to parse JSON output. Stderr: ${result.stderr}`;
|
|
46
|
+
}
|
|
37
47
|
}
|
|
38
48
|
}
|
|
39
49
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@freestyle-sh/with-python",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"freestyle-sandboxes": "^0.1.
|
|
7
|
-
"@freestyle-sh/with-type-run-code": "^0.2.
|
|
6
|
+
"freestyle-sandboxes": "^0.1.13",
|
|
7
|
+
"@freestyle-sh/with-type-run-code": "^0.2.3"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "./dist/index.js",
|