22
33# The MIT License (MIT)
44#
5- # Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
5+ # Copyright (c) 2016-2017 Scott Shawcroft for Adafruit Industries
66#
77# Permission is hereby granted, free of charge, to any person obtaining a copy
88# of this software and associated documentation files (the "Software"), to deal
2222# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323# THE SOFTWARE.
2424
25+ import json
2526import os
2627import os .path
2728import shlex
2829import shutil
29- import sys
3030import subprocess
31+ import sys
3132import zipfile
3233
34+ import click
35+
3336from circuitpython_build_tools import build
37+ from circuitpython_build_tools import target_versions
3438
3539import pkg_resources
3640
@@ -44,51 +48,45 @@ def add_file(bundle, src_file, zip_name):
4448 return file_sector_size
4549
4650
47- def build_bundle (lib_location , bundle_version , output_filename ,
48- mpy_cross = None ):
51+ def build_bundle (libs , bundle_version , output_filename ,
52+ build_tools_version = "devel" , mpy_cross = None ):
4953 build_dir = "build-" + os .path .basename (output_filename )
5054 build_lib_dir = os .path .join (build_dir , "lib" )
5155 if os .path .isdir (build_dir ):
5256 print ("Deleting existing build." )
5357 shutil .rmtree (build_dir )
5458 os .makedirs (build_lib_dir )
5559
60+ multiple_libs = len (libs ) > 1
61+
5662 success = True
5763 total_size = 512
58- for subdirectory in os .listdir ("libraries" ):
59- for library in os .listdir (os .path .join ("libraries" , subdirectory )):
60- library_path = os .path .join ("libraries" , subdirectory , library )
61-
62- build .library (library_path , build_lib_dir , mpy_cross = mpy_cross )
63-
64- with open (os .path .join (build_lib_dir , "VERSIONS.txt" ), "w" ) as f :
65- f .write (bundle_version + "\r \n " )
66- versions = subprocess .run ('git submodule foreach \" git remote get-url origin && git describe --tags\" ' , shell = True , stdout = subprocess .PIPE )
67- repo = None
68- for line in versions .stdout .split (b"\n " ):
69- if line .startswith (b"Entering" ) or not line :
70- continue
71- if line .startswith (b"git@" ):
72- repo = b"https://github.com/" + line .split (b":" )[1 ][:- len (".git" )]
73- elif line .startswith (b"https:" ):
74- repo = line .strip ()[:- len (".git" )]
75- else :
76- f .write (repo .decode ("utf-8" , "strict" ) + "/releases/tag/" + line .strip ().decode ("utf-8" , "strict" ) + "\r \n " )
77-
78-
79- with open (os .path .join (build_lib_dir , ".build-tools-version.txt" ), "w" ) as f :
80- pkg = pkg_resources .get_distribution ("circuitpython-travis-build-tools" )
81-
82- if pkg :
83- f .write (pkg .version + "\n " )
84- else :
85- f .write ("devel\n " )
64+ for library_path in libs :
65+ build .library (library_path , build_lib_dir , mpy_cross = mpy_cross )
66+
67+ if multiple_libs :
68+ with open (os .path .join (build_lib_dir , "VERSIONS.txt" ), "w" ) as f :
69+ f .write (bundle_version + "\r \n " )
70+ versions = subprocess .run ('git submodule foreach \" git remote get-url origin && git describe --tags\" ' , shell = True , stdout = subprocess .PIPE )
71+ repo = None
72+ for line in versions .stdout .split (b"\n " ):
73+ if line .startswith (b"Entering" ) or not line :
74+ continue
75+ if line .startswith (b"git@" ):
76+ repo = b"https://github.com/" + line .split (b":" )[1 ][:- len (".git" )]
77+ elif line .startswith (b"https:" ):
78+ repo = line .strip ()[:- len (".git" )]
79+ else :
80+ f .write (repo .decode ("utf-8" , "strict" ) + "/releases/tag/" + line .strip ().decode ("utf-8" , "strict" ) + "\r \n " )
8681
8782 with zipfile .ZipFile (output_filename , 'w' ) as bundle :
88- total_size += add_file (bundle , "README.txt" , "lib/README.txt" )
89- for filename in os .listdir ("update_scripts" ):
90- src_file = os .path .join ("update_scripts" , filename )
91- total_size += add_file (bundle , src_file , os .path .join ("lib" , filename ))
83+ build_metadata = {"build-tools-version" : build_tools_version }
84+ bundle .comment = json .dumps (build_metadata ).encode ("utf-8" )
85+ if multiple_libs :
86+ total_size += add_file (bundle , "README.txt" , "lib/README.txt" )
87+ for filename in os .listdir ("update_scripts" ):
88+ src_file = os .path .join ("update_scripts" , filename )
89+ total_size += add_file (bundle , src_file , os .path .join ("lib" , filename ))
9290 for root , dirs , files in os .walk (build_lib_dir ):
9391 ziproot = root [len (build_dir + "/" ):].replace ("-" , "_" )
9492 for filename in files :
@@ -97,23 +95,28 @@ def build_bundle(lib_location, bundle_version, output_filename,
9795
9896 print ()
9997 print (total_size , "B" , total_size / 1024 , "kiB" , total_size / 1024 / 1024 , "MiB" )
100- print ("Bundled in" , zip_filename )
98+ print ("Bundled in" , output_filename )
10199 if not success :
102100 print ("WARNING: some failures above" )
103101 sys .exit (2 )
104102
105-
106- if __name__ == "__main__" :
107- from circuitpython_build_tools import target_versions
108-
109- bundle_lib_location = os .path .abspath (sys .argv [1 ])
110- output_dir = os .path .abspath (sys .argv [2 ])
111- if len (sys .argv ) >= 3 :
112- filename_prefix = sys .argv [3 ]
113- else :
114- filename_prefix = "adafruit-circuitpython-bundle"
115- os .makedirs (output_dir , exist_ok = True )
116- os .chdir (bundle_lib_location )
103+ def _find_libraries (current_path , depth ):
104+ if depth <= 0 :
105+ return [current_path ]
106+ subdirectories = []
107+ for subdirectory in os .listdir (current_path ):
108+ path = os .path .join (current_path , subdirectory )
109+ if os .path .isdir (path ):
110+ subdirectories .extend (_find_libraries (path , depth - 1 ))
111+ return subdirectories
112+
113+ @click .command ()
114+ @click .option ('--filename_prefix' , required = True , help = "Filename prefix for the output zip files." )
115+ @click .option ('--output_directory' , default = "bundles" , help = "Output location for the zip files." )
116+ @click .option ('--library_location' , required = True , help = "Location of libraries to bundle." )
117+ @click .option ('--library_depth' , default = 0 , help = "Depth of library folders. This is useful when multiple libraries are bundled together but are initially in separate subfolders." )
118+ def build_bundles (filename_prefix , output_directory , library_location , library_depth ):
119+ os .makedirs (output_directory , exist_ok = True )
117120
118121 bundle_version = None
119122 tag = subprocess .run ('git describe --tags --exact-match' , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -124,10 +127,25 @@ def build_bundle(lib_location, bundle_version, output_filename,
124127 bundle_version = commitish
125128 bundle_version = bundle_version .stdout .strip ().decode ("utf-8" , "strict" )
126129
127- zip_filename = os .path .join (output_dir ,
130+ libs = _find_libraries (os .path .abspath (library_location ), library_depth )
131+ print (libs )
132+
133+ pkg = pkg_resources .get_distribution ("circuitpython-travis-build-tools" )
134+ build_tools_version = "devel"
135+ if pkg :
136+ build_tools_version = pkg .version
137+
138+ build_tools_fn = "z-build_tools_version-{}.ignore" .format (
139+ build_tools_version )
140+ build_tools_fn = os .path .join (output_directory , build_tools_fn )
141+ with open (build_tools_fn , "w" ) as f :
142+ f .write (build_tools_version )
143+
144+ zip_filename = os .path .join (output_directory ,
128145 filename_prefix + '-py-{VERSION}.zip' .format (
129146 VERSION = bundle_version ))
130- build_bundle (bundle_lib_location , bundle_version , zip_filename )
147+ build_bundle (libs , bundle_version , zip_filename ,
148+ build_tools_version = build_tools_version )
131149 os .makedirs ("build_deps" , exist_ok = True )
132150 for version in target_versions .VERSIONS :
133151 # Use prebuilt mpy-cross on Travis, otherwise build our own.
@@ -137,8 +155,9 @@ def build_bundle(lib_location, bundle_version, output_filename,
137155 else :
138156 mpy_cross = "build_deps/mpy-cross-" + version ["name" ]
139157 build .mpy_cross (mpy_cross , version ["tag" ])
140- zip_filename = os .path .join (output_dir ,
158+ zip_filename = os .path .join (output_directory ,
141159 filename_prefix + '-{TAG}-{VERSION}.zip' .format (
142160 TAG = version ["name" ],
143161 VERSION = bundle_version ))
144- build_bundle (bundle_lib_location , bundle_version , zip_filename , mpy_cross = mpy_cross )
162+ build_bundle (libs , bundle_version , zip_filename , mpy_cross = mpy_cross ,
163+ build_tools_version = build_tools_version )
0 commit comments