-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressed_ota.py
27 lines (21 loc) · 1.03 KB
/
compressed_ota.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import gzip
import shutil
import os
Import("env")
def compressFirmware(source, target, env):
""" Compress ESP8266 firmware using gzip for 'compressed OTA upload' """
SOURCE_FILE = env.subst("$BUILD_DIR") + os.sep + env.subst("$PROGNAME") + ".bin"
COMPRESSED_FILE = SOURCE_FILE + '.gz'
BACKUP_FILE = SOURCE_FILE + '.bak'
# Kopiere die Originaldatei als Backup
shutil.copy(SOURCE_FILE, BACKUP_FILE)
if os.path.exists(SOURCE_FILE):
print("Compressing firmware for upload...")
with open(SOURCE_FILE, 'rb') as f_in:
with gzip.open(COMPRESSED_FILE, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
if os.path.exists(COMPRESSED_FILE):
ORG_FIRMWARE_SIZE = os.stat(SOURCE_FILE).st_size
GZ_FIRMWARE_SIZE = os.stat(COMPRESSED_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.AddPreAction("upload", compressFirmware)