Last active
October 22, 2022 01:30
-
-
Save Askhento/eec33aedd82d823af53f5ee29674d8a0 to your computer and use it in GitHub Desktop.
gzip web content before image creation ( python script for PlatformIO )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SCRIPT TO GZIP CRITICAL FILES FOR ACCELERATED WEBSERVING | |
# see also https://community.platformio.org/t/question-esp32-compress-files-in-data-to-gzip-before-upload-possible-to-spiffs/6274/10 | |
# | |
# todo : add minify js | |
# todo : exclude files with _ at begginging | |
Import( 'env', 'projenv' ) | |
import os | |
import gzip | |
import shutil | |
import glob | |
# HELPER TO GZIP A FILE | |
def gzip_file( src_path, dst_path ): | |
with open( src_path, 'rb' ) as src, gzip.open( dst_path, 'wb' ) as dst: | |
for chunk in iter( lambda: src.read(4096), b"" ): | |
dst.write( chunk ) | |
def gzip_webfiles( source, target, env ): | |
# FILETYPES | |
filetypes_to_gzip = [ 'css', 'html', 'js', 'ico'] | |
print( '\nGZIP: INITIATED GZIP FOR SPIFFS...\n' ) | |
data_dir = env.get('PROJECT_DATA_DIR') | |
data_src_dir = os.path.join(env.get('PROJECT_DIR'), 'data_src') | |
if(os.path.exists(data_dir) and not os.path.exists(data_src_dir) ): | |
print('GZIP: "data" dir exists, "data_src" not found.') | |
print('GZIP: renaming "' + data_dir + '" to "' + data_src_dir + '"') | |
os.rename(data_dir, data_src_dir) | |
if(os.path.exists(data_dir)): | |
print('GZIP: Deleting data dir ' + data_dir) | |
shutil.rmtree(data_dir) | |
print('GZIP: Re-creating empty data dir ' + data_dir) | |
os.mkdir(data_dir) | |
# DETERMINE FILES TO COMPRESS | |
files_to_gzip = [] | |
for extension in filetypes_to_gzip: | |
files_to_gzip.extend(glob.glob(os.path.join(data_src_dir, '*.' + extension))) | |
print('GZIP: files to gzip: ' + str(files_to_gzip)) | |
all_files = glob.glob(os.path.join(data_src_dir, '*.*')) | |
files_to_copy = list(set(all_files) - set(files_to_gzip)) | |
print('GZIP: files to copy: ' + str(files_to_copy)) | |
for file in files_to_copy: | |
print('GZIP: Copying file: ' + file + ' to data dir') | |
shutil.copy(file, data_dir) | |
# COMPRESS AND MOVE FILES | |
was_error = False | |
try: | |
for source_file_path in files_to_gzip: | |
print( 'GZIP: ZIPPING... ' + source_file_path ) | |
target_file_path = os.path.join( data_dir, os.path.basename( source_file_path ) + '.gz' ) | |
# CHECK IF FILE ALREADY EXISTS | |
if os.path.exists( target_file_path ): | |
print( 'GZIP: REMOVING... ' + target_file_path ) | |
os.remove( target_file_path ) | |
# print( 'GZIP: GZIPPING FILE...\n' + source_file_path + ' TO...\n' + target_file_path + "\n\n" ) | |
print( 'GZIP: GZIPPED... ' + target_file_path + "\n" ) | |
gzip_file( source_file_path, target_file_path ) | |
except IOError as e: | |
was_error = True | |
print( 'GZIP: FAILED TO COMPRESS FILE: ' + source_file_path ) | |
# print( 'GZIP: EXCEPTION... {}'.format( e ) ) | |
if was_error: | |
print( 'GZIP: FAILURE/INCOMPLETE.\n' ) | |
else: | |
print( 'GZIP: SUCCESS/COMPRESSED.\n' ) | |
# IMPORTANT, this needs to be added to call the routine | |
env.AddPreAction( '$BUILD_DIR/spiffs.bin', gzip_webfiles ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ^^^ here some other settings ^^^ | |
extra_scripts = | |
post:src/gzip_web_content.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment