-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathupdate-constants.py
More file actions
executable file
·51 lines (41 loc) · 1.65 KB
/
update-constants.py
File metadata and controls
executable file
·51 lines (41 loc) · 1.65 KB
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
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
import sys
def defines(file):
lines = iter(open(file).read().splitlines())
for line in lines:
if line.startswith('#define SD_MESSAGE') and '_STR ' not in line:
if line.endswith('\\'):
line = line[:-1] + next(lines)
_, name, value = line.split()
assert 'SD_' in name
assert 'SD_ID128_MAKE' in value
yield name, value
def process(includefile, docfile, *headers):
# Collects all messages from all headers and saves the sorted list back to
# headers[0]. Writes includefile (C) and docfile (rst).
# Collect all messages into a single sorted, deduplicated list
defs = sum((list(defines(file)) for file in headers), start=[])
defs = sorted(set(defs))
with open(includefile, 'wt') as out:
print(f'Writing {out.name}…')
for name, value in defs:
print(f'add_id(m, "{name}", {name}) JOINER', file=out)
with open(headers[0], 'wt') as out:
print(f'Writing {out.name}…')
for name, value in defs:
print(f'#define {name} {value}', file=out)
old = open(docfile).read().splitlines()
with open(docfile, 'wt') as out:
print(f'Writing {out.name}…')
for line in old:
print(line, file=out)
if 'autogenerated' in line:
break
for name, value in defs:
print(f' .. autoattribute:: systemd.id128.{name}', file=out)
if __name__ == '__main__':
includefile = sys.argv[1]
docfile = sys.argv[2]
headers = sys.argv[3:]
process(includefile, docfile, *headers)