forked from matth-x/ESP32_WiFi_V4.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_script.py
151 lines (124 loc) · 5.11 KB
/
extra_script.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from os.path import join, isfile, isdir, basename
from os import listdir, system
from pprint import pprint
import hashlib
Import("env")
# Install pre-requisites
npm_installed = (0 == system("npm --version"))
#
# Dump build environment (for debug)
# print env.Dump()
#print("Current build targets", map(str, BUILD_TARGETS))
#
def get_c_name(source_file):
return basename(source_file).upper().replace('.', '_').replace('-', '_')
def text_to_header(source_file):
with open(source_file) as source_fh:
original = source_fh.read()
filename = get_c_name(source_file)
output = "static const char CONTENT_{}[] PROGMEM = ".format(filename)
for line in original.splitlines():
output += u"\n \"{}\\n\"".format(line.replace('\\', '\\\\').replace('"', '\\"'))
output += ";\n"
output += "static const char CONTENT_{}_ETAG[] PROGMEM = \"{}\";\n".format(filename, hashlib.sha256(original.encode('utf-8')).hexdigest())
return output
def binary_to_header(source_file):
filename = get_c_name(source_file)
output = "static const char CONTENT_"+filename+"[] PROGMEM = {\n "
count = 0
etag = hashlib.sha256()
with open(source_file, "rb") as source_fh:
byte = source_fh.read(1)
while byte != b"":
output += "0x{:02x}, ".format(ord(byte))
etag.update(byte)
count += 1
if 16 == count:
output += "\n "
count = 0
byte = source_fh.read(1)
output += "0x00 };\n"
output += "static const char CONTENT_{}_ETAG[] PROGMEM = \"{}\";\n".format(filename, etag.hexdigest())
return output
def data_to_header(env, target, source):
output = ""
for source_file in source:
#print("Reading {}".format(source_file))
file = source_file.get_abspath()
if file.endswith(".css") or file.endswith(".js") or file.endswith(".htm") or file.endswith(".html") or file.endswith(".svg") or file.endswith(".json"):
output += text_to_header(file)
else:
output += binary_to_header(file)
target_file = target[0].get_abspath()
print("Generating {}".format(target_file))
with open(target_file, "w") as output_file:
output_file.write(output)
def make_static(env, target, source):
output = ""
out_files = []
for file in listdir(dist_dir):
if isfile(join(dist_dir, file)):
out_files.append(file)
# Sort files to make sure the order is constant
out_files = sorted(out_files)
# include the files
for out_file in out_files:
filename = "web_server."+out_file+".h"
output += "#include \"{}\"\n".format(filename)
output += "StaticFile staticFiles[] = {\n"
for out_file in out_files:
filetype = "TEXT"
if out_file.endswith(".css"):
filetype = "CSS"
elif out_file.endswith(".js"):
filetype = "JS"
elif out_file.endswith(".htm") or out_file.endswith(".html"):
filetype = "HTML"
elif out_file.endswith(".jpg"):
filetype = "JPEG"
elif out_file.endswith(".png"):
filetype = "PNG"
elif out_file.endswith(".svg"):
filetype = "SVG"
elif out_file.endswith(".json"):
filetype = "JSON"
c_name = get_c_name(out_file)
output += " { \"/"+out_file+"\", CONTENT_"+c_name+", sizeof(CONTENT_"+c_name+") - 1, _CONTENT_TYPE_"+filetype+", CONTENT_"+c_name+"_ETAG },\n"
output += "};\n"
target_file = target[0].get_abspath()
print("Generating {}".format(target_file))
with open(target_file, "w") as output_file:
output_file.write(output)
def process_html_app(source, dest, env):
web_server_static_files = join(dest, "web_server_static_files.h")
web_server_static = join("$BUILDSRC_DIR", "web_server_static.cpp.o")
for file in sorted(listdir(source)):
if isfile(join(source, file)):
data_file = join(source, file)
header_file = join(dest, "web_server."+file+".h")
env.Command(header_file, data_file, data_to_header)
env.Depends(web_server_static_files, header_file)
env.Depends(web_server_static, env.Command(web_server_static_files, source, make_static))
#
# Generate Web app resources
#
if npm_installed:
headers_src = join(env.subst("$PROJECTSRC_DIR"), "web_static")
gui_dir = join(env.subst("$PROJECT_DIR"), "gui")
dist_dir = join(gui_dir, "dist")
node_modules = join(gui_dir, "node_modules")
# Check the GUI dir has been checked out
if(isfile(join(gui_dir, "package.json"))):
# Check to see if the Node modules have been downloaded
if(isdir(node_modules)):
if(isdir(dist_dir)):
process_html_app(dist_dir, headers_src, env)
else:
print("Warning: GUI not built, run 'cd %s; npm run build'" % gui_dir)
else:
print("Warning: GUI dependencies not found, run 'cd %s; npm install'" % gui_dir)
else:
print("Warning: GUI files not found, run 'git submodule update --init'")
else:
print("Warning: Node.JS and NPM required to update the UI")
print("PATH="+env['ENV']['PATH'])