diff --git a/.gitignore b/.gitignore index d37dde1..bdc037d 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ gcc-arm-none-eabi/gcc_arm_none_eabi/toolchain/ # downloaded git-lfs git-lfs/git_lfs/bin/ + +# downloaded mdbook +mdbook/mdbook/bin/ diff --git a/mdbook/build.sh b/mdbook/build.sh new file mode 100755 index 0000000..a90414d --- /dev/null +++ b/mdbook/build.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -e + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" +cd "$DIR" + +VERSION="0.5.2" +INSTALL_DIR="$DIR/mdbook/bin" +VERSION_FILE="$INSTALL_DIR/.version" + +# Skip if already at correct version +if [ -f "$VERSION_FILE" ] && [ "$(cat "$VERSION_FILE")" = "$VERSION" ]; then + echo "mdbook $VERSION already present, skipping." + exit 0 +fi + +OS="$(uname -s)" +ARCH="$(uname -m)" + +case "${OS}-${ARCH}" in + Linux-x86_64) TARGET="x86_64-unknown-linux-musl" ;; + Linux-aarch64) TARGET="aarch64-unknown-linux-musl" ;; + Darwin-arm64) TARGET="aarch64-apple-darwin" ;; + *) + echo "Unsupported platform: ${OS}-${ARCH}" >&2 + exit 1 + ;; +esac + +FILENAME="mdbook-v${VERSION}-${TARGET}.tar.gz" +URL="https://github.com/rust-lang/mdBook/releases/download/v${VERSION}/${FILENAME}" + +echo "Downloading $FILENAME ..." +curl -fSL -o "$FILENAME" "$URL" + +echo "Extracting ..." +mkdir -p "$INSTALL_DIR" +tar -xzf "$FILENAME" -C "$INSTALL_DIR" mdbook + +chmod +x "$INSTALL_DIR/mdbook" + +rm -f "$FILENAME" +echo "$VERSION" > "$VERSION_FILE" + +echo "Installed mdbook to $INSTALL_DIR" +du -sh "$INSTALL_DIR" diff --git a/mdbook/mdbook/__init__.py b/mdbook/mdbook/__init__.py new file mode 100644 index 0000000..a118955 --- /dev/null +++ b/mdbook/mdbook/__init__.py @@ -0,0 +1,15 @@ +import os +import sys + +BIN_DIR = os.path.join(os.path.dirname(__file__), "bin") + + +def _run(): + binary = os.path.join(BIN_DIR, "mdbook") + os.execvp(binary, [binary] + sys.argv[1:]) + + +def smoketest(): + import subprocess + binary = os.path.join(BIN_DIR, "mdbook") + subprocess.run([binary, "--version"], check=True) diff --git a/mdbook/pyproject.toml b/mdbook/pyproject.toml new file mode 100644 index 0000000..0cb35c9 --- /dev/null +++ b/mdbook/pyproject.toml @@ -0,0 +1,18 @@ +[build-system] +requires = ["setuptools>=64", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "mdbook" +version = "0.5.2" +description = "mdBook command-line tool for creating books from Markdown" +requires-python = ">=3.8" + +[project.scripts] +mdbook = "mdbook:_run" + +[tool.setuptools.packages.find] +include = ["mdbook*"] + +[tool.setuptools.package-data] +mdbook = ["bin/**/*"] diff --git a/mdbook/setup.py b/mdbook/setup.py new file mode 100644 index 0000000..35fde4f --- /dev/null +++ b/mdbook/setup.py @@ -0,0 +1,58 @@ +import os +import platform +import subprocess + +from setuptools.command.build_py import build_py + +try: + from wheel.bdist_wheel import bdist_wheel +except ImportError: + bdist_wheel = None + + +class BuildMdbook(build_py): + """Run build.sh to download mdbook before collecting package data.""" + + def run(self): + pkg_dir = os.path.dirname(os.path.abspath(__file__)) + build_script = os.path.join(pkg_dir, "build.sh") + subprocess.check_call(["bash", build_script], cwd=pkg_dir) + + super().run() + + +cmdclass = {"build_py": BuildMdbook} + +if bdist_wheel is not None: + + class PlatformWheel(bdist_wheel): + """Produce a platform-specific, Python-version-agnostic wheel.""" + + def finalize_options(self): + super().finalize_options() + self.root_is_pure = False + + def get_tag(self): + system = platform.system() + machine = platform.machine() + + if system == "Linux": + plat = f"linux_{machine}" + elif system == "Darwin": + plat = "macosx_11_0_arm64" + else: + plat = f"{system.lower()}_{machine}" + + return "py3", "none", plat + + cmdclass["bdist_wheel"] = PlatformWheel + + +def setup(): + from setuptools import setup as _setup + + _setup(cmdclass=cmdclass) + + +if __name__ == "__main__": + setup() diff --git a/pyproject.toml b/pyproject.toml index d65860c..2380ca6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ members = [ "libusb", "libjpeg", "libyuv", + "mdbook", "nanosvg", "ncurses", "qt5",