|
6 | 6 | import os |
7 | 7 | import time |
8 | 8 | import traceback |
| 9 | +import zipfile |
| 10 | +import json |
| 11 | +import base64 |
9 | 12 | from queue import Queue |
10 | 13 | from uuid import uuid4 |
11 | 14 |
|
@@ -75,6 +78,17 @@ def __init__(self, host: str, port: int): |
75 | 78 | self.application = None |
76 | 79 | self.running = True |
77 | 80 |
|
| 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 | + |
78 | 92 | def state_change(self, event): |
79 | 93 | LogManager.logger.info(f"State changed to {self.state}") |
80 | 94 | if self.consumer is not None: |
@@ -120,18 +134,20 @@ def terminated_callback(name, code): |
120 | 134 | if launchers_configuration is None: |
121 | 135 | raise Exception("Launch configuration missing") |
122 | 136 |
|
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)) |
135 | 151 |
|
136 | 152 | LogManager.logger.info( |
137 | 153 | f"Launch transition started, configuration: {configuration}") |
@@ -194,7 +210,31 @@ def load_code(self, event): |
194 | 210 | self.code_loaded = False |
195 | 211 | LogManager.logger.info("Internal transition load_code executed") |
196 | 212 | 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 | + |
198 | 238 | self.code_loaded = True |
199 | 239 |
|
200 | 240 | def code_loaded(self, event): |
|
0 commit comments