forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename-ifdefs.py
85 lines (74 loc) · 1.93 KB
/
rename-ifdefs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
# This script was used during the #define FEATURE -> #define USE_FEATURE
# migration. It's commited to the repo in case it will be useful again
# in the future.
# Conditional flags in the RENAMES list are renamed prepending USE_ to
# them unles a different renaming is found in the NEW_NAMES map.
#
# This script should be able to replace all ocurrences in all the source
# code for the project, including the settings.yaml file.
import os
import re
RENAMES = [
'ACC',
'GYRO',
'BARO',
'MAG',
'LED_STRIP',
'SPEKTRUM_BIND',
'SERIAL_RX',
'BLACKBOX',
'GPS',
'GPS_PROTO_UBLOX',
'TELEMETRY',
'TELEMETRY_LTM',
'TELEMETRY_FRSKY',
'CMS',
'GPS_PROTO_NMEA',
'GPS_PROTO_I2C_NAV',
'GPS_PROTO_NAZA',
'GPS_PROTO_UBLOX_NEO7PLUS',
'GPS_PROTO_MTK',
'TELEMETRY_HOTT',
'TELEMETRY_IBUS',
'TELEMETRY_MAVLINK',
'TELEMETRY_SMARTPORT',
'TELEMETRY_CRSF',
'PWM_DRIVER_PCA9685',
'PITOT',
'OSD',
'NAV',
'ASYNC_GYRO_PROCESSING',
'BOOTLOG',
'STATS',
'FIXED_WING_LANDING',
]
NEW_NAMES = {
'FIXED_WING_LANDING': 'NAV_FIXED_WING_LANDING',
}
REPLS = [
'(define ){0}(\W|$)',
'(undef ){0}(\W|$)',
'(ifdef ){0}(\W|$)',
'(defined\\(){0}(\\)(\W|$))',
'(condition: ){0}(\W|$)',
]
def replace_in_files(root):
def visit(arg, dirname, names):
for n in names:
p = os.path.join(dirname, n)
if os.path.isfile(p):
with open(p) as f:
data = f.read()
new_data = data
for name in RENAMES:
new_name = NEW_NAMES.get(name, 'USE_' + name)
repl = '\\1' + new_name + '\\2'
for r in REPLS:
pattern = r.format(name)
new_data = re.sub(pattern, repl, new_data)
if new_data != data:
with open(p, 'w') as f:
f.write(new_data)
os.path.walk(root, visit, None)
replace_in_files('src')