Skip to content

Commit 3c9b4fa

Browse files
committed
Allow multi file user applications and launch files
1 parent ddef762 commit 3c9b4fa

2 files changed

Lines changed: 57 additions & 20 deletions

File tree

manager/libs/applications/compatibility/robotics_application_wrapper.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, update_callback):
2727
time.sleep(5)
2828
self.start_console()
2929
self.user_process = None
30+
self.entrypoint_path = None
3031

3132
def _create_process(self, cmd):
3233
#print("creando procesos")
@@ -40,15 +41,11 @@ def terminate(self):
4041
stop_process_and_children(self.user_process)
4142
self.user_process = None
4243

43-
def load_code(self, code: str):
44-
self.f = open("user_code.py", "w")
45-
self.f.write(code)
46-
self.f.close()
47-
#self.update_callback("Hello World")
44+
def load_code(self, path: str):
45+
self.entrypoint_path = path
4846

4947
def run(self):
50-
self.user_process = self._create_process(f"DISPLAY=:2 python {self.f.name}")
51-
48+
self.user_process = self._create_process(f"DISPLAY=:2 python {self.entrypoint_path}")
5249
self.running = True
5350

5451
def stop(self):

manager/manager/manager.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import os
77
import time
88
import traceback
9+
import zipfile
10+
import json
11+
import base64
912
from queue import Queue
1013
from uuid import uuid4
1114

@@ -75,6 +78,17 @@ def __init__(self, host: str, port: int):
7578
self.application = None
7679
self.running = True
7780

81+
# Creates workspace directories
82+
worlds_dir = "/workspace/worlds"
83+
code_dir = "/workspace/code"
84+
binaries_dir = "/workspace/binaries"
85+
if not os.path.isdir(worlds_dir):
86+
os.makedirs(worlds_dir)
87+
if not os.path.isdir(code_dir):
88+
os.makedirs(code_dir)
89+
if not os.path.isdir(binaries_dir):
90+
os.makedirs(binaries_dir)
91+
7892
def state_change(self, event):
7993
LogManager.logger.info(f"State changed to {self.state}")
8094
if self.consumer is not None:
@@ -120,18 +134,20 @@ def terminated_callback(name, code):
120134
if launchers_configuration is None:
121135
raise Exception("Launch configuration missing")
122136

123-
# check if launch file is sent
124-
launch_file = configuration.get('launch_file', None)
125-
if (launch_file is not None):
126-
directory = "workspace/worlds/"
127-
file_path = directory + launch_file.get('name', "world.launch")
128-
# if folder does not exist, create it
129-
if not os.path.exists(directory):
130-
os.makedirs(directory)
131-
# store the file
132-
file = open(file_path, 'w')
133-
file.write(launch_file.get('contents', None))
134-
file.close()
137+
# check if launch files are sent
138+
launch_files = configuration.get('launch_files', None)
139+
if (launch_files is not None):
140+
try:
141+
# Convert base64 to binary
142+
binary_content = base64.b64decode(launch_files)
143+
# Save the binary content as a file
144+
with open('workspace/binaries/user_worlds.zip', 'wb') as file:
145+
file.write(binary_content)
146+
# Unzip the file
147+
with zipfile.ZipFile('workspace/binaries/user_worlds.zip', 'r') as zip_ref:
148+
zip_ref.extractall('workspace/worlds/')
149+
except Exception as e:
150+
print("An error occurred while opening zip_path as r:" + str(e))
135151

136152
LogManager.logger.info(
137153
f"Launch transition started, configuration: {configuration}")
@@ -194,7 +210,31 @@ def load_code(self, event):
194210
self.code_loaded = False
195211
LogManager.logger.info("Internal transition load_code executed")
196212
message_data = event.kwargs.get('data', {})
197-
self.application.load_code(message_data['code'])
213+
214+
# Code is sent raw
215+
message_code = message_data.get('code', None)
216+
if message_code is not None:
217+
self.application.load_code(message_code)
218+
219+
# Code is sent zipped
220+
message_zip = message_data.get('zip', None)
221+
if message_zip is not None:
222+
try:
223+
# Convert base64 to binary
224+
binary_content = base64.b64decode(message_zip)
225+
# Save the binary content as a file
226+
with open('workspace/binaries/user_app.zip', 'wb') as file:
227+
file.write(binary_content)
228+
# Unzip the file
229+
with zipfile.ZipFile('workspace/binaries/user_app.zip', 'r') as zip_ref:
230+
zip_ref.extractall('workspace/code/')
231+
entrypoint_path = message_data.get('entrypoint', None)
232+
if (entrypoint_path is not None):
233+
entrypoint_path = "/workspace/code/" + entrypoint_path
234+
self.application.load_code(entrypoint_path)
235+
except Exception as e:
236+
file.write("An error occurred while opening zip_path as r:" + str(e))
237+
198238
self.code_loaded = True
199239

200240
def code_loaded(self, event):

0 commit comments

Comments
 (0)