-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfavicon_script.py
160 lines (118 loc) · 4.99 KB
/
favicon_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
152
153
154
155
156
157
158
159
160
Import("env", "projenv")
print(env)
print(projenv)
import fileinput
from shutil import copy
from os import listdir
from os.path import isfile, join, isdir
from random import random
env_folder = "./env_data_folders/"
def_src = env_folder + "default/"
env_src = env_folder + env['PIOENV'] + "/"
dest = "./data/images/"
## files (names) in the default folder are expected in the env folder
files_to_copy = [f for f in listdir(def_src) if isfile(join(def_src, f))]
print("\nRUNNING SCRIPT\n")
def before_buildfs(source, target, env):
print("Copy files before the building littlefs:")
print("\tFiles to copy:")
for fi in files_to_copy :
print("\t\t" + fi)
if(isdir(env_src)) :
print("\nFound " + env['PIOENV'] + " folder. Will use favicons within.\n")
path = env_src
else :
print("\nNo " + env['PIOENV'] + " folder. Will use default icons from " + def_src + "\n")
path = def_src
led_name = env.GetProjectOption("led_name")
## files not in env specific folder will be copied from default
for f in files_to_copy:
src = join(path , f)
dst = join(dest , f)
if(isfile(src)):
print("\t\tCOPY favicon: " + src + "\tto " + dst)
copy(src, dst)
else:
print("\t\tCOPY from default: " + join(def_src, f) + "\tto " + dst)
copy(join(def_src, f) , dst)
fav = join(path , "favicon.ico")
if(isfile(fav)):
print("\t\tCOPY: " + fav + "\tto " + "./data/favicon.ico")
copy(fav, "./data/favicon.ico")
print("\n\tPreparing index.htm")
print("\t\tLED Name: " + led_name)
if(led_name == ""):
led_name = "LED Control"
if(isfile(env_folder + "index.htm")):
copy(env_folder + "index.htm", "./data/index.htm")
if(isfile(env_folder + "browserconfig.xml")):
copy(env_folder + "browserconfig.xml", "./data/images/browserconfig.xml")
if(isfile(env_folder + "site.webmanifest")):
copy(env_folder + "site.webmanifest", "./data/images/site.webmanifest")
with fileinput.FileInput("./data/index.htm", inplace=True) as file:
for line in file:
print(line.replace("%TITLE%", led_name), end='')
with fileinput.FileInput("./data/index.htm", inplace=True) as file:
for line in file:
print(line.replace("%RANDOM%", str(random())), end='')
with fileinput.FileInput("./data/images/site.webmanifest", inplace=True) as file:
for line in file:
print(line.replace("%TITLE%", led_name), end='')
print("\nFINISHED Script before building littlefs\n\n")
def before_build(source, target, env):
print("Preparing Build")
led_name = env.GetProjectOption("led_name")
config = env.GetProjectConfig()
build_version = config.get("common_env_data", "build_version")
## additional defines
import urllib
import subprocess
if(led_name == ""):
led_name = "LED Control"
print("\t\tLED Name: " + led_name)
led_name_url = str(led_name)
led_name_url = led_name_url.replace(" ","_")
led_name_url = urllib.parse.quote(led_name_url)
print("\t\tLED URL: " + led_name_url)
print("\n\tSetting CPPDEFINES:")
print('\t\tLED_NAME: '+led_name_url)
print('\t\tBUILD_VERSION: '+build_version)
d_version = str(subprocess.check_output(["git", "describe"]).strip())
d_version = d_version.replace("b'", "")
d_version = d_version.replace("'", "")
if(build_version != d_version):
res = subprocess.run("git tag -a -f \""+build_version+"\" -m \"version "+build_version+"\"", text=True, capture_output=True)
print("\n\t"+res.stdout)
else:
print("\n\tNot a new version: "+d_version)
projenv.Append(CPPDEFINES=[("LED_NAME", "\\\""+led_name_url+"\\\"")])
projenv.Append(CPPDEFINES=[("BUILD_VERSION", "\\\""+build_version+"\\\"")])
print("\n\tAppended the CPPDEFINES:")
for k in projenv['CPPDEFINES']:
print("\t\t{}".format(k))
print("\nFINISHED Script Before Build\n")
def after_build(source, target, env):
print("\n\tBuild done, Nothing else to do now...")
print("\nFINISHED Script After Build\n")
def after_buildfs(source, target, env):
print("\nlittlefs.bin finished, deleting copied files...")
from os import remove
if(isfile("./data/images/site.webmanifest")):
remove("./data/images/site.webmanifest")
if(isfile("./data/index.htm")):
remove("./data/index.htm")
if(isfile("./data/images/browserconfig.xml")):
remove("./data/images/browserconfig.xml")
if(isfile("./data/favicon.ico")):
remove("./data/favicon.ico")
for f in files_to_copy:
dst = join(dest , f)
if(isfile(dst)):
remove(dst)
print("\nFINISHED Script After FS Build\n")
env.AddPreAction("$BUILD_DIR/littlefs.bin", before_buildfs)
env.AddPostAction("$BUILD_DIR/littlefs.bin", after_buildfs)
#env.AddPreAction("", before_build)
env.AddPostAction("buildprog", after_build)
before_build("","",env)
print("\nFINISHED Script\n\n")