|
| 1 | +""" |
| 2 | +PlatformIO post-build script: archive firmware.elf files. |
| 3 | +
|
| 4 | +Copies firmware.elf to elf_archive/ with a timestamp after each build. |
| 5 | +Keeps only the last 10 files to avoid filling up disk space. |
| 6 | +
|
| 7 | +Usage in platformio.ini |
| 8 | +----------------------- |
| 9 | + extra_scripts = post:archive_elf.py |
| 10 | +""" |
| 11 | + |
| 12 | +Import("env") |
| 13 | + |
| 14 | +import os |
| 15 | +import shutil |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +MAX_ARCHIVES = 10 |
| 19 | +ARCHIVE_DIR = os.path.join(env.subst("$PROJECT_DIR"), "elf_archive") |
| 20 | + |
| 21 | + |
| 22 | +def archive_elf(source, target, env): |
| 23 | + elf_path = os.path.join(env.subst("$BUILD_DIR"), "firmware.elf") |
| 24 | + if not os.path.isfile(elf_path): |
| 25 | + print("[archive_elf] firmware.elf not found, skipping.") |
| 26 | + return |
| 27 | + |
| 28 | + os.makedirs(ARCHIVE_DIR, exist_ok=True) |
| 29 | + |
| 30 | + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 31 | + dest = os.path.join(ARCHIVE_DIR, f"firmware_{timestamp}.elf") |
| 32 | + shutil.copy2(elf_path, dest) |
| 33 | + print(f"[archive_elf] Saved {dest}") |
| 34 | + |
| 35 | + # Keep only the last MAX_ARCHIVES files |
| 36 | + files = sorted( |
| 37 | + [f for f in os.listdir(ARCHIVE_DIR) if f.endswith(".elf")], |
| 38 | + ) |
| 39 | + while len(files) > MAX_ARCHIVES: |
| 40 | + old = os.path.join(ARCHIVE_DIR, files.pop(0)) |
| 41 | + os.remove(old) |
| 42 | + print(f"[archive_elf] Removed old archive {old}") |
| 43 | + |
| 44 | + |
| 45 | +env.AddPostAction("$BUILD_DIR/firmware.elf", archive_elf) |
0 commit comments