|
1 | | -# Copyright 2025 Arm Limited and/or its affiliates. |
| 1 | +# Copyright 2025-2026 Arm Limited and/or its affiliates. |
2 | 2 | # |
3 | 3 | # This source code is licensed under the BSD-style license found in the |
4 | 4 | # LICENSE file in the root directory of this source tree. |
|
14 | 14 |
|
15 | 15 | import logging |
16 | 16 | import os # nosec B404 - used alongside subprocess for tool invocation |
| 17 | +import shutil |
17 | 18 | import subprocess # nosec B404 - required to drive external converter CLI |
18 | 19 | import tempfile |
19 | 20 | from typing import final, List |
@@ -143,30 +144,33 @@ def vgf_compile( |
143 | 144 | with open(tosa_path, "wb") as f: |
144 | 145 | f.write(tosa_flatbuffer) |
145 | 146 |
|
146 | | - additional_flags = " ".join(compile_flags) |
| 147 | + compile_flags = [f for f in compile_flags if f and f.strip()] |
147 | 148 | converter_binary = require_model_converter_binary() |
148 | 149 | vgf_path = tosa_path + ".vgf" |
149 | | - conversion_command = ( |
150 | | - f"{converter_binary} {additional_flags} -i {tosa_path} -o {vgf_path}" |
151 | | - ) |
| 150 | + conversion_command = [ |
| 151 | + converter_binary, |
| 152 | + *compile_flags, |
| 153 | + "-i", |
| 154 | + tosa_path, |
| 155 | + "-o", |
| 156 | + vgf_path, |
| 157 | + ] |
152 | 158 | try: |
153 | 159 | subprocess.run( # nosec B602 - shell invocation constrained to trusted converter binary |
154 | | - [conversion_command], shell=True, check=True, capture_output=True |
| 160 | + conversion_command, shell=True, check=True, capture_output=True |
155 | 161 | ) |
156 | 162 | except subprocess.CalledProcessError as process_error: |
| 163 | + conversion_command_str = " ".join(conversion_command) |
157 | 164 | raise RuntimeError( |
158 | | - f"Vgf compiler ('{conversion_command}') failed with error:\n \ |
| 165 | + f"Vgf compiler ('{conversion_command_str}') failed with error:\n \ |
159 | 166 | {process_error.stderr.decode()}\n \ |
160 | 167 | Stdout:\n{process_error.stdout.decode()}" |
161 | 168 | ) |
162 | 169 |
|
163 | 170 | if artifact_path: |
164 | 171 | logger.info(f"Emitting debug output to: {vgf_path=}") |
165 | 172 | os.makedirs(artifact_path, exist_ok=True) |
166 | | - cp = f"cp {vgf_path} {artifact_path}" |
167 | | - subprocess.run( # nosec B602 - shell copy of trusted artifact for debugging |
168 | | - cp, shell=True, check=True, capture_output=False |
169 | | - ) |
| 173 | + shutil.copy2(vgf_path, artifact_path) |
170 | 174 |
|
171 | 175 | vgf_bytes = open(vgf_path, "rb").read() |
172 | 176 | return vgf_bytes |
0 commit comments