~aleteoryx/muditaos

f731cab807f3d667fbd5b7b2068482f8da61bdee — Bartosz 3 years ago ae4badb
[MOS-805] Prepare update package - rewritten script

Rewritten script for generating update package
3 files changed, 75 insertions(+), 108 deletions(-)

M cmake/modules/AddPackage.cmake
D tools/generate_update_image.sh
A tools/generate_update_package.py
M cmake/modules/AddPackage.cmake => cmake/modules/AddPackage.cmake +2 -2
@@ 63,7 63,7 @@ function(add_update_package SOURCE_TARGET)
    endif()
    set(CPACK_PACKAGE_NAME ${SOURCE_TARGET})
    set_cpack_vars()
    set(UPDATE_PKG "${SOURCE_TARGET}-${PROJECT_VERSION}-${CPACK_SYSTEM_NAME}-Update.tar")
    set(UPDATE_PKG "${SOURCE_TARGET}-${PROJECT_VERSION}-RT1051-Update.tar")

    set(PACKAGE_UPDATE_FILE_NAME ${UPDATE_PKG} PARENT_SCOPE)
    set(PACKAGE_UPDATE_MIME "application/x-tar" PARENT_SCOPE)


@@ 76,7 76,7 @@ function(add_update_package SOURCE_TARGET)
                ecoboot.bin-target
                recovery.bin-target
                assets
        COMMAND ${CMAKE_SOURCE_DIR}/tools/generate_update_image.sh ${SOURCE_TARGET} ${PROJECT_VERSION} ${CPACK_SYSTEM_NAME}
        COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/generate_update_package.py --output_path ${CMAKE_BINARY_DIR} --system_path ${SYSROOT_PATH}/system_a --product ${SOURCE_TARGET}
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
        COMMENT "Generating update image: ${UPDATE_PKG}"
    )

D tools/generate_update_image.sh => tools/generate_update_image.sh +0 -106
@@ 1,106 0,0 @@
#!/bin/bash -e
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#create update image

function help() {
cat <<- EOF
	Create Update Image for the SOURCE_TARGET. This script should be run from build directory.

	${0} <SOURCE_TARGET> <VERSION> <PLATFORM>

	    SOURCE_TARGET    - Name of the target (usually PurePhone or BellHybrid)
	    VERSION          - version number to attach to file name
	    PLATFORM         - RT1051 or Linux

    In the current work dir, the script will create update image named from this template:
	<SOURCE_TARGET>-<VERSION>-<PLATFORM>-Update.tar



EOF
}

function setVars() {
    SOURCE_TARGET=${1}
    VERSION=${2}
    PLATFORM=${3}
    STAGEING_DIR="${SOURCE_TARGET}-${VERSION}-${PLATFORM}-Update"
    PACKAGE_FILE="${STAGEING_DIR}.tar"
    DEPS=(
        "sysroot/sys/current/assets"
        "sysroot/sys/user"
        "sysroot/sys/current/${SOURCE_TARGET}-boot.bin"
        "sysroot/sys/current/Luts.bin"
        "version.json"
        "ecoboot.bin"
        "updater.bin"
        )
}

function checkForDeps() {
    local DEPS=${1}
    I=0
    DEPS_COUNT=${#DEPS[@]}
    while [[ I -lt ${DEPS_COUNT} ]]; do
        ITEM=${DEPS[${I}]}
        if [[ ! -e "${ITEM}" ]]; then
            echo "Couldn't find dependency: \"${ITEM}\""
            echo exti 2
        fi
        I=$(( I + 1))
    done
    echo "Deps-OK"
}

function cleanStagingDir(){
    local STAGEING_DIR=${1}
    if [[ -d ${STAGEING_DIR} ]]; then
        rm -Rf ${STAGEING_DIR}
    fi
    mkdir ${STAGEING_DIR} -v
}

function linkInStageing(){
    pushd ${STAGEING_DIR} 1> /dev/null

    mkdir assets
    pushd assets 1> /dev/null
    ln -s ../../sysroot/sys/current/assets/fonts
    ln -s ../../sysroot/sys/current/assets/images
    ln -s ../../sysroot/sys/current/assets/lang
    popd 1> /dev/null

    ln -s ../sysroot/sys/user
    ln -s ../sysroot/sys/current/${SOURCE_TARGET}-boot.bin boot.bin
    ln -s ../sysroot/sys/current/Luts.bin
    ln -s ../ecoboot.bin
    ln -s ../updater.bin
    ln -s ../${SOURCE_TARGET}-version.json version.json
    popd 1> /dev/null
}

function addChecksums() {
    pushd ${STAGEING_DIR} 1> /dev/null
    rhash -u checksums.txt -r .
    popd 1> /dev/null
}

function compress() {
    tar chf ${PACKAGE_FILE} -C ${STAGEING_DIR} .
}

if [[ $# -ne 3 ]]; then
    help
    exit 1
fi

setVars "${1}" "${2}" "${3}"
checkForDeps ${DEPS}
cleanStagingDir ${STAGEING_DIR}
linkInStageing
addChecksums
compress



A tools/generate_update_package.py => tools/generate_update_package.py +73 -0
@@ 0,0 1,73 @@
#!/usr/bin/env python3
# Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import os
import argparse
import logging
import sys
import tarfile
import json

log = logging.getLogger(__name__)
logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', level=logging.INFO)


def main() -> int:
    parser = argparse.ArgumentParser(description='Create update package for MuditaOS')
    parser.add_argument('--product',
                        metavar='product',
                        type=str,
                        help='target product: BellHybrid/PurePhone',
                        required=True)

    parser.add_argument('--system_path',
                        metavar='system_path',
                        type=str,
                        help='path to the sysroot/system_a catalog',
                        required=True)

    parser.add_argument('--output_path',
                        metavar='db_path',
                        type=str,
                        help='destination path for the update package',
                        required=True)

    args = parser.parse_args()

    version = None
    json_path = os.path.join(args.system_path, "version.json")

    if os.path.exists(json_path):
        with open(json_path, "r") as json_file:
            json_data = json.load(json_file)
            version = json_data["version"]["version"]
    else:
        log.error("version.json does not exist!")
        return 1

    if version is None:
        log.error("Wrong version or damaged version.json")
        return 1

    output_name = f"{args.product}-{version}-RT1051-Update.tar"
    output_file_path = os.path.join(args.output_path, output_name)

    assets_to_archive = ["bin", "scripts", "assets", "data", "db", "version.json"]
    assets_with_path = [os.path.join(args.system_path, asset) for asset in assets_to_archive]

    if not os.path.exists(args.output_path):
        os.makedirs(args.output_path, exist_ok=True)

    if os.path.exists(output_file_path):
        os.remove(output_file_path)

    with tarfile.open(output_file_path, "w") as tar:
        for asset in assets_with_path:
            tar.add(asset, arcname=os.path.basename(asset))

    return 0


if __name__ == "__main__":
    sys.exit(main())