peach-package-builder/peach_package_builder/build_rust_packages.py

98 lines
3.4 KiB
Python

#!/usr/bin/env python3
import subprocess
import argparse
import sys
import os
from peach_package_builder.constants import *
from peach_package_builder.utils import add_deb_to_freight, update_freight_cache
def add_debs_dir_to_freight():
"""
adds all packages in MICROSERVICES_DEB_DIR to freight cache
"""
print("[ ADDING PACKAGES TO FREIGHT LIBRARY ]")
for package in os.scandir(MICROSERVICES_DEB_DIR):
if package.name.endswith(".deb"):
add_deb_to_freight(package_name=package.name, package_path=package.path)
update_freight_cache()
def build_rust_packages(default_branch=False, package=None):
"""
builds all PeachCloud microservices written in rust and copies them to MICROSERVICES_DEB_DIR
:param default_branch: checks out main git branch if True
:param package: if provided, only builds this package
"""
print("[ BUILDING AND UPDATING RUST MICROSERVICE PACKAGES ]")
build_peach_workspace_rust_packages(default_branch=default_branch, package=package)
# update freight cache
update_freight_cache()
def build_peach_workspace_rust_packages(default_branch=False, package=None):
print("[ BUILDING AND UPDATING PEACH WORKSPACE RUST PACKAGES ]")
# this arg ensures we build the default branch, otherwise we build what ever is found locally
if default_branch:
remote_branch = 'origin/main'
branch = 'main'
subprocess.check_call(["git", "reset", "HEAD", "--hard"])
subprocess.check_call(["git", "checkout", branch], cwd=WORKSPACE_SRC_DIR)
subprocess.check_call(["git", "fetch", "--all"], cwd=WORKSPACE_SRC_DIR)
subprocess.check_call(["git", "reset", "--hard", remote_branch], cwd=WORKSPACE_SRC_DIR)
# if package argument was provided, then only build that one package, otherwise build all packages
if package:
services = filter(lambda s: s["name"] == package, SERVICES)
else:
services = SERVICES
# iterate through crates and build
for service in services:
service_name = service["name"]
service_path = os.path.join(WORKSPACE_SRC_DIR, service_name)
build_rust_package(service_path, service_name)
def build_rust_package(service_path, service_name):
"""
this function builds a specific service and adds the deb to the deb dir
:param service_path: string path to where the package src code is
:param service_name: string name of the service
:return:
"""
print("[ BUILIDING SERVICE {} ]".format(service_name))
debian_package_path = subprocess.check_output(
[
CARGO_PATH,
"deb",
"--target",
"aarch64-unknown-linux-gnu"],
cwd=service_path).decode("utf-8").strip()
subprocess.call(["cp", debian_package_path, MICROSERVICES_DEB_DIR])
# add deb to freight
package_name = os.path.basename(debian_package_path)
add_deb_to_freight(package_name=package_name, package_path=debian_package_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--default",
help="Ensure default branch for all repos for build",
action="store_true"
)
parser.add_argument(
"-p",
"--package",
help="Optionally specify package to build",
required=False,
default=None
)
args = parser.parse_args()
build_rust_packages(default_branch=args.default, package=args.package)