Skip to content

Commit

Permalink
Regex changes
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Nov 13, 2024
1 parent a3c46de commit 5b3b590
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/toolbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ jobs:
pip install --upgrade pip
pip install pyyaml
- name: Check Binary arch
shell: bash
run: |
file $(pwd)/toolbox/cmsis-toolbox-${{ matrix.target }}-${{ matrix.arch }}/bin/cbridge
- name: Check Binary arch
shell: bash
run: |
uname -m
- name: Generate manifest file
shell: bash
run: |
Expand Down
77 changes: 66 additions & 11 deletions scripts/generate_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import logging
import sys
from pathlib import Path

# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
Expand All @@ -28,34 +29,88 @@ def calculate_checksum(filepath):
logging.error(f"Error calculating checksum for {filepath}: {e}")
return None

def get_version(binary_path):
def get_version(binary_path: str) -> str:
"""Retrieve the version of the binary using the '-V' flag."""
# Ensure the binary is executable

binary = Path(binary_path)
# Check if the binary exists and is executable
if not binary.is_file():
logging.error(f"Binary not found: {binary_path}")
return "unknown"
if not os.access(binary, os.X_OK):
logging.error(f"Binary is not executable: {binary_path}")
return "unknown"

try:
logging.info(f"Executing version command: {binary_path} -V")
# Execute the binary with the '-V' flag without using shell=True for security
result = subprocess.run(
[binary_path, '-V'],
shell=True,
[str(binary), '-V'],
capture_output=True,
universal_newlines=True
text=True, # Preferred over universal_newlines=True in newer Python versions
check=True, # Raises CalledProcessError if the command exits with a non-zero status
timeout=10 # Prevents the subprocess from hanging indefinitely
)
match = re.search(r'\b\d+\.\d+\.\d+(?:[-+][\w\.]+)?\b', result.stdout)

# Combine stdout and stderr in case version info is printed to stderr
output = f"{result.stdout}"
logging.debug(f"Version command output: {output}")

# Improved regex to capture version numbers more flexibly
# Adjust the pattern based on the expected output format
version_pattern = r"(\d+\.\d+\.\d+(?:[-+][\w\.]+)?)"
match = re.search(version_pattern, output)

if match:
return match.group()
version = match.group(1)
logging.info(f"Version found: {version}")
return version
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"
logging.error(f"CalledProcessError retrieving version for {binary_path}: {e}")
except subprocess.TimeoutExpired:
logging.error(f"Timeout expired while retrieving version for {binary_path}")
except FileNotFoundError:
logging.error(f"Binary not found during execution: {binary_path}")
except Exception as e:
logging.error(f"Unexpected error retrieving version for {binary_path}: {e}")

return "unknown"
# # Ensure the binary is executable
# try:
# versionCmd = binary_path + ' -V'
# logging.info(f"version command {versionCmd}")
# result = subprocess.run(versionCmd, shell=True, universal_newlines=True, capture_output=True)
# logging.info(f"version command response: {result.stdout}")
# # result = subprocess.run(
# # [binary_path, '-V'],
# # shell=True,
# # capture_output=True,
# # universal_newlines=True
# # )
# # match = re.search(r'\b\d+\.\d+\.\d+(?:[-+][\w\.]+)?\b', result.stdout)
# match = re.search(r"(\d+\.\d+\.\d+.*) \(C\)", result.stdout)
# 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}")
binary_path = args.toolbox_root_dir + f"/bin/{binary}{args.bin_extn}"
checksum = calculate_checksum(binary_path)
if checksum:
version = get_version(binary_path)
new_path = binary_path.replace("cmsis-toolbox-linux-arm64", "cmsis-toolbox-linux-amd64")
version = get_version(new_path)
checksums[binary] = {"version": version, "sha256": checksum}

manifest = {
Expand Down

0 comments on commit 5b3b590

Please sign in to comment.