13
13
# limitations under the License.
14
14
15
15
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
18
18
19
19
from SCons .Script import Builder
20
20
21
21
from platformio .util import cd
22
22
23
23
Import ("env" )
24
24
25
+
25
26
#
26
27
# Embedded files helpers
27
28
#
28
29
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
-
52
30
def extract_files (cppdefines , files_type ):
53
31
for define in cppdefines :
54
32
if files_type not in define :
@@ -82,11 +60,31 @@ def remove_config_define(cppdefines, files_type):
82
60
return
83
61
84
62
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 ):
86
81
for f in files :
87
82
filename = basename (f ) + ".txt.o"
88
83
file_target = env .TxtToBin (join ("$BUILD_DIR" , filename ), f )
89
84
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 )
90
88
env .Append (PIOBUILDFILES = [env .File (join ("$BUILD_DIR" , filename ))])
91
89
92
90
@@ -104,14 +102,10 @@ def embed_files(files):
104
102
suffix = ".txt.o" ))
105
103
)
106
104
107
-
108
-
109
105
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 ):
112
108
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