j7s_diagnostics_py/build.py

49 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Copyright 2026 James Pace
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
#
import argparse
import subprocess
import venv
from pathlib import Path
# The build for this is a little funky.
# Maturin only plays nice with pyproject.toml style configs.
# ROS2 is the opposite.
# So... We setup an venv, pip install into it (which plays nice with pyproject.toml).
# Then we copy the result to the right place for ament/ros2.
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--src-dir", type=str)
parser.add_argument("--build-dir", type=str)
parser.add_argument("--install-path", type=str)
args = parser.parse_args()
build(args.src_dir, args.build_dir, args.install_path)
def build(source_directory, build_directory, install_directory):
# Make a venv in the build_directory.
builder = venv.EnvBuilder(system_site_packages=True, with_pip=True, prompt="")
venv_directory = Path(build_directory) / "venv"
builder.create(venv_directory)
# activate it and call pip install.
command = f". {venv_directory}/bin/activate && pip install {source_directory}"
subprocess.run(command, shell=True)
# copy the result to the install directory.
command = f"cp -r {venv_directory}/lib {install_directory}"
subprocess.run(command, shell=True)
if __name__ == "__main__":
main()