|
1 | 1 | """ |
2 | | -core/exporters/library_packer.py |
3 | | --------------------------------- |
4 | | -Gestor de empaquetado de librerías para EduBot Pro. |
5 | | -Convierte código fuente suelto en librerías .ZIP estándares |
6 | | -compatibles con el Gestor de Librerías de Arduino IDE. |
| 2 | +Gestor de empaquetado de librerías para MiniML Engine. |
| 3 | +Genera estructuras duales compatibles con PlatformIO (library.json) |
| 4 | +y Arduino IDE (library.properties), resolviendo problemas de linkeo. |
7 | 5 | """ |
8 | 6 |
|
9 | 7 | import os |
|
13 | 11 | class LibraryPackager: |
14 | 12 |
|
15 | 13 | @staticmethod |
16 | | - def create_arduino_zip(model_name, cpp_code, version="1.0.0", quantized=False, force=False): |
| 14 | + def create_arduino_zip(model_name, cpp_code, version="1.0.0", quantized=False, force=False, license_type="Apache-2.0"): |
17 | 15 | """ |
18 | | - Crea ZIP Arduino. Si quantized=True, añade flag "Pro" en descripción. |
| 16 | + Crea ZIP estructurado para PlatformIO y Arduino. |
19 | 17 | """ |
20 | 18 | safe_name = "".join(c for c in model_name if c.isalnum() or c == '_') |
21 | 19 | lib_name = f"MiniML_{safe_name}" |
22 | 20 | zip_filename = f"{lib_name}_v{version}.zip" |
| 21 | + |
23 | 22 | export_path = os.path.join("exports", zip_filename) |
24 | 23 | os.makedirs("exports", exist_ok=True) |
| 24 | + |
25 | 25 | if os.path.exists(export_path) and not force: |
26 | 26 | return {"success": False, "requires_confirm": True, "error": f"La librería '{zip_filename}' ya existe. ¿Deseas reemplazarla?"} |
27 | 27 |
|
28 | 28 | desc_extra = " (INT8 Quantized)" if quantized else "" |
| 29 | + year = datetime.now().year |
29 | 30 |
|
30 | 31 | try: |
31 | 32 | with zipfile.ZipFile(export_path, 'w', zipfile.ZIP_DEFLATED) as zipf: |
32 | | - zipf.writestr(f"{lib_name}/src/{lib_name}.h", cpp_code) |
33 | 33 |
|
| 34 | + # Código Fuente (Estructura Dual) |
| 35 | + # Guardamos en src/ para que ambos entornos lo detecten |
| 36 | + header_path = f"{lib_name}/src/{lib_name}.h" |
| 37 | + zipf.writestr(header_path, cpp_code) |
| 38 | + |
| 39 | + # Archivo .cpp auxiliar para evitar "Multiple Definitions" si se incluye en varios archivos |
| 40 | + cpp_stub = f"""#include "{lib_name}.h"\n// El motor MiniML opera de forma estática (Header-Only).\n""" |
| 41 | + zipf.writestr(f"{lib_name}/src/{lib_name}.cpp", cpp_stub) |
| 42 | + |
| 43 | + # Manifiesto para Arduino IDE |
34 | 44 | props = ( |
35 | 45 | f"name={lib_name}\n" |
36 | 46 | f"version={version}\n" |
37 | | - f"author=MiniML Engine\n" |
38 | | - f"maintainer=User\n" |
39 | | - f"sentence=TinyML Model{desc_extra} generated by MiniML Engine.\n" |
40 | | - f"paragraph=Optimized inference engine for Arduino.\n" |
| 47 | + f"author=MiniML Engine Architect\n" |
| 48 | + f"maintainer=MiniML Developer\n" |
| 49 | + f"sentence=Edge AI Model{desc_extra} generated by MiniML Engine.\n" |
| 50 | + f"paragraph=Optimized C++ inference engine for microcontrollers.\n" |
41 | 51 | f"category=Data Processing\n" |
42 | | - f"url=https://github.com/Shuuida/MiniML-Engine\n" |
| 52 | + f"url=https://github.com/MiniML-Engine\n" |
43 | 53 | f"architectures=*\n" |
| 54 | + f"depends=\n" |
44 | 55 | ) |
45 | 56 | zipf.writestr(f"{lib_name}/library.properties", props) |
46 | 57 |
|
47 | | - # Ejemplo .ino simple |
48 | | - example = f"""#include "{lib_name}.h"\n{safe_name} model;\nvoid setup() {{ Serial.begin(9600); }}\nvoid loop() {{}}""" |
49 | | - zipf.writestr(f"{lib_name}/examples/Inference/Inference.ino", example) |
| 58 | + # Manifiesto para PlatformIO (library.json) |
| 59 | + pio_json = f"""{{ |
| 60 | + "name": "{lib_name}", |
| 61 | + "version": "{version}", |
| 62 | + "description": "Edge AI Model{desc_extra} generated by MiniML Engine", |
| 63 | + "keywords": ["machine-learning", "tinyml", "ai", "miniml"], |
| 64 | + "repository": {{ |
| 65 | + "type": "git", |
| 66 | + "url": "https://github.com/MiniML-Engine" |
| 67 | + }}, |
| 68 | + "authors": [ |
| 69 | + {{ |
| 70 | + "name": "MiniML Engine Architect" |
| 71 | + }} |
| 72 | + ], |
| 73 | + "license": "{license_type}", |
| 74 | + "frameworks": ["arduino", "espidf", "mbed"], |
| 75 | + "platforms": ["atmelavr", "espressif32", "ststm32", "nordicnrf52"], |
| 76 | + "build": {{ |
| 77 | + "flags": ["-O3"] |
| 78 | + }} |
| 79 | +}}""" |
| 80 | + zipf.writestr(f"{lib_name}/library.json", pio_json) |
| 81 | + |
| 82 | + # Inyección de Licencia Open Source (Apache 2.0 por defecto) |
| 83 | + license_text = f"""Copyright {year} MiniML Engine Developer |
| 84 | +
|
| 85 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 86 | +you may not use this file except in compliance with the License. |
| 87 | +You may obtain a copy of the License at |
| 88 | +
|
| 89 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 90 | +
|
| 91 | +Unless required by applicable law or agreed to in writing, software |
| 92 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 93 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 94 | +See the License for the specific language governing permissions and |
| 95 | +limitations under the License. |
| 96 | +""" |
| 97 | + zipf.writestr(f"{lib_name}/LICENSE", license_text) |
| 98 | + |
| 99 | + # Ejemplo de Integración |
| 100 | + example = f"""/* |
| 101 | + * MiniML Engine - Inferencia Edge |
| 102 | + * Modelo: {safe_name} |
| 103 | + */ |
| 104 | +#include <Arduino.h> |
| 105 | +#include "{lib_name}.h" |
| 106 | +
|
| 107 | +void setup() {{ |
| 108 | + Serial.begin(115200); |
| 109 | + Serial.println("MiniML Model Initialized"); |
| 110 | +}} |
| 111 | +
|
| 112 | +void loop() {{ |
| 113 | + // float input_data[] = {{ ... }}; |
| 114 | + // float prediction = predict(input_data); |
| 115 | + delay(1000); |
| 116 | +}} |
| 117 | +""" |
| 118 | + zipf.writestr(f"{lib_name}/examples/BasicInference/BasicInference.ino", example) |
50 | 119 |
|
51 | 120 | return {"success": True, "path": export_path} |
52 | 121 |
|
|
0 commit comments