-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (52 loc) · 1.93 KB
/
Program.cs
File metadata and controls
63 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Filesystem example — input/output directories and temp output.
//
// Mirrors: src/wasm_sandbox/examples/python_filesystem_demo.rs
using HyperlightSandbox.Api;
using HyperlightSandbox.Guest.Python;
Console.WriteLine("=== Hyperlight Sandbox .NET — Filesystem Example ===\n");
// --- Test 1: Temp output ---
Console.WriteLine("═══ Test 1: Temp output directory ═══");
using (var sandbox = new SandboxBuilder()
.WithPythonModule()
.WithTempOutput()
.Build())
{
sandbox.Run("""
with open("/output/hello.txt", "w") as f:
f.write("Hello from the sandbox!")
print("Wrote hello.txt")
""");
var files = sandbox.GetOutputFiles();
Console.WriteLine($" Output files: [{string.Join(", ", files)}]");
Console.WriteLine($" Output path: {sandbox.OutputPath}");
}
// --- Test 2: Input directory ---
Console.WriteLine("\n═══ Test 2: Input directory ═══");
// Create a temp input directory with a test file.
var inputDir = Path.Combine(Path.GetTempPath(), $"hyperlight-input-{Guid.NewGuid():N}");
Directory.CreateDirectory(inputDir);
File.WriteAllText(Path.Combine(inputDir, "data.txt"), "Input data from host");
try
{
using var sandbox = new SandboxBuilder()
.WithPythonModule()
.WithInputDir(inputDir)
.WithTempOutput()
.Build();
var result = sandbox.Run("""
with open("/input/data.txt", "r") as f:
content = f.read()
print(f"Read from input: {content}")
with open("/output/processed.txt", "w") as f:
f.write(f"Processed: {content.upper()}")
print("Wrote processed.txt to output")
""");
Console.WriteLine($" stdout: {result.Stdout.Trim()}");
Console.WriteLine($" Output files: [{string.Join(", ", sandbox.GetOutputFiles())}]");
}
finally
{
Directory.Delete(inputDir, recursive: true);
}
Console.WriteLine("\n✅ Filesystem example finished successfully!");
return 0;