|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright (c) 2016 Scott Shawcroft for Adafruit Industries |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | + |
| 25 | +import os |
| 26 | +import os.path |
| 27 | +import shlex |
| 28 | +import shutil |
| 29 | +import sys |
| 30 | +import subprocess |
| 31 | +import zipfile |
| 32 | + |
| 33 | +import circuitpython_build_tools |
| 34 | + |
| 35 | + |
| 36 | +def add_file(bundle, src_file, zip_name): |
| 37 | + bundle.write(src_file, zip_name) |
| 38 | + file_size = os.stat(src_file).st_size |
| 39 | + file_sector_size = file_size |
| 40 | + if file_size % 512 != 0: |
| 41 | + file_sector_size = (file_size // 512 + 1) * 512 |
| 42 | + print(zip_name, file_size, file_sector_size) |
| 43 | + return file_sector_size |
| 44 | + |
| 45 | + |
| 46 | +def build_bundle(lib_location, bundle_version, output_filename, |
| 47 | + mpy_cross=None): |
| 48 | + build_dir = "build-" + output_filename |
| 49 | + build_lib_dir = os.path.join(build_dir, "lib") |
| 50 | + if os.path.isdir(build_dir): |
| 51 | + print("Deleting existing build.") |
| 52 | + shutil.rmtree(build_dir) |
| 53 | + os.mkdir(build_dir) |
| 54 | + os.mkdir(build_lib_dir) |
| 55 | + |
| 56 | + success = True |
| 57 | + 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 | + circuitpython_build_tools.build_library(library_path, |
| 63 | + build_lib_dir, |
| 64 | + mpy_cross=mpy_cross) |
| 65 | + |
| 66 | + with open(os.path.join(build_lib_dir, "VERSIONS.txt"), "w") as f: |
| 67 | + f.write(bundle_version + "\r\n") |
| 68 | + versions = subprocess.run('git submodule foreach \"git remote get-url origin && git describe --tags\"', shell=True, stdout=subprocess.PIPE) |
| 69 | + repo = None |
| 70 | + for line in versions.stdout.split(b"\n"): |
| 71 | + if line.startswith(b"Entering") or not line: |
| 72 | + continue |
| 73 | + if line.startswith(b"git@"): |
| 74 | + repo = b"https://github.com/" + line.split(b":")[1][:-len(".git")] |
| 75 | + elif line.startswith(b"https:"): |
| 76 | + repo = line.strip()[:-len(".git")] |
| 77 | + else: |
| 78 | + f.write(repo.decode("utf-8", "strict") + "/releases/tag/" + line.strip().decode("utf-8", "strict") + "\r\n") |
| 79 | + |
| 80 | + with zipfile.ZipFile(output_filename, 'w') as bundle: |
| 81 | + total_size += add_file(bundle, "README.txt", "lib/README.txt") |
| 82 | + for filename in os.listdir("update_scripts"): |
| 83 | + src_file = os.path.join("update_scripts", filename) |
| 84 | + total_size += add_file(bundle, src_file, os.path.join("lib", filename)) |
| 85 | + for root, dirs, files in os.walk(build_lib_dir): |
| 86 | + ziproot = root[len(build_dir + "/"):].replace("-", "_") |
| 87 | + for filename in files: |
| 88 | + total_size += add_file(bundle, os.path.join(root, filename), |
| 89 | + os.path.join(ziproot, filename.replace("-", "_"))) |
| 90 | + |
| 91 | + print() |
| 92 | + print(total_size, "B", total_size / 1024, "kiB", total_size / 1024 / 1024, "MiB") |
| 93 | + print("Bundled in", zip_filename) |
| 94 | + if not success: |
| 95 | + print("WARNING: some failures above") |
| 96 | + sys.exit(2) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + from scripts import target_versions |
| 101 | + |
| 102 | + tagged = input("Did you tag this release already ([y]/n)? ") |
| 103 | + if tagged and tagged.lower() != 'y': |
| 104 | + print("Go ahead and tag. I'll wait.") |
| 105 | + sys.exit(3) |
| 106 | + |
| 107 | + bundle_lib_location = os.path.abspath(sys.argv[1]) |
| 108 | + output_dir = os.path.abspath(sys.argv[2]) |
| 109 | + os.chdir(bundle_lib_location) |
| 110 | + |
| 111 | + bundle_version = None |
| 112 | + tag = subprocess.run('git describe --tags --exact-match', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 113 | + if tag.returncode == 0: |
| 114 | + bundle_version = tag |
| 115 | + else: |
| 116 | + commitish = subprocess.run("git log --pretty=format:'%h' -n 1", shell=True, stdout=subprocess.PIPE) |
| 117 | + bundle_version = commitish |
| 118 | + bundle_version = bundle_version.stdout.strip().decode("utf-8", "strict") |
| 119 | + |
| 120 | + zip_filename = os.path.join(output_dir, |
| 121 | + 'adafruit-circuitpython-bundle-{VERSION}.zip'.format( |
| 122 | + VERSION=bundle_version)) |
| 123 | + build_bundle(bundle_lib_location, bundle_version, zip_filename) |
| 124 | + os.makedirs("build_deps", exist_ok=True) |
| 125 | + for version in target_versions.VERSIONS: |
| 126 | + mpy_cross = "build_deps/mpy-cross-" + version["name"] |
| 127 | + circuitpython_build_tools.build_mpy_cross(mpy_cross, version["tag"]) |
| 128 | + zip_filename = os.path.join(output_dir, |
| 129 | + 'adafruit-circuitpython-bundle-{TAG}-{VERSION}.zip'.format( |
| 130 | + TAG=version["name"], |
| 131 | + VERSION=bundle_version)) |
| 132 | + build_bundle(bundle_lib_location, bundle_version, zip_filename, mpy_cross=mpy_cross) |
0 commit comments