iot-firmware/gen_release.py

107 lines
4.3 KiB
Python
Raw Normal View History

2023-12-04 16:03:52 +00:00
import os
import shutil
import subprocess
import sys
import subprocess
import os
import shutil
import subprocess
import sys
import platform
2023-12-05 12:46:48 +00:00
import re
2023-12-07 07:59:24 +00:00
import configparser
from time import sleep as delay
2023-12-04 16:03:52 +00:00
# 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')
2023-12-07 08:43:11 +00:00
# Get the platformio path from environment variable
platformio_path = os.environ.get('PLATFORMIO_PATH')
# If the environment variable is not set, use the default path
if platformio_path is None:
if platform.system() == 'Darwin':
platformio_path = os.path.expanduser('~/.platformio/penv/bin/platformio')
elif platform.system() == 'Windows':
platformio_path = os.path.expanduser('~/.platformio/penv/Scripts/platformio.exe')
elif platform.system() == 'Linux':
platformio_path = os.path.expanduser('~/.platformio/penv/bin/platformio')
2023-12-04 16:03:52 +00:00
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()
2023-12-05 12:46:48 +00:00
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()[:10]
2023-12-04 16:03:52 +00:00
2023-12-05 12:46:48 +00:00
# If arguments are supplied, build the environments that match the arguments
2023-12-04 16:03:52 +00:00
if len(sys.argv) > 1:
environments = sys.argv[1:]
2023-12-05 12:46:48 +00:00
# If no argument are supplied, build all environments listed in platformio.ini
2023-12-04 16:03:52 +00:00
else:
2023-12-05 12:46:48 +00:00
environments = []
with open(os.path.join(current_dir, 'platformio.ini')) as f:
# Iterate over the lines in platformio.ini
for line in f.readlines():
# Check if the line contains the string "env:"
if '[env:' in line:
# Extract the environment using regular expressions
environment = re.search(r'\[env:(.*?)\]', line).group(1)
# Add the environment to the list of environments
environments.append(environment)
2023-12-04 16:03:52 +00:00
2023-12-07 07:59:24 +00:00
# Remove old platformio.ini file from firmware folder if it exists
if os.path.isfile(os.path.join(firmware_folder, 'platformio.ini')):
os.remove(os.path.join(firmware_folder, 'platformio.ini'))
# Copy the platformio.ini file to the firmware folder
shutil.copyfile(os.path.join(current_dir, 'platformio.ini'), os.path.join(firmware_folder, 'platformio.ini'))
# Check that the file is copied correctly
if not os.path.isfile(os.path.join(firmware_folder, 'platformio.ini')):
raise Exception('platformio.ini file not copied correctly')
# Read the platformio.ini file
config = configparser.ConfigParser()
config.read(os.path.join(firmware_folder, 'platformio.ini'))
# Add firmware version to build_flags in platformio.ini
for environment in environments:
if 'build_flags' not in config[f'env:{environment}']:
config[f'env:{environment}']['build_flags'] = f'-DFW_VERSION=\\"{git_branch}_{environment}_{commit_hash}\\"'
else:
config[f'env:{environment}']['build_flags'] = config[f'env:{environment}']['build_flags'] + f' -DFW_VERSION=\\"{git_branch}_{environment}_{commit_hash}\\"'
# if argument is not supplied, build all environments listed in platformio.ini
if len(sys.argv) == 1:
2023-12-07 08:43:11 +00:00
subprocess.run([f'{platformio_path}', 'run','-c',f'{firmware_folder}/platformio.ini'], cwd=current_dir)
2023-12-07 07:59:24 +00:00
2023-12-05 12:46:48 +00:00
for environment in environments:
2023-12-07 07:59:24 +00:00
if(len(sys.argv) > 1):
2023-12-07 08:43:11 +00:00
subprocess.run([f'{platformio_path}', 'run', '-e', environment,'-c',f'{firmware_folder}/platformio.ini'], cwd=current_dir)
2023-12-05 12:46:48 +00:00
2023-12-04 16:03:52 +00:00
# Iterate over the subfolders in the firmware folder
for subfolder in os.listdir(firmware_folder):
subfolder_path = os.path.join(firmware_folder, subfolder)
2023-12-05 12:46:48 +00:00
# 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 and commit hash appended
new_file_name = os.path.join(release_folder, f"{git_branch}_{subfolder}_{commit_hash}.bin")
shutil.move(firmware_file, new_file_name)