@freestyle-sh/with-python 0.2.0 → 0.2.2

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 CHANGED
@@ -1,16 +1,19 @@
1
- import { VmWith, VmWithInstance, CreateVmOptions } from 'freestyle-sandboxes';
1
+ import { VmWith, VmWithInstance, VmSpec } from 'freestyle-sandboxes';
2
2
  import { VmRunCodeInstance, JSONValue, RunCodeResponse, VmRunCode } from '@freestyle-sh/with-type-run-code';
3
3
 
4
4
  declare class VmPython extends VmWith<PythonRuntimeInstance> implements VmRunCode<VmRunCodeInstance> {
5
5
  constructor();
6
- configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
6
+ configureSnapshotSpec(spec: VmSpec): VmSpec;
7
7
  createInstance(): PythonRuntimeInstance;
8
8
  }
9
9
  declare class PythonRuntimeInstance extends VmWithInstance implements VmRunCodeInstance {
10
10
  builder: VmPython;
11
11
  constructor(builder: VmPython);
12
- runCode<Result extends JSONValue = any>({ code, }: {
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
@@ -1,16 +1,16 @@
1
- import { VmWith, VmTemplate, VmWithInstance } from 'freestyle-sandboxes';
1
+ import { VmWith, VmSpec, VmWithInstance } from 'freestyle-sandboxes';
2
2
 
3
3
  class VmPython extends VmWith {
4
4
  constructor() {
5
5
  super();
6
6
  }
7
- configure(existingConfig) {
8
- const pythonConfig = {
9
- template: new VmTemplate({
7
+ configureSnapshotSpec(spec) {
8
+ return this.composeSpecs(
9
+ spec,
10
+ new VmSpec({
10
11
  aptDeps: ["python3"]
11
12
  })
12
- };
13
- return this.compose(existingConfig, pythonConfig);
13
+ );
14
14
  }
15
15
  createInstance() {
16
16
  return new PythonRuntimeInstance(this);
@@ -22,19 +22,28 @@ class PythonRuntimeInstance extends VmWithInstance {
22
22
  super();
23
23
  this.builder = builder;
24
24
  }
25
- async runCode({
26
- code
27
- }) {
25
+ async runCode(args) {
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}` : ""}`;
28
33
  const result = await this.vm.exec({
29
- command: `python3 -c "${code.replace(/"/g, '\\"')}"`
34
+ command
30
35
  });
31
36
  let parsedResult = void 0;
32
37
  if (result.stdout) {
33
- try {
34
- parsedResult = JSON.parse(result.stdout);
35
- } catch (e) {
36
- if (result.stderr) {
37
- `Failed to parse JSON output. Stderr: ${result.stderr}`;
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
+ }
38
47
  }
39
48
  }
40
49
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-python",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "private": false,
5
5
  "dependencies": {
6
- "freestyle-sandboxes": "^0.1.2",
7
- "@freestyle-sh/with-type-run-code": "^0.2.0"
6
+ "freestyle-sandboxes": "^0.1.8",
7
+ "@freestyle-sh/with-type-run-code": "^0.2.2"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",