Skip to content

Commit 31f8624

Browse files
committed
Improve support for embedding external files
1 parent d03fafa commit 31f8624

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

builder/frameworks/_embed_files.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,20 @@
1313
# limitations under the License.
1414

1515
import shutil
16-
from os import SEEK_CUR, SEEK_END, makedirs
17-
from os.path import basename, isfile, isdir, join
16+
from os import SEEK_CUR, SEEK_END
17+
from os.path import basename, isfile, join
1818

1919
from SCons.Script import Builder
2020

2121
from platformio.util import cd
2222

2323
Import("env")
2424

25+
2526
#
2627
# Embedded files helpers
2728
#
2829

29-
30-
def prepare_files(files):
31-
if not files:
32-
return
33-
34-
fixed_files = []
35-
build_dir = env.subst("$BUILD_DIR")
36-
if not isdir(build_dir):
37-
makedirs(build_dir)
38-
for f in files:
39-
fixed_file = join(build_dir, basename(f))
40-
shutil.copy(env.subst(f), fixed_file)
41-
with open(fixed_file, "rb+") as fp:
42-
fp.seek(-1, SEEK_END)
43-
if fp.read(1) != '\0':
44-
fp.seek(0, SEEK_CUR)
45-
fp.write(b'\0')
46-
47-
fixed_files.append(fixed_file)
48-
49-
return fixed_files
50-
51-
5230
def extract_files(cppdefines, files_type):
5331
for define in cppdefines:
5432
if files_type not in define:
@@ -82,11 +60,31 @@ def remove_config_define(cppdefines, files_type):
8260
return
8361

8462

85-
def embed_files(files):
63+
def prepare_file(source, target, env):
64+
filepath = source[0].get_abspath()
65+
shutil.copy(filepath, filepath + ".piobkp")
66+
67+
with open(filepath, "rb+") as fp:
68+
fp.seek(-1, SEEK_END)
69+
if fp.read(1) != '\0':
70+
fp.seek(0, SEEK_CUR)
71+
fp.write(b'\0')
72+
73+
74+
def revert_original_file(source, target, env):
75+
filepath = source[0].get_abspath()
76+
if isfile(filepath + ".piobkp"):
77+
shutil.move(filepath + ".piobkp", filepath)
78+
79+
80+
def embed_files(files, files_type):
8681
for f in files:
8782
filename = basename(f) + ".txt.o"
8883
file_target = env.TxtToBin(join("$BUILD_DIR", filename), f)
8984
env.Depends("$PIOMAINPROG", file_target)
85+
if files_type == "COMPONENT_EMBED_TXTFILES":
86+
env.AddPreAction(file_target, prepare_file)
87+
env.AddPostAction(file_target, revert_original_file)
9088
env.Append(PIOBUILDFILES=[env.File(join("$BUILD_DIR", filename))])
9189

9290

@@ -104,14 +102,10 @@ def embed_files(files):
104102
suffix=".txt.o"))
105103
)
106104

107-
108-
109105
flags = env.get("CPPDEFINES")
110-
for component_files in ("COMPONENT_EMBED_TXTFILES", "COMPONENT_EMBED_FILES"):
111-
if component_files not in env.Flatten(flags):
106+
for files_type in ("COMPONENT_EMBED_TXTFILES", "COMPONENT_EMBED_FILES"):
107+
if files_type not in env.Flatten(flags):
112108
continue
113-
files = extract_files(flags, component_files)
114-
if component_files == "COMPONENT_EMBED_TXTFILES":
115-
files = prepare_files(files)
116-
embed_files(files)
117-
remove_config_define(flags, component_files)
109+
files = extract_files(flags, files_type)
110+
embed_files(files, files_type)
111+
remove_config_define(flags, files_type)

0 commit comments

Comments
 (0)