CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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