forked from merbanan/rtl_433
-
Notifications
You must be signed in to change notification settings - Fork 2
/
maintainer_update.py
executable file
·176 lines (144 loc) · 5.52 KB
/
maintainer_update.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
"""rtl_433 maintainer updates to build files and docs."""
import sys
import os
import subprocess
import glob
import re
def require_clean_work_tree():
"""Check if the working tree is clean, exit otherwise."""
clean = not len(subprocess.check_output(["git", "diff", "--stat"]))
if not clean:
print("Please commit or stash your changes.")
exit(1)
def grep_lines(pattern, filepath):
with open(filepath, 'r') as file:
filedata = file.read()
regex = re.compile(pattern)
return regex.findall(filedata)
def replace_text(pattern, repl, filepath):
with open(filepath, 'r') as file:
filedata = file.read()
regex = re.compile(pattern)
filedata = regex.sub(repl, filedata)
with open(filepath, 'w') as file:
file.write(filedata)
def replace_block(from_pattern, to_pattern, repl, filepath):
with open(filepath, 'r') as file:
filedata = file.read()
pattern = '(?ms)(' + from_pattern + ').*?(' + to_pattern + ')'
repl = r'\g<1>%s\g<2>' % repl
regex = re.compile(pattern)
filedata = regex.sub(repl, filedata)
with open(filepath, 'w') as file:
file.write(filedata)
def get_help_text(option):
try:
help_text = subprocess.check_output(
["./build/src/rtl_433", "-c", "0", option], stderr=subprocess.STDOUT).decode('utf-8')
except subprocess.CalledProcessError as e:
help_text = e.output.decode('utf-8')
# trim help text
help_text = re.sub(r'(?s).*Usage:', '', help_text)
help_text = re.sub(r'(?s).*option requires an argument -- \'?.\'?', '', help_text)
# help_text = re.sub(r'(?m)^\s*=\s+(.*)\s+=\s*$', r'### \1', help_text)
return help_text
def markup_man_text(help_text):
# sub section headings
help_text = re.sub(r'(?m)^\s*=\s+(.*)\s+=\s*$', r'.SS "\1"', help_text)
# indented lines
help_text = re.sub(r'(?m)^\t(.*)$', r'.RS\n\1\n.RE', help_text)
# options
help_text = re.sub(r'(?m)^\s*\[(\S*)(.*)\]\s*(.*)$',
r'.TP\n[ \\\\fB\1\\\\fI\2\\\\fP ]\n\3', help_text)
# fix hyphens
help_text = re.sub(r'-', '\\-', help_text)
# fix quotes
help_text = re.sub(r'(?m)^\'', ' \'', help_text)
return help_text
def parse_devices(devices_text):
devices = []
for line in devices_text.splitlines():
# match the [123] device number
device_info = re.search(r'\[(\d{1,5})\](.) (.*)', line)
if not device_info:
continue
device_number = int(device_info.group(1).strip(), base=10)
is_disabled = device_info.group(2).strip() == "*"
device_text = device_info.group(3).strip()
devices.append((device_number, device_text, is_disabled))
return devices
verbose = '-v' in sys.argv
# Make sure we run from the top dir
topdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(topdir)
# Only ever run on a clean working tree
require_clean_work_tree()
# glob all src and device files
os.chdir("src")
src_files = sorted(glob.glob('*.c'))
if (verbose):
print("src_files =", src_files)
device_files = sorted(glob.glob('devices/*.c'))
if (verbose):
print("device_files =", device_files)
os.chdir("..")
# glob all includes
os.chdir("include")
include_files = sorted(glob.glob('*.h'))
if (verbose):
print("include_files =", include_files)
os.chdir("..")
# grep all r_devices
r_devices = [grep_lines(r'(?m)^r_device\s*(.*?)\s*=.*',
os.path.join("src", p)) for p in device_files]
r_devices = [item for sublist in r_devices for item in sublist]
if (verbose):
print("r_devices =", r_devices)
# count r_devices, correct for 'new_template' being used six times
r_devices_used = len(r_devices) + 5
# src/CMakeLists.txt
repl = src_files + device_files
repl.remove('rtl_433.c') # exclude apps from lib sources
repl = '\n ' + ('\n '.join(repl)) + '\n'
replace_block(r'add_library\(r_433 STATIC$',
r'^\)', repl, 'src/CMakeLists.txt')
# include/rtl_433.h
# update '#define MAX_PROTOCOLS ?' with actual count
#replace_text(r'(?m)(#define\s+MAX_PROTOCOLS\s+)\d+',
# r'\g<1>%d' % r_devices_used, 'include/rtl_433.h')
# include/rtl_433_devices.h
# check that everything between '#define DEVICES' and \n\n with DECL(device_name) matches r_devices
# TODO: implement the check...
if (not os.path.isfile("./build/src/rtl_433")):
print("\nWARNING: rtl_433 binary not found: skipping README/man generation!\n")
exit(0)
# README.md
# Replace everything between ``` with help output.
repl = '\n' + get_help_text('-h') + '\n'
devices = get_help_text('-R') + '\n'
repl2 = get_help_text('-d') + '\n'
repl2 += get_help_text('-g') + '\n'
repl2 += get_help_text('-X') + '\n'
repl2 += get_help_text('-F') + '\n'
repl2 += get_help_text('-M') + '\n'
repl2 += get_help_text('-r') + '\n'
repl2 += get_help_text('-w') + '\n'
replace_block(r'```',
r'```', repl + devices + repl2, 'README.md')
# conf/rtl_433.example.conf
parsed_devices = parse_devices(devices)
conf_text = ""
for dev_num, dev_descr, disabled in parsed_devices:
comment = "# " if disabled else " "
spaces = (4 - len(str(dev_num))) * " "
text = f"{comment}protocol {dev_num}{spaces}# {dev_descr}\n"
conf_text += text
#print(dev_num, "-" if disabled else "+", dev_descr)
print(conf_text)
replace_block(r'## Protocols to enable \(command line option \"-R\"\)\n',
"## Flex devices", "\n" + conf_text + "\n", "conf/rtl_433.example.conf")
# MAN pages
repl = markup_man_text(repl + repl2)
replace_block(r'\.\\" body',
r'\.\\" end', '\n'+repl, 'man/man1/rtl_433.1')