python cleanup

This commit is contained in:
proddy
2025-10-25 16:03:13 +02:00
parent 342f238983
commit d98f3cc8c5

View File

@@ -2,133 +2,121 @@ import hashlib
import shutil import shutil
import re import re
import os import os
from pathlib import Path
Import("env") Import("env")
OUTPUT_DIR = "build{}".format(os.path.sep) OUTPUT_DIR = Path("build")
def bin_copy(source, target, env): def bin_copy(source, target, env):
"""Optimized firmware renaming and copying function."""
# get the application version from emsesp_version.h # Get the application version from emsesp_version.h
bag = {} version_file = Path('./src/emsesp_version.h')
exprs = [(re.compile(r'^#define EMSESP_APP_VERSION\s+"(\S+)"'), 'app_version')] if not version_file.exists():
with open('./src/emsesp_version.h', 'r') as f: print("Error: emsesp_version.h not found!")
for l in f.readlines(): return
for expr, var in exprs:
m = expr.match(l)
if m and len(m.groups()) > 0:
bag[var] = m.group(1)
app_version = bag.get('app_version') app_version = None
version_pattern = re.compile(r'^#define EMSESP_APP_VERSION\s+"(\S+)"')
# print(env.Dump()) with version_file.open('r') as f:
for line in f:
match = version_pattern.match(line)
if match:
app_version = match.group(1)
break
# get the chip type, in uppercase if not app_version:
mcu = env.get('BOARD_MCU').upper() print("Error: Could not find EMSESP_APP_VERSION in emsesp_version.h!")
# alternatively take platform from the pio target return
# platform = str(target[0]).split(os.path.sep)[2]
# work out the flash memory from the PIO env name (sloppy but works) # Get the chip type, in uppercase
# unfortunately the board_upload.flash_size is not passed down mcu = env.get('BOARD_MCU', '').upper()
flash_mem = "4MB" if not mcu:
pio_env = env.get('PIOENV').upper() print("Error: Could not determine MCU type!")
parts = pio_env.split('_') return
# if it ends with a _P skip (we use this to denote PSRAM)
if parts[-1].endswith("P"):
index = -2
else:
index = -1
# if doesn't have an M at the end # Work out the flash memory from the PIO env name
if parts[index].endswith("M"): flash_mem = "4MB" # default
flash_mem = parts[index] + "B" pio_env = env.get('PIOENV', '').upper()
if pio_env:
parts = pio_env.split('_')
# If it ends with _P skip (we use this to denote PSRAM)
index = -2 if parts[-1].endswith("P") else -1
# find if BOARD_HAS_PSRAM is in the cppdefines # If it has an M at the end, use it
cppdefines = env.get("CPPDEFINES") if parts[index].endswith("M"):
if 'BOARD_HAS_PSRAM' in cppdefines: flash_mem = parts[index] + "B"
psram = True
else:
psram = False
print("*********************************************") # Check if BOARD_HAS_PSRAM is in the cppdefines
print("EMS-ESP version: " + app_version) cppdefines = env.get("CPPDEFINES", [])
psram = 'BOARD_HAS_PSRAM' in cppdefines
# show psram as Yes or No print("=" * 90)
psram_status = "Yes" if psram else "No" print(f"EMS-ESP version: {app_version}")
print("Has PSRAM: " + psram_status) print(f"Has PSRAM: {'Yes' if psram else 'No'}")
print("MCU: "+str(mcu)) print(f"MCU: {mcu}")
print("Flash Mem: " + flash_mem) print(f"Flash Mem: {flash_mem}")
# convert . to _ so Windows doesn't complain # Convert . to _ so Windows doesn't complain
# Format is EMS-ESP-<version>-<mcu>-<flash> with + at the end if it has PSRAM # Format is EMS-ESP-<version>-<mcu>-<flash> with + at the end if it has PSRAM
variant = "EMS-ESP-" + \ variant = f"EMS-ESP-{app_version.replace('.', '_')}-{mcu}-{flash_mem}{'+' if psram else ''}"
app_version.replace(".", "_") + "-" + mcu + "-" + \
flash_mem + ("+" if psram else "")
# check if output directories exist and create if necessary # Create output directories
if not os.path.isdir(OUTPUT_DIR): firmware_dir = OUTPUT_DIR / "firmware"
os.mkdir(OUTPUT_DIR) firmware_dir.mkdir(parents=True, exist_ok=True)
for d in ['firmware']: # Define file paths
if not os.path.isdir("{}{}".format(OUTPUT_DIR, d)): bin_file = firmware_dir / f"{variant}.bin"
os.mkdir("{}{}".format(OUTPUT_DIR, d)) md5_file = firmware_dir / f"{variant}.md5"
# create string with location and file names based on variant # Remove existing files if they exist
bin_file = "{}firmware{}{}.bin".format(OUTPUT_DIR, os.path.sep, variant) for file_path in [bin_file, md5_file]:
md5_file = "{}firmware{}{}.md5".format(OUTPUT_DIR, os.path.sep, variant) if file_path.exists():
file_path.unlink()
# check if new target files exist and remove if necessary print(f"Filename: {bin_file}")
for f in [bin_file]:
if os.path.isfile(f):
os.remove(f)
# check if new target files exist and remove if necessary # Copy firmware.bin to firmware/<variant>.bin
for f in [md5_file]: shutil.copy2(str(target[0]), str(bin_file))
if os.path.isfile(f):
os.remove(f)
print("Filename: "+bin_file) # Calculate and write MD5 hash
with bin_file.open("rb") as f:
md5_hash = hashlib.md5(f.read()).hexdigest()
# copy firmware.bin to firmware/<variant>.bin print(f"MD5: {md5_hash}")
shutil.copy(str(target[0]), bin_file) md5_file.write_text(md5_hash)
with open(bin_file, "rb") as f: # Make a copy using the old 3.6.x filename format for backwards compatibility
result = hashlib.md5(f.read()) # Note: there is a chance newer E32V2s (which use the 16MB partition table and PSRAM)
print("MD5: "+result.hexdigest()) # are running a custom build of the 3.6.5 firmware as 3.6.5 was released before
file1 = open(md5_file, 'w') # production of the gateway board. Updating via the WebUI will break the system
file1.write(result.hexdigest()) # and require a manual update.
file1.close()
# make a copy using the old 3.6.x filename format for backwards compatibility with the WebUI version check, e.g. pio_env = env.get('PIOENV', '')
# create a EMS-ESP-<version>-ESP32_S3.bin if target is s3_16M_P (16MB, PSRAM) extra_variant = None
# create a EMS-ESP-<version>-ESP32.bin if target is s_4M (4MB, no PSRAM), compatible only with S32 V1 and E32 V1.0,1.4,1.5
# if pio_env == "s3_16M_P":
# Note: there is a chance newer E32V2s (which use the 16MB partition table and PSRAM) are running a custom build extra_variant = f"EMS-ESP-{app_version.replace('.', '_')}-ESP32_S3"
# of the 3.6.5 firmware as 3.6.5 was released before production of the gateway board. Updating via the WebUI will break the system and require a manual update. elif pio_env == "s_4M":
# extra_variant = f"EMS-ESP-{app_version.replace('.', '_')}-ESP32"
extra_variant = ""
if env.get('PIOENV') == "s3_16M_P":
extra_variant = "EMS-ESP-" + \
app_version.replace(".", "_") + "-ESP32_S3"
elif env.get('PIOENV') == "s_4M":
extra_variant = "EMS-ESP-" + app_version.replace(".", "_") + "-ESP32"
if extra_variant: if extra_variant:
extra_bin_file = "{}firmware{}{}.bin".format( extra_bin_file = firmware_dir / f"{extra_variant}.bin"
OUTPUT_DIR, os.path.sep, extra_variant) extra_md5_file = firmware_dir / f"{extra_variant}.md5"
if os.path.isfile(extra_bin_file):
os.remove(extra_bin_file)
extra_md5_file = "{}firmware{}{}.md5".format( # Remove existing files if they exist
OUTPUT_DIR, os.path.sep, extra_variant) for file_path in [extra_bin_file, extra_md5_file]:
if os.path.isfile(extra_md5_file): if file_path.exists():
os.remove(extra_md5_file) file_path.unlink()
shutil.copy(bin_file, extra_bin_file) # Copy files
shutil.copy(md5_file, extra_md5_file) shutil.copy2(str(bin_file), str(extra_bin_file))
print("Filename copy for 3.6.x: "+extra_bin_file) shutil.copy2(str(md5_file), str(extra_md5_file))
print(f"Filename copy for 3.6.x: {extra_bin_file}")
print("*********************************************") print("=" * 90)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", [bin_copy]) env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", [bin_copy])