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/PrintVersion.py
Views: 1798
1
#!/usr/bin/env python
2
3
"""
4
Extract version information for the various vehicle types, print it
5
6
AP_FLAKE8_CLEAN
7
8
"""
9
10
import os
11
import re
12
import sys
13
14
from optparse import OptionParser
15
16
parser = OptionParser("print_version.py [options] ArduCopter|ArduPlane|Rover|AntennaTracker")
17
18
(opts, args) = parser.parse_args()
19
20
includefiles = {
21
"ArduCopter": "version.h",
22
"ArduPlane": "version.h",
23
"Rover": "version.h",
24
"AntennaTracker": "version.h",
25
"ArduSub": "version.h",
26
}
27
28
if len(args) > 0:
29
vehicle = args[0]
30
if vehicle not in includefiles:
31
print("Unknown vehicle (%s) (be in a vehicle directory or supply a vehicle type as an argument)" % (vehicle,))
32
sys.exit(1)
33
includefilepath = "%s/%s" % (vehicle, includefiles[vehicle])
34
else:
35
# assume we are in e.g. APM/Rover/
36
vehicle = os.path.basename(os.getcwd())
37
if vehicle not in includefiles:
38
print("Unknown vehicle (%s) (be in a vehicle directory or supply a vehicle type as an argument)" % (vehicle,))
39
sys.exit(1)
40
includefilepath = includefiles[vehicle]
41
42
43
file = open(includefilepath)
44
45
firmware_version_regex = re.compile(r".*define +FIRMWARE_VERSION.*")
46
firmware_version_extract_regex = re.compile(r".*define +FIRMWARE_VERSION[ ]+(?P<major>\d+)[ ]*,[ ]*(?P<minor>\d+)[ ]*,[ ]*(?P<point>\d+)[ ]*,[ ]*(?P<type>[A-Z_]+)[ ]*") # noqa: E501
47
48
for line in file:
49
if not firmware_version_regex.match(line):
50
continue
51
match = firmware_version_extract_regex.match(line)
52
if not match:
53
print("Failed to match FIRMWARE_VERSION line (%s)" % (line,))
54
sys.exit(1)
55
print("%d.%d.%d-%s" % (int(match.group("major")),
56
int(match.group("minor")),
57
int(match.group("point")),
58
match.group("type")))
59
60