iot-firmware/gen_release.py

67 lines
2.6 KiB
Python

import os
import shutil
import subprocess
import sys
import platform
import subprocess
import os
import shutil
import subprocess
import sys
import platform
# Get the current directory
current_dir = os.path.dirname(os.path.abspath(__file__))
# Define the path to the firmware folder
firmware_folder = os.path.join(current_dir, 'firmware')
# Define the path to the release folder
release_folder = os.path.join(current_dir, 'release')
# Add the path to the PlatformIO executable to the system PATH
platformio_path = os.path.expanduser('~/.platformio/penv/Scripts')
if os.path.exists(release_folder):
# If the release folder exists, delete it
shutil.rmtree(release_folder)
os.makedirs(release_folder)
# Get the current Git branch name
git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
# Build the PlatformIO project
if len(sys.argv) > 1:
# If arguments are supplied, build the environments that match the arguments
environments = sys.argv[1:]
for environment in environments:
subprocess.run([f'{platformio_path}/platformio.exe', 'run', '-e', environment], cwd=current_dir)
# Iterate over the subfolders in the firmware folder
for subfolder in os.listdir(firmware_folder):
subfolder_path = os.path.join(firmware_folder, subfolder)
# Check if the subfolder matches the environment argument
if subfolder == environment:
# Check if the subfolder contains a firmware.bin file
firmware_file = os.path.join(subfolder_path, 'firmware.bin')
if os.path.isfile(firmware_file):
# Move the firmware.bin file to the release folder with the Git branch name appended
new_file_name = os.path.join(release_folder, f"{subfolder}_{git_branch}.bin")
shutil.move(firmware_file, new_file_name)
else:
# If no argument is supplied, build all environments
subprocess.run([f'{platformio_path}/platformio.exe', 'run'], cwd=current_dir)
# Iterate over the subfolders in the firmware folder
for subfolder in os.listdir(firmware_folder):
subfolder_path = os.path.join(firmware_folder, subfolder)
# Check if the subfolder contains a firmware.bin file
firmware_file = os.path.join(subfolder_path, 'firmware.bin')
if os.path.isfile(firmware_file):
# Move the firmware.bin file to the release folder with the Git branch name appended
new_file_name = os.path.join(release_folder, f"{subfolder}_{git_branch}.bin")
shutil.move(firmware_file, new_file_name)