Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/generate_pdef.xml_metadata.py
Views: 1798
#!/usr/bin/env python312'''3Rsync apm.pdef.xml files for different versions of the ArduPilot firmware4For each version, it checks out the corresponding tag, generates parameter metadata,5and finally rsyncs the updated parameter metadata pdef.xml files.67SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>89SPDX-License-Identifier: GPL-3.0-or-later10'''1112import os13import datetime14import shutil15import subprocess1617VEHICLE_TYPES = ["Copter", "Plane", "Rover", "ArduSub", "Tracker"] # Add future vehicle types here18RSYNC_USERNAME = 'amilcar'1920# Store the current working directory21old_cwd = os.getcwd()2223def get_vehicle_tags(vehicle_type):24"""25Lists all tags in the ArduPilot repository that start with the given vehicle type followed by '-'.26Returns a list of tag names.27"""28try:29# Change to the ArduPilot directory30os.chdir('../ardupilot/')31tags_output = subprocess.check_output(['git', 'tag', '--list', f'{vehicle_type}-[0-9]\\.[0-9]\\.[0-9]'], text=True)32return tags_output.splitlines()33except Exception as e: # pylint: disable=broad-exception-caught34print(f"Error getting {vehicle_type} tags: {e}")35return []3637def generate_vehicle_versions():38"""39Generates arrays for each vehicle type based on the tags fetched from the ArduPilot repository.40"""41vehicle_versions = {}4243for vehicle_type in VEHICLE_TYPES:44tags = get_vehicle_tags(vehicle_type)45if tags:46vehicle_versions[vehicle_type] = [tag.split('-')[1] for tag in tags]4748return vehicle_versions4950def create_one_pdef_xml_file(vehicle_type: str, dst_dir: str, git_tag: str):51os.chdir('../ardupilot')52subprocess.run(['git', 'checkout', git_tag], check=True)53# subprocess.run(['git', 'pull'], check=True)54subprocess.run(['Tools/autotest/param_metadata/param_parse.py', '--vehicle', vehicle_type, '--format', 'xml'], check=True)55# Return to the old working directory56os.chdir(old_cwd)5758if not os.path.exists(dst_dir):59os.makedirs(dst_dir)6061# Insert an XML comment on line 3 in the ../ardupilot/apm.pdef.xml file to indicate62# the tag used to generate the file and the current date63with open('../ardupilot/apm.pdef.xml', 'r', encoding='utf-8') as f:64lines = f.readlines()65lines.insert(2, f'<!-- Generated from git tag {git_tag} on {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} -->\n')66with open('../ardupilot/apm.pdef.xml', 'w', encoding='utf-8') as f:67f.writelines(lines)68shutil.copy('../ardupilot/apm.pdef.xml', f'{dst_dir}/apm.pdef.xml')6970# Function to sync files using rsync71def sync_to_remote(vehicle_dir: str) -> None:72src_dir = f'{vehicle_dir}/'73dst_user = RSYNC_USERNAME74dst_host = 'firmware.ardupilot.org'75dst_path = f'param_versioned/{vehicle_dir}/'7677# Construct the rsync command78rsync_cmd = [79'rsync',80'-avz',81'--progress',82'--password-file=.rsync_pass',83src_dir,84f'{dst_user}@{dst_host}::{dst_path}'85]8687print(f'Synchronizing {src_dir} to {dst_path}...')88print(rsync_cmd)89subprocess.run(rsync_cmd, check=True)909192def main():93vehicle_versions = generate_vehicle_versions()9495# Iterate over the vehicle_versions list96for vehicle_type, versions in vehicle_versions.items():9798vehicle_dir = vehicle_type99if vehicle_type == 'ArduSub':100vehicle_dir = 'Sub'101102for version in versions:103if version[0] == '3' and vehicle_type != 'AP_Periph':104continue # Skip ArduPilot 3.x versions, as param_parse.py does not support them out of the box105if version[0] == '4' and version[2] == '0' and vehicle_type != 'ArduSub':106continue # Skip ArduPilot 4.0.x versions, as param_parse.py does not support them out of the box107create_one_pdef_xml_file(vehicle_type, f'{vehicle_dir}/stable-{version}', f'{vehicle_type}-{version}')108109sync_to_remote(vehicle_dir)110111112if __name__ == '__main__':113main()114115116