Skip to content

Commit

Permalink
Added BSP asset packing and upload
Browse files Browse the repository at this point in the history
  • Loading branch information
StrahinjaJacimovic committed Aug 15, 2024
1 parent e181eb9 commit 9bf73ca
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
38 changes: 30 additions & 8 deletions scripts/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -64,26 +84,28 @@ 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')):
archive_path = os.path.join(repo_dir, 'templates.7z')
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))
37 changes: 36 additions & 1 deletion scripts/support.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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}")

0 comments on commit 9bf73ca

Please sign in to comment.