-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_extras.py
92 lines (78 loc) · 2.85 KB
/
compile_extras.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
#Script used for setting config and minifying web files before loading them onto spiffs.
import shutil
import os
import rjsmin
from css_html_js_minify import process_single_html_file, process_single_css_file
config_path = "components/config/config.h"
config_present = os.path.isfile(config_path)
dir_present = os.path.isdir('components/config')
minified_data = 'data_minified'
regular_data = 'data'
if config_present:
print("Config file found, proceeding...")
else:
print("Config file not found, using config_default.h ...")
if not dir_present:
os.mkdir('components/config')
shutil.copy2('config_default.h', config_path)
else:
shutil.copy2('config_default.h', config_path)
print("Minifying data files....")
if not os.path.isdir(minified_data):
os.mkdir(minified_data)
for filename in os.listdir(minified_data):
file_path = os.path.join(minified_data, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete minified file %s. Reason: %s' % (file_path, e))
for filename in os.listdir(regular_data):
file_path = os.path.join(regular_data, filename)
minified_path = os.path.join(minified_data, filename)
if os.path.isfile(file_path):
if "favicon" in filename:
shutil.copy2(file_path, minified_path)
elif ".min" in filename:
shutil.copy2(file_path, minified_path)
else:
if ".js" in filename:
jsFile = open(file_path, "r")
data = jsFile.read()
jsFile.close()
minified = rjsmin.jsmin(data)
output = open(minified_path, "w")
output.write(minified)
output.close()
elif ".html" in filename:
process_single_html_file(file_path, output_path=minified_path)
elif ".css" in filename:
process_single_css_file(file_path, output_path=minified_path)
#Append config.h with minified files list
minified_files = [f for f in os.listdir(minified_data) if os.path.isfile(os.path.join(minified_data, f))]
a = "static const char *sites[] = {\"/\", "
for i in minified_files:
a = a + "\"/" + i + "\", "
a = a[:-2]
a+="};\n"
print("Updated config.h with: ")
print(a)
word = 'static const char *sites'
sitesLine = -1
with open(r'components/config/config.h', 'r') as fp:
lines = fp.readlines()
for line in lines:
if line.find(word) != -1:
sitesLine = lines.index(line)
if sitesLine==-1:
cfg = open(config_path, "a")
cfg.write("\n"+a)
cfg.close()
else:
cfglines = open(config_path, "r").readlines()
cfglines[sitesLine] = a
out = open(config_path, "w")
out.writelines(cfglines)
out.close()