-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_www_gzip.py
91 lines (74 loc) · 3.3 KB
/
prep_www_gzip.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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' )
env.Replace(
PROJECT_DATA_DIR='data',
PROJECTDATA_DIR="$PROJECT_DATA_DIR", # legacy for dev/platform
)
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 = 'data'
#gzip_dir_path = os.path.join( data_dir_path, 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( data_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 )