PlatformIO Community

Script for LittleFS Upload

Hi, I recently moved to LittleFS and I can’t do my pre script for uploadfs to work. I’ve been using it with SPIFFS to Gzip web files before upload and works great.

My level of knowledge is basic so anyone cant point me how I correct this?

From what i’ve saw the script its never called

This is my platformio.ini

[env:d1_mini]

;platform = espressif8266@2.4.0

platform = espressif8266

board = d1_mini

framework = arduino

monitor_speed = 921600

board_build.filesystem = littlefs

extra_scripts = post:scripts/prepare_SPIFFS.py

lib_deps =

ArduinoJson@>=6.14.1            

ESPAsyncTCP@>=1.2.2

ESP Async WebServer@>=1.2.3

TaskScheduler@>=3.1.2

JC_Button@>=2.1.2

So what’s the content of this magic file?

# 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

#

# Ampliamente modificado  Ver original en el link citado anteriormente

Import( 'env', 'projenv' )

import os

import gzip

import shutil

import glob

# Funcion complementaria para utilizar gzip

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 )

# Comprime los archivos definidos de 'data_src/' en 'data/'

def gzip_webfiles( source, target, env ):

    

    # Tipos de archivos que necesitan ser comprimidos

    filetypes_to_gzip = [ 'css', 'html', 'js' ]

    print( '\nGZIP: Iniciando el proceso de gzipeado para la imágen SPIFFS...\n' )

    

    data_src_dir_path = os.path.join(env.get('PROJECT_DIR'), 'data_src')

    data_dir_path = env.get( 'PROJECTDATA_DIR' )

    

    # Verifica si existen data y datasrc. Si existe el primero y no el segundo lo renombra

    if(os.path.exists(data_dir_path) and not os.path.exists(data_src_dir_path) ):

        print('GZIP: El directorio "'+data_dir_path+'" existe, "'+data_src_dir_path+'" no se encuentra.')

        print('GZIP: Renombrando "' + data_dir_path + '" a "' + data_src_dir_path + '"')

        os.rename(data_dir_path, data_src_dir_path)

    # Elimina el directiorio 'data'

    if(os.path.exists(data_dir_path)):

        print('GZIP: Eliminando el directorio "data" ' + data_dir_path)

        shutil.rmtree(data_dir_path)

    # Recrea el directorio 'data' vacío

    print('GZIP: Re-creando un directorio de datos vacío ' + data_dir_path)

    os.mkdir(data_dir_path)

    # Determino los archivos a comprimir

    files_to_gzip = []

    for extension in filetypes_to_gzip:        

        files_to_gzip.extend( glob.glob( os.path.join( data_src_dir_path, '*.' + extension ) ) )

    

    all_files = glob.glob(os.path.join(data_src_dir_path, '*.*'))

    files_to_copy = list(set(all_files) - set(files_to_gzip))

    #print('MEWGZIP: archivos a copiar: ' + str(files_to_gzip))

    for file in files_to_copy:

        print('GZIP: Copiando archivo: ' + file + ' al directorio de datos')

        shutil.copy(file, data_dir_path)

    # Comprime y mueve los archivos

    #print('MEWGZIP: archivos a comprimir: ' + str(files_to_gzip))

    was_error = False

    try:

        for source_file_path in files_to_gzip:

            print( 'GZIP: Comprimiendo... ' + source_file_path )

            #base_file_path = source_file_path.replace( source_file_prefix, '' )

            base_file_path = source_file_path

            target_file_path = os.path.join( data_dir_path, os.path.basename( base_file_path ) + '.gz' )

            

            # print( 'GZIP: GZIPPING FILE...\n' + source_file_path + ' TO...\n' + target_file_path + "\n\n" )

            print( 'GZIP: Comprimido... ' + target_file_path )

            gzip_file( source_file_path, target_file_path )

    except IOError as e:

        was_error = True

        print( 'GZIP: Fallo al comprimir el archivo: ' + source_file_path )

        # print( 'GZIP: EXCEPTION... {}'.format( e ) )

    if was_error:

        print( 'GZIP: Fallo/Incomppleto.\n' )

    else:

        print( 'GZIP: Comprimido correctamente.\n' )

# IMPORTANT, this needs to be added to call the routine

env.AddPreAction( '$BUILD_DIR/spiffs.bin', gzip_webfiles )

Sorry, I’ve found the problem… the filename of the image

change spiffs.bin to littlefs.bin did the trick. The script its working now

env.AddPreAction( '$BUILD_DIR/spiffs.bin', gzip_webfiles )

to

env.AddPreAction( '$BUILD_DIR/littlefs.bin', gzip_webfiles )

If you want to be safe and indifferent towards user-defined image names or even SPIFFS/LitteFS, you should use the $ESP32_SPIFFS_IMAGE_NAME varaiable as the code shows here

So e.g.

env.AddPreAction( '$BUILD_DIR/${ESP32_SPIFFS_IMAGE_NAME}.bin', gzip_webfiles )

Hi maxgerhardt thanks for ur help.

Im using esp8266 platform not esp32… its the same case?

Edit:

I looked and found this

ESP8266_FS_IMAGE_NAME

I assume this is the correct variable name for my app case right?

Indeed per

1 Like