Files

69 lines
2.5 KiB
Python

"""Packaged Espressif CLI: no system Python or first-use package install required."""
import os
import sys
ESPTOOL_VERSION = "5.6.0"
def load_esptool():
# Import only AFTER the owned entry point adopts its TTY: esp-pylib caches
# the port enumerator at import time. Never initialize its optional IDE hook.
os.environ["NO_COLOR"] = "1"
import esptool
if esptool.__version__ != ESPTOOL_VERSION:
raise RuntimeError("Packaged esptool version differs from the reviewed pin")
esptool.log.set_console_options(no_color=True, force_terminal=False)
return esptool
def run_esptool(args):
"""Supported CLI embedding; preserve nonzero failures and plain piped logs."""
esptool = load_esptool()
import serial
try:
esptool.main(args)
except (esptool.FatalError, serial.SerialException, StopIteration, KeyboardInterrupt) as error:
print(f"Espressif operation failed: {error}", file=sys.stderr)
raise SystemExit(2) from error
def check_stub():
# Debian's stripped package omitted this blob; validate the actual bundled
# resources before accepting a flash job, without opening a serial port.
esptool = load_esptool()
from esptool.loader import StubFlasher
import serial
from esptool.targets.esp32s3 import ESP32S3ROM
# v5 selects its resource from a target object, not the v4 chip-name string.
# A closed pySerial object lets the real target constructor run without I/O.
target = ESP32S3ROM(serial.Serial(port=None))
stub = StubFlasher(target)
if not stub.text or not stub.text_start or not stub.entry:
raise RuntimeError('ESP32-S3 flashing stub is incomplete')
return esptool.__version__
def self_test():
print(f"archy-esptool {check_stub()}: ESP32-S3 stub ready")
if __name__ == '__main__':
if sys.argv[1:] == ['--archy-uf2-self-test']:
import json
import radio_uf2
radio_uf2.validate_board_info(b'Board-ID: WisBlock-RAK4631-Board\nSoftDevice: S140 6.1.1\n')
print(json.dumps({'contract': 1, 'board': 'rak4631', 'format': 'uf2'}))
sys.exit(0)
if sys.argv[1:2] in (['--archy-uf2-owned'], ['--archy-uf2']):
from radio_uf2 import main
main(sys.argv[1:])
sys.exit(0)
if sys.argv[1:2] == ['--archy-owned']:
from archy_owned_serial import install
install(sys.argv[2])
sys.argv = [sys.argv[0], *sys.argv[3:]]
if sys.argv[1:] == ['--archy-self-test']:
self_test()
else:
run_esptool(sys.argv[1:])