5 FIRMWARE_PACKING_ENABLED
= True
10 # https://gist.github.com/andrewwalters/d4e3539319e55fc980db1ba67254d7ed
12 def binary_compress(target_file
, source_file
):
13 """ Compress ESP8266 firmware using gzip for 'compressed OTA upload' """
15 source_file_bak
= source_file
16 if target_file
== source_file
:
17 source_file_bak
= source_file
+ ".bak"
19 if os
.path
.exists(target_file
) and os
.path
.exists(source_file_bak
):
20 """ Recompress if source file is newer than target file """
21 tgt_mtime
= os
.stat(target_file
).st_mtime
22 src_mtime
= os
.stat(source_file_bak
).st_mtime
23 if target_file
== source_file
:
24 """ bak file is older than source file """
25 do_compress
= (src_mtime
< tgt_mtime
)
27 """ target file is older than source file """
28 do_compress
= (src_mtime
> tgt_mtime
)
31 print("Compressing firmware for upload...")
32 if target_file
== source_file
:
33 shutil
.move(source_file
, source_file_bak
)
34 with
open(source_file_bak
, 'rb') as f_in
:
35 with gzip
.open(target_file
, 'wb') as f_out
:
36 shutil
.copyfileobj(f_in
, f_out
)
37 """ Set modification time on compressed file so incremental build works """
38 shutil
.copystat(source_file_bak
, target_file
)
39 """ print compression info """
40 size_orig
= os
.stat(source_file_bak
).st_size
41 size_gz
= os
.stat(target_file
).st_size
42 print("Compression reduced firmware size to {:.0f}% (was {} bytes, now {} bytes)".format(
43 (size_gz
/ size_orig
) * 100, size_orig
, size_gz
))
46 def compressFirmware(source
, target
, env
):
47 """ Compress ESP8266 firmware using gzip for 'compressed OTA upload' """
48 if FIRMWARE_PACKING_ENABLED
:
49 build_dir
= env
.subst("$BUILD_DIR")
50 image_name
= env
.subst("$PROGNAME")
51 source_file
= os
.path
.join(build_dir
, image_name
+ ".bin")
52 target_file
= source_file
+ ".gz"
53 binary_compress(target_file
, source_file
)
54 os
.remove(source_file
)
55 os
.rename(target_file
, source_file
)
58 def compress_files(source
, target
, env
):
59 #add filetypes (extensions only) to be gzipped before uploading. Everything else will be copied directly
60 filetypes_to_gzip
= ['js', 'html', 'css']
61 #filetypes_to_gzip = []
63 project_dir
= env
.get('PROJECT_DIR')
64 platform
= env
.get('PIOPLATFORM', '')
65 if platform
== 'espressif8266':
66 data_src_dir
= os
.path
.join(project_dir
,
67 "utils", "ESP8266SerialToWebsocket", "html")
69 data_src_dir
= os
.path
.join(project_dir
,
70 "src", "esp32", "html")
72 print(' ***** Packing html files *****')
74 data_dir
= env
.get('PROJECTDATA_DIR')
76 if not os
.path
.exists(data_src_dir
):
77 print(' HTML source "%s" does not found.' % data_src_dir
)
80 if os
.path
.exists(data_dir
):
81 print(' RM data dir: ' + data_dir
)
82 shutil
.rmtree(data_dir
)
84 print(' MK data dir: ' + data_dir
)
88 for extension
in filetypes_to_gzip
:
89 files_to_gzip
.extend(glob
.glob(os
.path
.join(data_src_dir
, '*.' + extension
)))
91 #print(' => files to gzip: ' + str(files_to_gzip))
93 all_files
= glob
.glob(os
.path
.join(data_src_dir
, '*.*'))
94 files_to_copy
= list(set(all_files
) - set(files_to_gzip
))
96 for f_name
in files_to_copy
:
97 print(' Copy: ' + os
.path
.basename(f_name
))
98 shutil
.copy(f_name
, data_dir
)
100 for f_name
in files_to_gzip
:
101 print(' GZip: ' + os
.path
.basename(f_name
))
102 with
open(f_name
, 'rb') as f_in
:
103 out_file
= os
.path
.join(data_dir
, os
.path
.basename(f_name
) + '.gz')
104 with gzip
.open(out_file
, 'wb') as f_out
:
105 shutil
.copyfileobj(f_in
, f_out
)
107 print(' packing done!')
109 def compress_fs_bin(source
, target
, env
):
110 build_dir
= env
.subst("$BUILD_DIR")
111 image_name
= env
.get('ESP8266_FS_IMAGE_NAME')
112 src_file
= os
.path
.join(build_dir
, image_name
+ ".bin")
113 target_file
= src_file
+ ".gz"
114 binary_compress(target_file
, src_file
)