Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/scripts/generate_pdef.xml_metadata.py
9532 views
1
#!/usr/bin/env python3
2
3
# flake8: noqa
4
5
'''
6
Rsync apm.pdef.xml files for different versions of the ArduPilot firmware
7
For each version, it checks out the corresponding tag, generates parameter metadata,
8
and finally rsyncs the updated parameter metadata pdef.xml files.
9
10
SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>
11
12
SPDX-License-Identifier: GPL-3.0-or-later
13
'''
14
15
import os
16
import datetime
17
import shutil
18
import subprocess
19
20
VEHICLE_TYPES = ["Copter", "Plane", "Rover", "ArduSub", "Tracker"] # Add future vehicle types here
21
RSYNC_USERNAME = 'amilcar'
22
23
# Store the current working directory
24
old_cwd = os.getcwd()
25
26
def get_vehicle_tags(vehicle_type):
27
"""
28
Lists all tags in the ArduPilot repository that start with the given vehicle type followed by '-'.
29
Returns a list of tag names.
30
"""
31
try:
32
# Change to the ArduPilot directory
33
os.chdir('../ardupilot/')
34
tags_output = subprocess.check_output(['git', 'tag', '--list', f'{vehicle_type}-[0-9]\\.[0-9]\\.[0-9]'], text=True)
35
return tags_output.splitlines()
36
except Exception as e: # pylint: disable=broad-exception-caught
37
print(f"Error getting {vehicle_type} tags: {e}")
38
return []
39
40
def generate_vehicle_versions():
41
"""
42
Generates arrays for each vehicle type based on the tags fetched from the ArduPilot repository.
43
"""
44
vehicle_versions = {}
45
46
for vehicle_type in VEHICLE_TYPES:
47
tags = get_vehicle_tags(vehicle_type)
48
if tags:
49
vehicle_versions[vehicle_type] = [tag.split('-')[1] for tag in tags]
50
51
return vehicle_versions
52
53
def create_one_pdef_xml_file(vehicle_type: str, dst_dir: str, git_tag: str):
54
os.chdir('../ardupilot')
55
subprocess.run(['git', 'checkout', git_tag], check=True)
56
# subprocess.run(['git', 'pull'], check=True)
57
subprocess.run(['Tools/autotest/param_metadata/param_parse.py', '--vehicle', vehicle_type, '--format', 'xml'], check=True)
58
# Return to the old working directory
59
os.chdir(old_cwd)
60
61
if not os.path.exists(dst_dir):
62
os.makedirs(dst_dir)
63
64
# Insert an XML comment on line 3 in the ../ardupilot/apm.pdef.xml file to indicate
65
# the tag used to generate the file and the current date
66
with open('../ardupilot/apm.pdef.xml', 'r', encoding='utf-8') as f:
67
lines = f.readlines()
68
lines.insert(2, f'<!-- Generated from git tag {git_tag} on {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} -->\n')
69
with open('../ardupilot/apm.pdef.xml', 'w', encoding='utf-8') as f:
70
f.writelines(lines)
71
shutil.copy('../ardupilot/apm.pdef.xml', f'{dst_dir}/apm.pdef.xml')
72
73
# Function to sync files using rsync
74
def sync_to_remote(vehicle_dir: str) -> None:
75
src_dir = f'{vehicle_dir}/'
76
dst_user = RSYNC_USERNAME
77
dst_host = 'firmware.ardupilot.org'
78
dst_path = f'param_versioned/{vehicle_dir}/'
79
80
# Construct the rsync command
81
rsync_cmd = [
82
'rsync',
83
'-avz',
84
'--progress',
85
'--password-file=.rsync_pass',
86
src_dir,
87
f'{dst_user}@{dst_host}::{dst_path}'
88
]
89
90
print(f'Synchronizing {src_dir} to {dst_path}...')
91
print(rsync_cmd)
92
subprocess.run(rsync_cmd, check=True)
93
94
95
def main():
96
vehicle_versions = generate_vehicle_versions()
97
98
# Iterate over the vehicle_versions list
99
for vehicle_type, versions in vehicle_versions.items():
100
101
vehicle_dir = vehicle_type
102
if vehicle_type == 'ArduSub':
103
vehicle_dir = 'Sub'
104
105
for version in versions:
106
if version[0] == '3' and vehicle_type != 'AP_Periph':
107
continue # Skip ArduPilot 3.x versions, as param_parse.py does not support them out of the box
108
if version[0] == '4' and version[2] == '0' and vehicle_type != 'ArduSub':
109
continue # Skip ArduPilot 4.0.x versions, as param_parse.py does not support them out of the box
110
create_one_pdef_xml_file(vehicle_type, f'{vehicle_dir}/stable-{version}', f'{vehicle_type}-{version}')
111
112
sync_to_remote(vehicle_dir)
113
114
115
if __name__ == '__main__':
116
main()
117
118