|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Create source code archive, including submodules, for binary releases.""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import pathlib |
| 8 | +import subprocess |
| 9 | +import tempfile |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | + |
| 13 | +TOP_DIR = pathlib.Path(__file__).parent.parent |
| 14 | + |
| 15 | + |
| 16 | +def git_archive(directory: Optional[os.PathLike], out: os.PathLike) -> None: |
| 17 | + """Calls the ``git archive`` command line. |
| 18 | +
|
| 19 | + Arguments: |
| 20 | + directory: The subdirectory |
| 21 | + out: The output file path. |
| 22 | + """ |
| 23 | + args = ["git", "archive", f"--output={out}"] |
| 24 | + cwd = TOP_DIR |
| 25 | + if directory: |
| 26 | + cwd = cwd.joinpath(directory) |
| 27 | + args.append(f"--prefix={directory}/") |
| 28 | + args.append("HEAD") |
| 29 | + |
| 30 | + subprocess.check_call(args, cwd=cwd) |
| 31 | + |
| 32 | + |
| 33 | +def archive(hub: str) -> None: |
| 34 | + """Build source code archive from git tree for specific hub. |
| 35 | +
|
| 36 | + Only git submodules used by ``hub`` will be included. |
| 37 | + """ |
| 38 | + with tempfile.TemporaryDirectory(prefix="pybricks-micropython-archive-") as d: |
| 39 | + archives = [] |
| 40 | + |
| 41 | + # main pybricks-micropython source code |
| 42 | + main_tar = pathlib.Path(d, "pybricks-micropython.tar") |
| 43 | + git_archive(None, main_tar) |
| 44 | + archives.append(main_tar) |
| 45 | + |
| 46 | + # every hub includes micropython/ |
| 47 | + micropython_tar = pathlib.Path(d, "micropython.tar") |
| 48 | + git_archive("micropython", micropython_tar) |
| 49 | + archives.append(micropython_tar) |
| 50 | + |
| 51 | + # only STM32 hubs have stm32lib |
| 52 | + if hub in ["cityhub", "movehub", "primehub", "technichub"]: |
| 53 | + stm32lib_tar = pathlib.Path(d, "stm32lib.tar") |
| 54 | + git_archive("micropython/lib/stm32lib", stm32lib_tar) |
| 55 | + archives.append(stm32lib_tar) |
| 56 | + |
| 57 | + # every hub include libfixmath |
| 58 | + libfixmath_tar = pathlib.Path(d, "libfixmath.tar") |
| 59 | + git_archive("lib/libfixmath", libfixmath_tar) |
| 60 | + archives.append(libfixmath_tar) |
| 61 | + |
| 62 | + # extra library for NXT |
| 63 | + if hub == "nxt": |
| 64 | + nxt_tar = pathlib.Path(d, "nxt.tar") |
| 65 | + git_archive("bricks/nxt/nxt-firmware-drivers", nxt_tar) |
| 66 | + archives.append(nxt_tar) |
| 67 | + |
| 68 | + # merge all of the individual tar files |
| 69 | + final_tar = pathlib.Path(f"pybricks-micropython-{hub}.tar.gz") |
| 70 | + final_tar.unlink(missing_ok=True) |
| 71 | + for a in archives: |
| 72 | + subprocess.check_call(["tar", "--concatenate", f"--file={final_tar}", a]) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + parser = argparse.ArgumentParser() |
| 77 | + parser.add_argument("hub", metavar="<hub>", help="The type of hub") |
| 78 | + args = parser.parse_args() |
| 79 | + archive(args.hub) |
0 commit comments