25
25
import json
26
26
import os
27
27
import os .path
28
+ import re
28
29
import shlex
29
30
import shutil
30
31
import subprocess
38
39
39
40
import pkg_resources
40
41
42
+ NOT_BUNDLE_LIBRARIES = [
43
+ "adafruit-blinka" ,
44
+ "adafruit-blinka-bleio" ,
45
+ "adafruit-blinka-displayio" ,
46
+ "pyserial" ,
47
+ ]
48
+
41
49
def add_file (bundle , src_file , zip_name ):
42
50
bundle .write (src_file , zip_name )
43
51
file_size = os .stat (src_file ).st_size
@@ -47,6 +55,55 @@ def add_file(bundle, src_file, zip_name):
47
55
print (zip_name , file_size , file_sector_size )
48
56
return file_sector_size
49
57
58
+ def get_module_name (library_path ):
59
+ """Figure out the module or package name anbd return it"""
60
+ url = subprocess .run ('git remote get-url origin' , shell = True , stdout = subprocess .PIPE , cwd = library_path )
61
+ url = url .stdout .decode ("utf-8" , errors = "ignore" ).strip ().lower ()
62
+ module_name = url [:- 4 ].split ("/" )[- 1 ].replace ("_" , "-" )
63
+ return module_name
64
+
65
+ def get_bundle_requirements (directory ):
66
+ """
67
+ Open the requirements.txt if it exists
68
+ Remove anything that shouldn't be a requirement like Adafruit_Blinka
69
+ Return the list
70
+ """
71
+
72
+ libraries = []
73
+ path = directory + "/requirements.txt"
74
+ if os .path .exists (path ):
75
+ with open (path , "r" ) as file :
76
+ requirements = file .read ()
77
+ file .close ()
78
+ for line in requirements .split ("\n " ):
79
+ line = line .lower ().strip ()
80
+ if line .startswith ("#" ) or line == "" :
81
+ # skip comments
82
+ pass
83
+ else :
84
+ if any (operators in line for operators in [">" , "<" , "=" ]):
85
+ # Remove everything after any pip style version specifiers
86
+ line = re .split ("[<|>|=|]" , line )[0 ]
87
+ if line not in libraries and line not in NOT_BUNDLE_LIBRARIES :
88
+ libraries .append (line )
89
+ return libraries
90
+
91
+ def build_bundle_json (libs , bundle_version , output_filename , package_folder_prefix ):
92
+ """
93
+ Generate a JSON file of all the libraries in libs
94
+ """
95
+ library_submodules = {}
96
+ for library_path in libs :
97
+ library = {}
98
+ package_info = build .get_package_info (library_path , package_folder_prefix )
99
+ module_name = get_module_name (library_path )
100
+ library ["package" ] = package_info ["is_package" ]
101
+ library ["path" ] = "lib/" + package_info ["module_name" ]
102
+ library ["dependencies" ] = get_bundle_requirements (library_path )
103
+ library_submodules [module_name ] = library
104
+ out_file = open (output_filename , "w" )
105
+ json .dump (library_submodules , out_file )
106
+ out_file .close ()
50
107
51
108
def build_bundle (libs , bundle_version , output_filename , package_folder_prefix ,
52
109
build_tools_version = "devel" , mpy_cross = None , example_bundle = False ):
@@ -188,3 +245,9 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
188
245
VERSION = bundle_version ))
189
246
build_bundle (libs , bundle_version , zip_filename , package_folder_prefix ,
190
247
build_tools_version = build_tools_version , example_bundle = True )
248
+
249
+ # Build Bundle JSON
250
+ json_filename = os .path .join (output_directory ,
251
+ filename_prefix + '-{VERSION}.json' .format (
252
+ VERSION = bundle_version ))
253
+ build_bundle_json (libs , bundle_version , json_filename , package_folder_prefix )
0 commit comments