Tasmota/pio-tools/gzip-firmware.py

35 lines
1.3 KiB
Python
Raw Normal View History

2020-08-17 18:45:59 +01:00
Import('env')
import os
import shutil
import gzip
2021-01-18 18:21:51 +00:00
platform = env.PioPlatform()
board = env.BoardConfig()
mcu = board.get("build.mcu", "esp32")
# gzip only for ESP8266
if env["PIOPLATFORM"] != "espressif32":
2020-08-17 18:45:59 +01:00
2021-01-18 18:21:51 +00:00
OUTPUT_DIR = "build_output{}".format(os.path.sep)
2020-08-17 18:45:59 +01:00
2021-01-18 18:21:51 +00:00
def bin_gzip(source, target, env):
variant = str(target[0]).split(os.path.sep)[2]
2020-08-17 18:45:59 +01:00
2021-01-18 18:21:51 +00:00
# create string with location and file names based on variant
bin_file = "{}firmware{}{}.bin".format(OUTPUT_DIR, os.path.sep, variant)
gzip_file = "{}firmware{}{}.bin.gz".format(OUTPUT_DIR, os.path.sep, variant)
2020-11-12 12:26:14 +00:00
2021-01-18 18:21:51 +00:00
# check if new target files exist and remove if necessary
if os.path.isfile(gzip_file): os.remove(gzip_file)
2020-08-17 18:45:59 +01:00
2021-01-18 18:21:51 +00:00
# write gzip firmware file
with open(bin_file,"rb") as fp:
with gzip.open(gzip_file, "wb", compresslevel = 9) as f:
shutil.copyfileobj(fp, f)
ORG_FIRMWARE_SIZE = os.stat(bin_file).st_size
GZ_FIRMWARE_SIZE = os.stat(gzip_file).st_size
print("Compression reduced firmware size by {:.0f}% (was {} bytes, now {} bytes)".format((GZ_FIRMWARE_SIZE / ORG_FIRMWARE_SIZE) * 100, ORG_FIRMWARE_SIZE, GZ_FIRMWARE_SIZE))
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", [bin_gzip])