2424
2525import os
2626import os .path
27+ import pathlib
2728import semver
2829import shutil
2930import sys
3031import subprocess
3132import tempfile
3233
3334IGNORE_PY = ["setup.py" , "conf.py" , "__init__.py" ]
35+ GLOB_PATTERNS = ["*.py" , "font5x8.bin" ]
3436
3537def version_string (path = None , * , valid_semver = False ):
3638 version = None
@@ -106,71 +108,41 @@ def library(library_path, output_directory, package_folder_prefix,
106108 package_files = []
107109 example_files = []
108110 total_size = 512
109- for filename in os .listdir (library_path ):
110- full_path = os .path .join (library_path , filename )
111- if os .path .isdir (full_path ):
112- path_walk = [names for names in os .walk (full_path )]
113- #print("- '{}' walk: {}".format(filename, path_walk))
114-
115- # iterate through path_walk, appending each file to
116- # 'walked_files' while retaining subdirectory structure
117- walked_files = []
118- for path in path_walk :
119- rel_path = ""
120- if filename .startswith ("examples" ):
121- path_tail_idx = path [0 ].rfind ("examples/" )
122- if path_tail_idx != - 1 :
123- rel_path = "{}/" .format (path [0 ][path_tail_idx + 9 :])
124111
112+ lib_path = pathlib .Path (library_path )
113+ parent_idx = len (lib_path .parts )
114+ glob_search = []
115+ for pattern in GLOB_PATTERNS :
116+ glob_search .extend (list (lib_path .rglob (pattern )))
117+
118+ for file in glob_search :
119+ #print(file_tree, ":", parent_idx)
120+ if file .parts [parent_idx ] == "examples" :
121+ example_files .append (file )
122+ else :
123+ if not example_bundle :
124+ if file .parts [parent_idx ].startswith (package_folder_prefix ):
125+ package_files .append (file )
125126 else :
126- path_tail_idx = (path [0 ].rfind ("{}/" .format (filename ))
127- + (len (filename ) + 1 ))
128- path_tail = path [0 ][path_tail_idx :]
129-
130- # if this entry is the package top dir, keep it
131- # empty so we don't double append the dir name
132- if filename not in path_tail :
133- rel_path = "{}/" .format (path_tail )
134-
135- for path_files in path [2 ]:
136- walked_files .append ("{}{}" .format (rel_path , path_files ))
137- #print(" - expanded file walk: {}".format(walked_files))
138-
139- files = filter (
140- lambda x : x .endswith (".py" ) or x .startswith ("font5x8.bin" ),
141- walked_files
142- )
143- files = map (lambda x : os .path .join (filename , x ), files )
144-
145- if filename .startswith ("examples" ):
146- example_files .extend (files )
147- #print("- example files: {}".format(example_files))
148- else :
149- if (not example_bundle and
150- not filename .startswith (package_folder_prefix )):
151- print ("skipped path: {}" .format (full_path ))
127+ if file .name in IGNORE_PY :
128+ #print("Ignoring:", file.resolve())
152129 continue
153- if not example_bundle :
154- package_files .extend (files )
155- #print("- package files: {} | {}".format(filename, package_files))
156-
157- if (filename .endswith (".py" ) and
158- filename not in IGNORE_PY and
159- not example_bundle ):
160- py_files .append (filename )
130+ if file .parent == lib_path :
131+ py_files .append (file )
161132
162133 if len (py_files ) > 1 :
163134 raise ValueError ("Multiple top level py files not allowed. Please put them in a package "
164135 "or combine them into a single file." )
165136
166137 for fn in example_files :
167- base_dir = os .path .join (output_directory .replace ("/lib" , "/" ), os .path .dirname (fn ))
138+ base_dir = os .path .join (output_directory .replace ("/lib" , "/" ),
139+ fn .relative_to (library_path ).parent )
168140 if not os .path .isdir (base_dir ):
169141 os .makedirs (base_dir )
170142 total_size += 512
171143
172144 for fn in package_files :
173- base_dir = os .path .join (output_directory , os . path . dirname ( fn ) )
145+ base_dir = os .path .join (output_directory , fn . relative_to ( library_path ). parent )
174146 if not os .path .isdir (base_dir ):
175147 os .makedirs (base_dir )
176148 total_size += 512
@@ -188,15 +160,17 @@ def library(library_path, output_directory, package_folder_prefix,
188160
189161 for filename in py_files :
190162 full_path = os .path .join (library_path , filename )
191- output_file = os .path .join (output_directory ,
192- filename .replace (".py" , new_extension ))
163+ output_file = os .path .join (
164+ output_directory ,
165+ filename .relative_to (library_path ).with_suffix (new_extension )
166+ )
193167 with tempfile .NamedTemporaryFile () as temp_file :
194168 _munge_to_temp (full_path , temp_file , library_version )
195169
196170 if mpy_cross :
197171 mpy_success = subprocess .call ([mpy_cross ,
198172 "-o" , output_file ,
199- "-s" , filename ,
173+ "-s" , str ( filename ) ,
200174 temp_file .name ])
201175 if mpy_success != 0 :
202176 raise RuntimeError ("mpy-cross failed on" , full_path )
@@ -208,21 +182,26 @@ def library(library_path, output_directory, package_folder_prefix,
208182 with tempfile .NamedTemporaryFile () as temp_file :
209183 _munge_to_temp (full_path , temp_file , library_version )
210184 if not mpy_cross or os .stat (full_path ).st_size == 0 :
211- output_file = os .path .join (output_directory , filename )
185+ output_file = os .path .join (output_directory ,
186+ filename .relative_to (library_path ))
212187 shutil .copyfile (temp_file .name , output_file )
213188 else :
214- output_file = os .path .join (output_directory ,
215- filename .replace (".py" , new_extension ))
189+ output_file = os .path .join (
190+ output_directory ,
191+ filename .relative_to (library_path ).with_suffix (new_extension )
192+ )
193+
216194 mpy_success = subprocess .call ([mpy_cross ,
217195 "-o" , output_file ,
218- "-s" , filename ,
196+ "-s" , str ( filename ) ,
219197 temp_file .name ])
220198 if mpy_success != 0 :
221199 raise RuntimeError ("mpy-cross failed on" , full_path )
222200
223201 for filename in example_files :
224202 full_path = os .path .join (library_path , filename )
225- output_file = os .path .join (output_directory .replace ("/lib" , "/" ), filename )
203+ output_file = os .path .join (output_directory .replace ("/lib" , "/" ),
204+ filename .relative_to (library_path ))
226205 with tempfile .NamedTemporaryFile () as temp_file :
227206 _munge_to_temp (full_path , temp_file , library_version )
228207 shutil .copyfile (temp_file .name , output_file )
0 commit comments