Skip to content

Commit

Permalink
Added manifest geenration script
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Nov 12, 2024
1 parent b5c5a98 commit 9141ed8
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/toolbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@ jobs:
sudo chown -R root: *
working-directory: toolbox/cmsis-toolbox-${{ matrix.target }}-${{ matrix.arch }}

- name: Set up Python
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
with:
python-version: '3.11'
cache: 'pip'

- name: Install pip dependencies
run: |
pip install --upgrade pip
pip install pyyaml
- name: check architecture
shell: bash
run: |
TOOLBOX_ABS_PATH=$(pwd)/toolbox/cmsis-toolbox-${{ matrix.target }}-${{ matrix.arch }}/bin/cbridge${{ matrix.binary_extension }}
file $TOOLBOX_ABS_PATH
- name: Generate manifest file
if: always()
run: |
TOOLBOX_VERSION=$(echo ${{ github.ref }} | cut -d'/' -f3)
BINARY_EXTENSION=${{ matrix.binary_extension }}
[ -z "$BINARY_EXTENSION" ] && BINARY_EXTENSION=""
TOOLBOX_ABS_PATH=$(pwd)/toolbox/cmsis-toolbox-${{ matrix.target }}-${{ matrix.arch }}
python ./scripts/generate_manifest.py \
-d "$TOOLBOX_ABS_PATH" \
-v "$TOOLBOX_VERSION" \
-e "$BINARY_EXTENSION"
- name: Zip folders Windows
if: ${{ matrix.target == 'windows'}}
run: |
Expand Down
118 changes: 118 additions & 0 deletions scripts/generate_manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import hashlib
import yaml # To install, use: pip install pyyaml
import argparse
import subprocess
import re
import os
import logging
import sys

# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

# Define the binary files
BINARY_FILES = ["cbridge", "cbuild", "cbuild2cmake", "cbuildgen", "cpackget", "csolution", "packchk", "svdconv", "vidx2pidx"]

def calculate_checksum(filepath):
"""Calculate SHA-256 checksum of a file."""
if os.path.isfile(filepath):
logging.info(f"{filepath} is a file.")
else:
logging.info(f"{filepath} is not a file.")

sha256 = hashlib.sha256()
try:
with open(filepath, "rb") as f:
while chunk := f.read():
sha256.update(chunk)
logging.info(f"returning the sha256 for {filepath}")
return sha256.hexdigest()
except FileNotFoundError:
logging.warning(f"File not found: {filepath}")
return None
except Exception as e:
logging.error(f"Error calculating checksum for {filepath}: {e}")
return None

def get_version(binary_path):
"""Retrieve the version of the binary using the '-V' flag."""
# Ensure the binary is executable
logging.info(f"checkpoint 2 {binary_path}")
if not os.access(binary_path, os.X_OK):
logging.info(f"checkpoint 3 {binary_path}")
logging.warning(f"{binary_path} is not executable. Attempting to set executable permission.")
os.chmod(binary_path, 0o755) # Make it executable
logging.info(f"checkpoint 4 {binary_path}")
try:
logging.info(f"checkpoint 5 {binary_path}")
result = subprocess.run(
[binary_path, '-V'],
shell=True,
capture_output=True,
universal_newlines=True
)
logging.info(f"subprocess1 run for {binary_path}")
match = re.search(r"(\d+\.\d+\.\d+.*) \(C\)", result.stdout)
logging.info(f"subprocess2 run for {binary_path}")
if match:
return match.group(1)
else:
logging.warning(f"Version format not found in output for {binary_path}")
return "unknown"
except subprocess.CalledProcessError as e:
logging.error(f"Error retrieving version for {binary_path}: {e}")
return "unknown"

def generate_manifest(args):
"""Generate the manifest with checksums and versions for binaries."""
checksums = {}
for binary in BINARY_FILES:
binary_path = os.path.join(args.toolbox_root_dir, f"bin/{binary}{args.bin_extn}")
checksum = calculate_checksum(binary_path)
logging.info("checkpoint 1")
if checksum:
version = get_version(binary_path)
checksums[binary] = {"version": version, "sha256": checksum}

manifest = {
"name": "CMSIS-Toolbox",
"version": args.toolbox_version,
"binaries": checksums
}
return manifest

def write_manifest(manifest, output_file):
"""Write the manifest to a YAML file."""
try:
with open(output_file, "w") as manifest_file:
yaml.dump(manifest, manifest_file, sort_keys=False)
logging.info(f"Manifest generated: {output_file}")
except Exception as e:
logging.error(f"Error writing manifest to {output_file}: {e}")
sys.exit(1)

def parse_arguments():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="Generate manifest file")
parser.add_argument('-d', '--toolbox_root_dir', type=str, required=True, help='Root directory of CMSIS-Toolbox')
parser.add_argument('-e', '--bin_extn', type=str, default='', help='Binary extension, e.g., .exe for Windows')
parser.add_argument('-v', '--toolbox_version', type=str, help='Release version number of CMSIS-Toolbox')

args = parser.parse_args()
args.toolbox_version = args.toolbox_version or os.getenv("GITHUB_REF_NAME", "refs/tags/v1.0.0").split('/')[-1]

return args

def main():
args = parse_arguments()
manifest = generate_manifest(args)
output_manifest_file = os.path.join(args.toolbox_root_dir, f"manifest_{args.toolbox_version}.yml")
manifest_filename = f"manifest_{args.toolbox_version}.yml"
write_manifest(manifest, output_manifest_file)

if __name__ == '__main__':
try:
main()
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
sys.exit(1)

0 comments on commit 9141ed8

Please sign in to comment.