Thank you for this helpful script. It gave me a good headstart on doing the exact same thing. But I did not want to delete and create data folders. I just wanted all the files which need gzip-ing to be found in a dedicated gzip
-directory of my SPIFFS.
Since the static webserver directory should not deliver these files I added an underscore to those files the webserver should not find/deliver directly. I added handler-methods in the code to deliver the gzipped files separately (not via static file server).
platform.ini
I found one thing very important to know, you could run into issues that PlatformIO build task does not find the extrascript, even though it is in the src
-directory. I fixed that by adding following entry to the ini-file:
extra_scripts =
post:src/LMBuildExtensionSPIFFS.py
This makes the Python file reachable. Otherwise you might see a weird warning which sounds like this and totally does not give a clue what happened:
warning: Calling missing SConscript without error is deprecated.
Transition by adding must_exist=0 to SConscript calls.
Missing SConscript 'LMBuildExtensionSPIFFS.py'
File "/Users/blazr/.platformio/penv/lib/python3.8/site-packages/platformio/builder/main.py", line 164, in <module>
So for everyone hitting this roadblock, this is just a not found file, add the correct relative path from your root project directory and it works.
LMBuildExtensionSPIFFS.py
I changed your code a bit. I exchanged especially the gzip-part because I had issues with it actually working. This worked for me.
Find the changed file here:
# 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
#
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 )
# GZIP DEFINED FILES FROM 'data' DIR to 'data/gzip/' DIR
def gzip_webfiles( source, target, env ):
# FILETYPES / SUFFIXES WHICh NEED TO BE GZIPPED
source_file_prefix = '_'
filetypes_to_gzip = [ 'css', 'html' ]
print( '\nGZIP: INITIATED GZIP FOR SPIFFS...\n' )
GZIP_DIR_NAME = 'gzip'
data_dir_path = env.get( 'PROJECTDATA_DIR' )
gzip_dir_path = os.path.join( env.get( 'PROJECTDATA_DIR' ), GZIP_DIR_NAME )
# CHECK DATA DIR
if not os.path.exists( data_dir_path ):
print( 'GZIP: DATA DIRECTORY MISSING AT PATH: ' + data_dir_path )
print( 'GZIP: PLEASE CREATE THE DIRECTORY FIRST (ABORTING)' )
print( 'GZIP: FAILURE / ABORTED' )
return
# CHECK GZIP DIR
if not os.path.exists( gzip_dir_path ):
print( 'GZIP: GZIP DIRECTORY MISSING AT PATH: ' + gzip_dir_path )
print( 'GZIP: TRYING TO CREATE IT...' )
try:
os.mkdir( gzip_dir_path )
except Exception as e:
print( 'GZIP: FAILED TO CREATE DIRECTORY: ' + gzip_dir_path )
# print( 'GZIP: EXCEPTION... ' + str( e ) )
print( 'GZIP: PLEASE CREATE THE DIRECTORY FIRST (ABORTING)' )
print( 'GZIP: FAILURE / ABORTED' )
return
# DETERMINE FILES TO COMPRESS
files_to_gzip = []
for extension in filetypes_to_gzip:
match_str = source_file_prefix + '*.'
files_to_gzip.extend( glob.glob( os.path.join( data_dir_path, match_str + extension ) ) )
# print( 'GZIP: GZIPPING FILES... {}\n'.format( files_to_gzip ) )
# COMPRESS AND MOVE FILES
was_error = False
try:
for source_file_path in files_to_gzip:
print( 'GZIP: ZIPPING... ' + source_file_path )
base_file_path = source_file_path.replace( source_file_prefix, '' )
target_file_path = os.path.join( gzip_dir_path, os.path.basename( base_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 will give you detailed feedback on the console while building. I tested my script against my build environment BUILD_ESP32
like this from the PlatformIO CLI Core Console:
platformio run -e BUILD_ESP32 --target buildfs
Thanks again for your work.
