@@ -100,31 +100,64 @@ def _munge_to_temp(original_path, temp_file, library_version):
100
100
temp_file .write (line .encode ("utf-8" ) + b"\r \n " )
101
101
temp_file .flush ()
102
102
103
- def library (library_path , output_directory , mpy_cross = None , example_bundle = False ):
103
+ def library (library_path , output_directory , mpy_cross = None , example_bundle = False , pkg_folder_prefix = None ):
104
104
py_files = []
105
105
package_files = []
106
106
example_files = []
107
107
total_size = 512
108
108
for filename in os .listdir (library_path ):
109
109
full_path = os .path .join (library_path , filename )
110
- if os .path .isdir (full_path ) and filename not in ["docs" ]:
111
- files = os .listdir (full_path )
112
- files = filter (lambda x : x .endswith (".py" ) or x .startswith ("font5x8.bin" ), files )
110
+ if os .path .isdir (full_path ):
111
+ path_walk = [names for names in os .walk (full_path )]
112
+ #print("- '{}' walk: {}".format(filename, path_walk))
113
+
114
+ # iterate through path_walk, appending each file to
115
+ # 'walked_files' while retaining subdirectory structure
116
+ walked_files = []
117
+ for path in path_walk :
118
+ path_tail_idx = path [0 ].rfind ("/" ) + 1
119
+ path_tail = path [0 ][path_tail_idx :]
120
+ rel_path = ""
121
+ # if this entry is the package top dir, keep it
122
+ # empty so we don't double append the dir name
123
+ if filename not in path_tail :
124
+ rel_path = "{}/" .format (path_tail )
125
+
126
+ for path_files in path [2 ]:
127
+ walked_files .append ("{}{}" .format (rel_path , path_files ))
128
+ #print(" - expanded file walk: {}".format(walked_files))
129
+
130
+ files = filter (lambda x : x .endswith (".py" ) or x .startswith ("font5x8.bin" ), walked_files )
113
131
files = map (lambda x : os .path .join (filename , x ), files )
132
+
114
133
if filename .startswith ("examples" ):
115
134
example_files .extend (files )
135
+ #print("- example files: {}".format(example_files))
116
136
else :
137
+ if pkg_folder_prefix :
138
+ if (not example_bundle and
139
+ not filename .startswith (pkg_folder_prefix )):
140
+ #print("skipped path: {}".format(full_path))
141
+ continue
117
142
if not example_bundle :
118
143
package_files .extend (files )
144
+ #print("- package files: {} | {}".format(filename, package_files))
145
+
119
146
if (filename .endswith (".py" ) and
120
147
filename not in IGNORE_PY and
121
148
not example_bundle ):
122
- py_files .append (filename )
149
+ py_files .append (filename )
123
150
124
151
if len (py_files ) > 1 :
125
152
raise ValueError ("Multiple top level py files not allowed. Please put them in a package "
126
153
"or combine them into a single file." )
127
154
155
+ for fn in example_files :
156
+ base_dir = os .path .join (output_directory .replace ("/lib" , "/" ), os .path .dirname (fn ))
157
+ if not os .path .isdir (base_dir ):
158
+ os .makedirs (base_dir )
159
+ total_size += 512
160
+
128
161
for fn in package_files :
129
162
base_dir = os .path .join (output_directory , os .path .dirname (fn ))
130
163
if not os .path .isdir (base_dir ):
@@ -163,9 +196,7 @@ def library(library_path, output_directory, mpy_cross=None, example_bundle=False
163
196
full_path = os .path .join (library_path , filename )
164
197
with tempfile .NamedTemporaryFile () as temp_file :
165
198
_munge_to_temp (full_path , temp_file , library_version )
166
- if (not mpy_cross or
167
- os .stat (full_path ).st_size == 0 or
168
- filename .endswith ("__init__.py" )):
199
+ if not mpy_cross or os .stat (full_path ).st_size == 0 :
169
200
output_file = os .path .join (output_directory , filename )
170
201
shutil .copyfile (temp_file .name , output_file )
171
202
else :
0 commit comments