3 A utility script for generating documentation.
5 Preprocessor macro output from opts_macro.h is parsed and combined with
6 bcachefs.5.rst.tmpl to generate bcachefs.5.rst.
15 TEMPLATE = './doc/bcachefs.5.rst.tmpl'
16 RST_FILE= './doc/bcachefs.5.rst'
27 Parses opts_macro.h preprocessor output
28 :param text: text to sanitize
30 :returns: a list of options
35 reg = re.search('FMT_START_SECTION(.*)FMT_END_SECTION', text,
38 raise re.error('no text found')
40 # decoding would probably be better, but this works
41 for char in SANITIZE_CHARS:
42 text = text.replace(char, '')
44 text = re.split(r'FMT_END_LINE', text)
46 # this seemed easier than getting preprocessor macros to play nice
47 # with python's builtin csv module
49 vals = line.split(';')
54 vals = list(map(str.strip, vals))
55 name, is_bool, desc, arg_name = vals
57 # this macro value from opts.h indicates that no values are passed
58 if is_bool == 'OPT_BOOL()':
59 args.append(f'--{name}\n{INDENT}{desc}')
61 args.append(f'--{name} <{arg_name}>\n{INDENT}{desc}')
63 raise re.error('no args found, likely parsing error')
69 ''' Transform stdin to option list and write templated output to new file '''
72 stdin = sys.stdin.read()
73 opts = sanitize(stdin)
74 opts = '\n'.join(opts)
76 # Insert into template
77 with open(TEMPLATE, 'r') as in_handle:
78 in_handle = in_handle.read()
79 out = in_handle.replace('OPTIONS_TABLE', opts)
80 with open(RST_FILE, 'w') as out_handle:
84 if __name__ == '__main__':