STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
pio_ota_upload.py
Go to the documentation of this file.
1"""
2PlatformIO extra_script: make ``pio run -t upload`` push over Ethernet.
3
4Adapted from maxgerhardt/pio-esp32-ethernet-ota, which replaces UPLOADCMD with
5a custom command — the idea being that OTA should be the same one-button
6action as a USB upload, not a separate script you have to remember. The
7difference here is that it drives ``ota_upload.py`` (our own protocol and our
8own error messages) rather than curl.
9
10Wire it into any project by adding an OTA env to its platformio.ini:
11
12 [env:ota]
13 extends = env:adafruit_feather_esp32s3 ; your normal build env
14 upload_protocol = custom
15 extra_scripts = post:../../tools/pio_ota_upload.py ; adjust depth
16 upload_flags =
17 --ip=192.168.2.41
18
19Then:
20
21 pio run -e ota -t upload
22
23``upload_flags`` are passed straight through to ota_upload.py, so anything it
24accepts works here — e.g. ``--port=3232`` or a longer ``--timeout=90``.
25
26The path depth differs per project (the repo's projects sit at different
27levels), which is why this is spelled out per project rather than in
28platformio-common.ini — the same reason lib_extra_dirs is per-project there.
29"""
30
31import os
32import sys
33
34Import("env") # noqa: F821 - injected by PlatformIO's SCons environment
35
36try:
37 _THIS_FILE = __file__
38except NameError:
39 # SCons exec()s an extra_script with a bare globals dict, so __file__ is
40 # not defined -- `pio run` over every env dies here with NameError before
41 # it builds anything. The path still rides on the code object, which is
42 # exactly what the traceback prints, so take it from there.
43 _THIS_FILE = sys._getframe().f_code.co_filename
44
45_TOOLS_DIR = os.path.dirname(os.path.abspath(_THIS_FILE))
46_UPLOADER = os.path.join(_TOOLS_DIR, "ota_upload.py")
47
48
49def _on_upload(source, target, env):
50 """Run ota_upload.py against the just-built firmware.bin."""
51 firmware = str(source[0])
52 # upload_flags does not arrive as a plain list. PlatformIO prepends the
53 # literal string "$UPLOAD_FLAGS" onto UPLOADERFLAGS and leaves SCons to
54 # expand it when the upload *command* runs -- but we read the value
55 # ourselves instead of running a command string, so without expanding it
56 # first every flag from platformio.ini is invisible and --ip looks absent.
57 flags = [str(f) for f in env.subst_list("$UPLOADERFLAGS")[0]]
58
59 if not any(str(f).startswith("--ip") for f in flags):
60 print(
61 "[OTA] ERROR: no --ip in upload_flags. Add e.g.\n"
62 " upload_flags =\n"
63 " --ip=192.168.2.41\n"
64 " to this environment in platformio.ini."
65 )
66 env.Exit(1)
67 return
68
69 cmd = [sys.executable, _UPLOADER, "--bin", firmware] + [str(f) for f in flags]
70 print("[OTA] " + " ".join(cmd))
71 rc = env.Execute(
72 env.VerboseAction(" ".join(f'"{c}"' for c in cmd), "Uploading over Ethernet")
73 )
74 if rc:
75 env.Exit(rc)
76
77
78env.Replace(UPLOADCMD=_on_upload) # noqa: F821
_on_upload(source, target, env)