From 8bf8ada30e7c224c13a25cf3a5072e445e0d2037 Mon Sep 17 00:00:00 2001 From: wellenvogel Date: Mon, 29 Sep 2025 09:35:14 +0200 Subject: [PATCH] #111: allow to add extra scripts with custom_script --- extra_script.py | 13 ++++++++++++ lib/exampletask/Readme.md | 38 ++++++++++++++++++++++++++++++++++ lib/exampletask/platformio.ini | 1 + lib/exampletask/script.py | 4 ++++ 4 files changed, 56 insertions(+) create mode 100644 lib/exampletask/script.py diff --git a/extra_script.py b/extra_script.py index 218782f..3f41463 100644 --- a/extra_script.py +++ b/extra_script.py @@ -547,3 +547,16 @@ env.Append( ) #script does not run on clean yet - maybe in the future env.AddPostAction("clean",cleangenerated) +extraScripts=getFileList(getOption(env,'custom_script',toArray=True)) +for script in extraScripts: + if os.path.isfile(script): + print(f"#extra {script}") + with open(script) as fh: + try: + code = compile(fh.read(), script, 'exec') + except SyntaxError as e: + print(f"#ERROR: script {script} does not compile: {e}") + continue + exec(code) + else: + print(f"#ERROR: script {script} not found") diff --git a/lib/exampletask/Readme.md b/lib/exampletask/Readme.md index 7637ecf..7c701fb 100644 --- a/lib/exampletask/Readme.md +++ b/lib/exampletask/Readme.md @@ -57,6 +57,44 @@ Files Starting from Version 20250305 you should normally not use this file name any more as those styles would be added for all build environments. Instead define a parameter _custom_css_ in your [platformio.ini](platformio.ini) for the environments you would like to add some styles for. This parameter accepts a list of file names (relative to the project root, separated by , or as multi line entry) + * [script.py](script.py)
+ Starting from version 202509xx you can define a parameter "custom_script" in your [platformio.ini](platformio.ini). + This parameter can contain a list of file names (relative to the project root) that will be added as a [platformio extra script](https://docs.platformio.org/en/latest/scripting/index.html#scripting). The scripts will be loaded at the end of the main [extra_script](../../extra_script.py). + You can add code there that is specific for your build. + Example: + ``` + # PlatformIO extra script for obp60task + epdtype = "unknown" + pcbvers = "unknown" + for x in env["BUILD_FLAGS"]: + if x.startswith("-D HARDWARE_"): + pcbvers = x.split('_')[1] + if x.startswith("-D DISPLAY_"): + epdtype = x.split('_')[1] + + propfilename = os.path.join(env["PROJECT_LIBDEPS_DIR"], env ["PIOENV"], "GxEPD2/library.properties") + properties = {} + with open(propfilename, 'r') as file: + for line in file: + match = re.match(r'^([^=]+)=(.*)$', line) + if match: + key = match.group(1).strip() + value = match.group(2).strip() + properties[key] = value + + gxepd2vers = "unknown" + try: + if properties["name"] == "GxEPD2": + gxepd2vers = properties["version"] + except: + pass + + env["CPPDEFINES"].extend([("BOARD", env["BOARD"]), ("EPDTYPE", epdtype), ("PCBVERS", pcbvers), ("GXEPD2VERS", gxepd2vers)]) + + print("added hardware info to CPPDEFINES") + print("friendly board name is '{}'".format(env.GetProjectOption ("board_name"))) + ``` + Interfaces ---------- diff --git a/lib/exampletask/platformio.ini b/lib/exampletask/platformio.ini index 348b36c..74363a9 100644 --- a/lib/exampletask/platformio.ini +++ b/lib/exampletask/platformio.ini @@ -14,5 +14,6 @@ custom_config= lib/exampletask/exampleConfig.json custom_js=lib/exampletask/example.js custom_css=lib/exampletask/example.css +custom_script=lib/exampletask/script.py upload_port = /dev/esp32 upload_protocol = esptool \ No newline at end of file diff --git a/lib/exampletask/script.py b/lib/exampletask/script.py new file mode 100644 index 0000000..fb53d6f --- /dev/null +++ b/lib/exampletask/script.py @@ -0,0 +1,4 @@ +Import("env") + +print("exampletask extra script running") +syntax error here \ No newline at end of file