Update gen_release.py

This commit is contained in:
Siwat Sirichai 2023-12-05 19:46:48 +07:00
parent 921a5f1cc4
commit fc04b190a4
1 changed files with 31 additions and 26 deletions

View File

@ -9,6 +9,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import platform import platform
import re
# Get the current directory # Get the current directory
@ -30,37 +31,41 @@ os.makedirs(release_folder)
# Get the current Git branch name # Get the current Git branch name
git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip() git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()[:10]
# Build the PlatformIO project # If arguments are supplied, build the environments that match the arguments
if len(sys.argv) > 1: if len(sys.argv) > 1:
# If arguments are supplied, build the environments that match the arguments
environments = sys.argv[1:] 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)
# If no argument are supplied, build all environments listed in platformio.ini
else:
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)
for environment in environments:
subprocess.run([f'{platformio_path}/platformio.exe', 'run', '-e', environment,f'-DFW_VERSION={git_branch}_{environment}_{commit_hash}'], cwd=current_dir)
# Iterate over the subfolders in the firmware folder # Iterate over the subfolders in the firmware folder
for subfolder in os.listdir(firmware_folder): for subfolder in os.listdir(firmware_folder):
subfolder_path = os.path.join(firmware_folder, subfolder) subfolder_path = os.path.join(firmware_folder, subfolder)
# Check if the subfolder contains a firmware.bin file # Check if the subfolder matches the environment argument
firmware_file = os.path.join(subfolder_path, 'firmware.bin') if subfolder == environment:
if os.path.isfile(firmware_file): # Check if the subfolder contains a firmware.bin file
# Move the firmware.bin file to the release folder with the Git branch name appended firmware_file = os.path.join(subfolder_path, 'firmware.bin')
new_file_name = os.path.join(release_folder, f"{subfolder}_{git_branch}.bin") if os.path.isfile(firmware_file):
shutil.move(firmware_file, new_file_name) # 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)