From 9bf73caf42c1b54e3ffdb126af7d46f44ebcbc3b Mon Sep 17 00:00:00 2001 From: strahi-linux Date: Thu, 15 Aug 2024 14:42:55 +0200 Subject: [PATCH] Added BSP asset packing and upload --- scripts/package.py | 38 ++++++++++++++++++++++++++++++-------- scripts/support.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/scripts/package.py b/scripts/package.py index 7c1ea8dd1..2250a81f9 100644 --- a/scripts/package.py +++ b/scripts/package.py @@ -50,6 +50,26 @@ def get_release_id(repo, tag_name, token): response.raise_for_status() return response.json()['id'] +def zip_bsp_related_files(archive_path, repo_dir): + support.copy_files_with_structure( + os.path.join(repo_dir, 'bsp'), + os.path.join(repo_dir, 'output/bsps'), + [ + 'board.h', + 'mcu_card.h' + ] + ) + support.copy_files_with_structure( + repo_dir, + os.path.join(repo_dir, 'output/bsps'), + [ + 'mcu_definitions.h', + 'can_definitions.h', + 'dma_definitions.h' + ] + ) + create_template_archive('output', archive_path) + if __name__ == '__main__': parser = argparse.ArgumentParser(description="Upload directories as release assets.") parser.add_argument("token", help="GitHub Token") @@ -64,16 +84,15 @@ def get_release_id(repo, tag_name, token): # Set copyright year for all files to current year support.update_copyright_year(repo_dir) + # Get the release ID used to upload assets + release_id = get_release_id(args.repo, f'mikroSDK-{version}', args.token) + if manifest_folder: archive_path = os.path.join(repo_dir, 'mikrosdk.7z') print('Creating archive: %s' % archive_path) create_7z_archive(version, repo_dir, archive_path) print('Archive created successfully: %s' % archive_path) - - # Get the release ID and upload the asset - release_id = get_release_id(args.repo, f'mikroSDK-{version}', args.token) upload_result = upload_asset_to_release(args.repo, release_id, archive_path, args.token) - print('Asset "%s" uploaded successfully to release ID: %s' % ('mikrosdk', release_id)) if os.path.exists(os.path.join(repo_dir, 'templates')): @@ -81,9 +100,12 @@ def get_release_id(repo, tag_name, token): print('Creating archive: %s' % archive_path) create_template_archive('templates', archive_path) print('Archive created successfully: %s' % archive_path) - - # Get the release ID and upload the asset - release_id = get_release_id(args.repo, f'mikroSDK-{version}', args.token) upload_result = upload_asset_to_release(args.repo, release_id, archive_path, args.token) - print('Asset "%s" uploaded successfully to release ID: %s' % ('templates', release_id)) + + # BSP asset + archive_path = os.path.join(repo_dir, 'bsps.7z') + print('Creating archive: %s' % archive_path) + zip_bsp_related_files(archive_path, repo_dir) + upload_result = upload_asset_to_release(args.repo, release_id, archive_path, args.token) + print('Asset "%s" uploaded successfully to release ID: %s' % ('bsps', release_id)) diff --git a/scripts/support.py b/scripts/support.py index c990e53d6..86d197936 100644 --- a/scripts/support.py +++ b/scripts/support.py @@ -1,4 +1,4 @@ -import requests, py7zr, zipfile, io, os, re, json +import requests, py7zr, zipfile, io, os, re, json, shutil import chardet # For detecting file encoding from datetime import datetime @@ -123,3 +123,38 @@ def update_copyright_year(directory): else: print('Updating file "%s"' % file_path) replace_copyright_year(file_path) + +def copy_files_with_structure(src_root, dst_root, filenames): + """ + Copy files named in `filenames` from `src_root` to `dst_root` while preserving the folder structure. + + Args: + src_root (str): Source root directory to search for files. + dst_root (str): Destination root directory where files will be copied. + filenames (list of str): List of filenames to copy. + """ + # Ensure filenames is a list + if not isinstance(filenames, list): + raise TypeError("filenames should be a list of strings") + + # Iterate over each directory in the source root + for dirpath, _, files in os.walk(src_root): + if dst_root in dirpath: + continue + for filename in filenames: + if filename in files: + # Construct full file path + src_file = os.path.join(dirpath, filename) + # Construct destination path preserving the folder structure + relative_path = os.path.relpath(dirpath, src_root) + dst_dir = os.path.join(dst_root, relative_path) + + # Ensure the destination directory exists + if not os.path.exists(dst_dir): + os.makedirs(dst_dir) + + # Construct destination file path + dst_file = os.path.join(dst_dir, filename) + # Copy the file + shutil.copy2(src_file, dst_file) + print(f"Copied {src_file} to {dst_file}")