Path: blob/master/Tools/scripts/generate_pdef.xml_metadata.py
9532 views
#!/usr/bin/env python312# flake8: noqa34'''5Rsync apm.pdef.xml files for different versions of the ArduPilot firmware6For each version, it checks out the corresponding tag, generates parameter metadata,7and finally rsyncs the updated parameter metadata pdef.xml files.89SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>1011SPDX-License-Identifier: GPL-3.0-or-later12'''1314import os15import datetime16import shutil17import subprocess1819VEHICLE_TYPES = ["Copter", "Plane", "Rover", "ArduSub", "Tracker"] # Add future vehicle types here20RSYNC_USERNAME = 'amilcar'2122# Store the current working directory23old_cwd = os.getcwd()2425def get_vehicle_tags(vehicle_type):26"""27Lists all tags in the ArduPilot repository that start with the given vehicle type followed by '-'.28Returns a list of tag names.29"""30try:31# Change to the ArduPilot directory32os.chdir('../ardupilot/')33tags_output = subprocess.check_output(['git', 'tag', '--list', f'{vehicle_type}-[0-9]\\.[0-9]\\.[0-9]'], text=True)34return tags_output.splitlines()35except Exception as e: # pylint: disable=broad-exception-caught36print(f"Error getting {vehicle_type} tags: {e}")37return []3839def generate_vehicle_versions():40"""41Generates arrays for each vehicle type based on the tags fetched from the ArduPilot repository.42"""43vehicle_versions = {}4445for vehicle_type in VEHICLE_TYPES:46tags = get_vehicle_tags(vehicle_type)47if tags:48vehicle_versions[vehicle_type] = [tag.split('-')[1] for tag in tags]4950return vehicle_versions5152def create_one_pdef_xml_file(vehicle_type: str, dst_dir: str, git_tag: str):53os.chdir('../ardupilot')54subprocess.run(['git', 'checkout', git_tag], check=True)55# subprocess.run(['git', 'pull'], check=True)56subprocess.run(['Tools/autotest/param_metadata/param_parse.py', '--vehicle', vehicle_type, '--format', 'xml'], check=True)57# Return to the old working directory58os.chdir(old_cwd)5960if not os.path.exists(dst_dir):61os.makedirs(dst_dir)6263# Insert an XML comment on line 3 in the ../ardupilot/apm.pdef.xml file to indicate64# the tag used to generate the file and the current date65with open('../ardupilot/apm.pdef.xml', 'r', encoding='utf-8') as f:66lines = f.readlines()67lines.insert(2, f'<!-- Generated from git tag {git_tag} on {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} -->\n')68with open('../ardupilot/apm.pdef.xml', 'w', encoding='utf-8') as f:69f.writelines(lines)70shutil.copy('../ardupilot/apm.pdef.xml', f'{dst_dir}/apm.pdef.xml')7172# Function to sync files using rsync73def sync_to_remote(vehicle_dir: str) -> None:74src_dir = f'{vehicle_dir}/'75dst_user = RSYNC_USERNAME76dst_host = 'firmware.ardupilot.org'77dst_path = f'param_versioned/{vehicle_dir}/'7879# Construct the rsync command80rsync_cmd = [81'rsync',82'-avz',83'--progress',84'--password-file=.rsync_pass',85src_dir,86f'{dst_user}@{dst_host}::{dst_path}'87]8889print(f'Synchronizing {src_dir} to {dst_path}...')90print(rsync_cmd)91subprocess.run(rsync_cmd, check=True)929394def main():95vehicle_versions = generate_vehicle_versions()9697# Iterate over the vehicle_versions list98for vehicle_type, versions in vehicle_versions.items():99100vehicle_dir = vehicle_type101if vehicle_type == 'ArduSub':102vehicle_dir = 'Sub'103104for version in versions:105if version[0] == '3' and vehicle_type != 'AP_Periph':106continue # Skip ArduPilot 3.x versions, as param_parse.py does not support them out of the box107if version[0] == '4' and version[2] == '0' and vehicle_type != 'ArduSub':108continue # Skip ArduPilot 4.0.x versions, as param_parse.py does not support them out of the box109create_one_pdef_xml_file(vehicle_type, f'{vehicle_dir}/stable-{version}', f'{vehicle_type}-{version}')110111sync_to_remote(vehicle_dir)112113114if __name__ == '__main__':115main()116117118