Skip to content

Commit b72904e

Browse files
committed
control: use zipapp to create archive
1 parent 8d8ae24 commit b72904e

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

resources/scenarios/commander.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from test_framework.test_node import TestNode
2222
from test_framework.util import PortSeed, get_rpc_proxy
2323

24-
WARNET_FILE = Path(os.path.dirname(__file__)) / "warnet.json"
24+
WARNET_FILE = Path("/warnet.json")
2525

2626
try:
2727
with open(WARNET_FILE) as file:

src/warnet/control.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import base64
2+
import io
23
import json
34
import os
45
import subprocess
56
import sys
67
import time
8+
import zipapp
79
from concurrent.futures import ThreadPoolExecutor, as_completed
810
from pathlib import Path
911

@@ -176,14 +178,13 @@ def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
176178
Pass `-- --help` to get individual scenario help
177179
"""
178180
scenario_path = Path(scenario_file).resolve()
181+
scenario_dir = scenario_path.parent
179182
scenario_name = scenario_path.stem
180183

181184
if additional_args and ("--help" in additional_args or "-h" in additional_args):
182185
return subprocess.run([sys.executable, scenario_path, "--help"])
183186

184-
with open(scenario_path, "rb") as file:
185-
scenario_data = base64.b64encode(file.read()).decode()
186-
187+
# Collect tank data for warnet.json
187188
name = f"commander-{scenario_name.replace('_', '')}-{int(time.time())}"
188189
namespace = get_default_namespace()
189190
tankpods = get_mission("tank")
@@ -200,9 +201,21 @@ def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
200201
for tank in tankpods
201202
]
202203

203-
# Encode warnet data
204+
# Encode tank data for warnet.json
204205
warnet_data = base64.b64encode(json.dumps(tanks).encode()).decode()
205206

207+
# Create in-memory buffer to store python archive instead of writing to disk
208+
archive_buffer = io.BytesIO()
209+
210+
# Compile python archive
211+
zipapp.create_archive(
212+
source=scenario_dir, target=archive_buffer, main=f"{scenario_name}:main", compressed=True
213+
)
214+
215+
# Encode the binary data as Base64
216+
archive_buffer.seek(0)
217+
archive_data = base64.b64encode(archive_buffer.read()).decode()
218+
206219
try:
207220
# Construct Helm command
208221
helm_command = [
@@ -214,8 +227,6 @@ def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
214227
"--set",
215228
f"fullnameOverride={name}",
216229
"--set",
217-
f"scenario={scenario_data}",
218-
"--set",
219230
f"warnet={warnet_data}",
220231
]
221232

0 commit comments

Comments
 (0)